@@ -14,8 +14,8 @@ impl TryFrom<c_int> for Error {
1414
1515 fn try_from ( value : c_int ) -> std:: result:: Result < Self , Self :: Error > {
1616 match value {
17- 33 => Ok ( Error :: EDOM ) ,
18- 34 => Ok ( Error :: ERANGE ) ,
17+ x if x == Error :: EDOM as c_int => Ok ( Error :: EDOM ) ,
18+ x if x == Error :: ERANGE as c_int => Ok ( Error :: ERANGE ) ,
1919 _ => Err ( value) ,
2020 }
2121 }
@@ -24,54 +24,66 @@ impl TryFrom<c_int> for Error {
2424/// Set errno to the given value.
2525#[ inline]
2626pub ( crate ) fn set_errno ( value : i32 ) {
27+ #[ cfg( target_os = "linux" ) ]
2728 unsafe {
28- #[ cfg( target_os = "linux" ) ]
29- {
30- * libc:: __errno_location ( ) = value;
31- }
32- #[ cfg( target_os = "macos" ) ]
33- {
34- * libc:: __error ( ) = value;
35- }
36- #[ cfg( target_os = "windows" ) ]
37- {
38- unsafe extern "C" {
39- safe fn _errno ( ) -> * mut i32 ;
40- }
41- * _errno ( ) = value;
42- }
43- #[ cfg( all( unix, not( any( target_os = "linux" , target_os = "macos" ) ) ) ) ]
44- {
45- // FreeBSD, NetBSD, OpenBSD, etc. use __error()
46- * libc:: __error ( ) = value;
29+ * libc:: __errno_location ( ) = value;
30+ }
31+ #[ cfg( target_os = "android" ) ]
32+ unsafe {
33+ * libc:: __errno ( ) = value;
34+ }
35+ #[ cfg( target_os = "macos" ) ]
36+ unsafe {
37+ * libc:: __error ( ) = value;
38+ }
39+ #[ cfg( target_os = "windows" ) ]
40+ unsafe {
41+ unsafe extern "C" {
42+ safe fn _errno ( ) -> * mut i32 ;
4743 }
44+ * _errno ( ) = value;
4845 }
46+ #[ cfg( all( unix, not( any( target_os = "linux" , target_os = "android" , target_os = "macos" ) ) ) ) ]
47+ unsafe {
48+ // FreeBSD, NetBSD, OpenBSD, etc. use __error()
49+ * libc:: __error ( ) = value;
50+ }
51+ // WASM and other targets: no-op (no errno)
52+ #[ cfg( not( any( unix, windows) ) ) ]
53+ let _ = value;
4954}
5055
5156/// Get the current errno value.
5257#[ inline]
5358pub ( crate ) fn get_errno ( ) -> i32 {
59+ #[ cfg( target_os = "linux" ) ]
5460 unsafe {
55- #[ cfg( target_os = "linux" ) ]
56- {
57- * libc:: __errno_location ( )
58- }
59- #[ cfg( target_os = "macos" ) ]
60- {
61- * libc:: __error ( )
62- }
63- #[ cfg( target_os = "windows" ) ]
64- {
65- unsafe extern "C" {
66- safe fn _errno ( ) -> * mut i32 ;
67- }
68- * _errno ( )
69- }
70- #[ cfg( all( unix, not( any( target_os = "linux" , target_os = "macos" ) ) ) ) ]
71- {
72- // FreeBSD, NetBSD, OpenBSD, etc. use __error()
73- * libc:: __error ( )
61+ * libc:: __errno_location ( )
62+ }
63+ #[ cfg( target_os = "android" ) ]
64+ unsafe {
65+ * libc:: __errno ( )
66+ }
67+ #[ cfg( target_os = "macos" ) ]
68+ unsafe {
69+ * libc:: __error ( )
70+ }
71+ #[ cfg( target_os = "windows" ) ]
72+ unsafe {
73+ unsafe extern "C" {
74+ safe fn _errno ( ) -> * mut i32 ;
7475 }
76+ * _errno ( )
77+ }
78+ #[ cfg( all( unix, not( any( target_os = "linux" , target_os = "android" , target_os = "macos" ) ) ) ) ]
79+ unsafe {
80+ // FreeBSD, NetBSD, OpenBSD, etc. use __error()
81+ * libc:: __error ( )
82+ }
83+ // WASM and other targets: no errno
84+ #[ cfg( not( any( unix, windows) ) ) ]
85+ {
86+ 0
7587 }
7688}
7789
@@ -80,8 +92,8 @@ pub(crate) fn get_errno() -> i32 {
8092pub ( crate ) fn is_error ( x : f64 ) -> Result < f64 > {
8193 match get_errno ( ) {
8294 0 => Ok ( x) ,
83- libc :: EDOM => Err ( Error :: EDOM ) ,
84- libc :: ERANGE => {
95+ e if e == Error :: EDOM as i32 => Err ( Error :: EDOM ) ,
96+ e if e == Error :: ERANGE as i32 => {
8597 // Underflow to zero is not an error.
8698 // Use 1.5 threshold to handle subnormal results that don't underflow to zero
8799 // (e.g., on Ubuntu/ia64) and to correctly detect underflows in expm1()
0 commit comments