]>
Commit | Line | Data |
---|---|---|
416331ca XL |
1 | #![allow(dead_code)] |
2 | #![allow(missing_docs, nonstandard_style)] | |
3 | ||
4 | use crate::io::ErrorKind; | |
5 | ||
416331ca | 6 | pub use self::rand::hashmap_random_keys; |
60c5eb7d | 7 | pub use crate::os::vxworks as platform; |
416331ca XL |
8 | pub use libc::strlen; |
9 | ||
10 | pub mod alloc; | |
11 | pub mod args; | |
12 | pub mod cmath; | |
13 | pub mod condvar; | |
14 | pub mod env; | |
15 | pub mod ext; | |
16 | pub mod fast_thread_local; | |
17 | pub mod fd; | |
18 | pub mod fs; | |
416331ca | 19 | pub mod io; |
60c5eb7d | 20 | pub mod memchr; |
416331ca XL |
21 | pub mod mutex; |
22 | pub mod net; | |
23 | pub mod os; | |
24 | pub mod path; | |
25 | pub mod pipe; | |
26 | pub mod process; | |
27 | pub mod rand; | |
28 | pub mod rwlock; | |
29 | pub mod stack_overflow; | |
60c5eb7d | 30 | pub mod stdio; |
416331ca XL |
31 | pub mod thread; |
32 | pub mod thread_local; | |
33 | pub mod time; | |
416331ca XL |
34 | |
35 | pub use crate::sys_common::os_str_bytes as os_str; | |
36 | ||
37 | #[cfg(not(test))] | |
38 | pub fn init() { | |
dfeec247 | 39 | // ignore SIGPIPE |
416331ca | 40 | unsafe { |
dfeec247 | 41 | assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR); |
416331ca | 42 | } |
416331ca XL |
43 | } |
44 | ||
45 | pub use libc::signal; | |
46 | ||
47 | pub fn decode_error_kind(errno: i32) -> ErrorKind { | |
48 | match errno as libc::c_int { | |
49 | libc::ECONNREFUSED => ErrorKind::ConnectionRefused, | |
50 | libc::ECONNRESET => ErrorKind::ConnectionReset, | |
51 | libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied, | |
52 | libc::EPIPE => ErrorKind::BrokenPipe, | |
53 | libc::ENOTCONN => ErrorKind::NotConnected, | |
54 | libc::ECONNABORTED => ErrorKind::ConnectionAborted, | |
55 | libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable, | |
56 | libc::EADDRINUSE => ErrorKind::AddrInUse, | |
57 | libc::ENOENT => ErrorKind::NotFound, | |
58 | libc::EINTR => ErrorKind::Interrupted, | |
59 | libc::EINVAL => ErrorKind::InvalidInput, | |
60 | libc::ETIMEDOUT => ErrorKind::TimedOut, | |
61 | libc::EEXIST => ErrorKind::AlreadyExists, | |
62 | ||
63 | // These two constants can have the same value on some systems, | |
64 | // but different values on others, so we can't use a match | |
65 | // clause | |
60c5eb7d | 66 | x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock, |
416331ca XL |
67 | |
68 | _ => ErrorKind::Other, | |
69 | } | |
70 | } | |
71 | ||
72 | #[doc(hidden)] | |
73 | pub trait IsMinusOne { | |
74 | fn is_minus_one(&self) -> bool; | |
75 | } | |
76 | ||
77 | macro_rules! impl_is_minus_one { | |
78 | ($($t:ident)*) => ($(impl IsMinusOne for $t { | |
79 | fn is_minus_one(&self) -> bool { | |
80 | *self == -1 | |
81 | } | |
82 | })*) | |
83 | } | |
84 | ||
85 | impl_is_minus_one! { i8 i16 i32 i64 isize } | |
86 | ||
87 | pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> { | |
60c5eb7d | 88 | if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) } |
416331ca XL |
89 | } |
90 | ||
91 | pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T> | |
60c5eb7d XL |
92 | where |
93 | T: IsMinusOne, | |
94 | F: FnMut() -> T, | |
416331ca XL |
95 | { |
96 | loop { | |
97 | match cvt(f()) { | |
98 | Err(ref e) if e.kind() == ErrorKind::Interrupted => {} | |
99 | other => return other, | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | // On Unix-like platforms, libc::abort will unregister signal handlers | |
105 | // including the SIGABRT handler, preventing the abort from being blocked, and | |
106 | // fclose streams, with the side effect of flushing them so libc bufferred | |
107 | // output will be printed. Additionally the shell will generally print a more | |
108 | // understandable error message like "Abort trap" rather than "Illegal | |
109 | // instruction" that intrinsics::abort would cause, as intrinsics::abort is | |
110 | // implemented as an illegal instruction. | |
111 | pub unsafe fn abort_internal() -> ! { | |
112 | libc::abort() | |
113 | } |