]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | #![allow(missing_docs)] | |
12 | #![allow(non_camel_case_types)] | |
1a4d82fc JJ |
13 | |
14 | use prelude::v1::*; | |
15 | ||
85aaf69f | 16 | use io::{self, ErrorKind}; |
1a4d82fc | 17 | use libc; |
9346a6ac AL |
18 | use num::One; |
19 | use ops::Neg; | |
1a4d82fc | 20 | |
1a4d82fc JJ |
21 | pub mod backtrace; |
22 | pub mod c; | |
1a4d82fc | 23 | pub mod condvar; |
85aaf69f SL |
24 | pub mod ext; |
25 | pub mod fd; | |
9346a6ac | 26 | pub mod fs2; |
1a4d82fc | 27 | pub mod mutex; |
85aaf69f | 28 | pub mod net; |
1a4d82fc | 29 | pub mod os; |
85aaf69f | 30 | pub mod os_str; |
85aaf69f | 31 | pub mod pipe2; |
85aaf69f | 32 | pub mod process2; |
1a4d82fc JJ |
33 | pub mod rwlock; |
34 | pub mod stack_overflow; | |
35 | pub mod sync; | |
1a4d82fc JJ |
36 | pub mod thread; |
37 | pub mod thread_local; | |
85aaf69f | 38 | pub mod time; |
c34b1796 | 39 | pub mod stdio; |
1a4d82fc | 40 | |
85aaf69f SL |
41 | pub fn decode_error_kind(errno: i32) -> ErrorKind { |
42 | match errno as libc::c_int { | |
43 | libc::ECONNREFUSED => ErrorKind::ConnectionRefused, | |
44 | libc::ECONNRESET => ErrorKind::ConnectionReset, | |
45 | libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied, | |
46 | libc::EPIPE => ErrorKind::BrokenPipe, | |
47 | libc::ENOTCONN => ErrorKind::NotConnected, | |
48 | libc::ECONNABORTED => ErrorKind::ConnectionAborted, | |
c34b1796 AL |
49 | libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable, |
50 | libc::EADDRINUSE => ErrorKind::AddrInUse, | |
51 | libc::ENOENT => ErrorKind::NotFound, | |
85aaf69f SL |
52 | libc::EINTR => ErrorKind::Interrupted, |
53 | libc::EINVAL => ErrorKind::InvalidInput, | |
85aaf69f | 54 | libc::ETIMEDOUT => ErrorKind::TimedOut, |
c34b1796 | 55 | libc::consts::os::posix88::EEXIST => ErrorKind::AlreadyExists, |
85aaf69f SL |
56 | |
57 | // These two constants can have the same value on some systems, | |
58 | // but different values on others, so we can't use a match | |
59 | // clause | |
60 | x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => | |
c34b1796 | 61 | ErrorKind::WouldBlock, |
85aaf69f SL |
62 | |
63 | _ => ErrorKind::Other, | |
64 | } | |
65 | } | |
66 | ||
9346a6ac AL |
67 | pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> { |
68 | let one: T = T::one(); | |
85aaf69f SL |
69 | if t == -one { |
70 | Err(io::Error::last_os_error()) | |
71 | } else { | |
72 | Ok(t) | |
73 | } | |
74 | } | |
75 | ||
c34b1796 | 76 | #[allow(deprecated)] |
85aaf69f | 77 | pub fn cvt_r<T, F>(mut f: F) -> io::Result<T> |
9346a6ac | 78 | where T: One + PartialEq + Neg<Output=T>, F: FnMut() -> T |
85aaf69f SL |
79 | { |
80 | loop { | |
81 | match cvt(f()) { | |
82 | Err(ref e) if e.kind() == ErrorKind::Interrupted => {} | |
83 | other => return other, | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
1a4d82fc JJ |
88 | pub fn ms_to_timeval(ms: u64) -> libc::timeval { |
89 | libc::timeval { | |
90 | tv_sec: (ms / 1000) as libc::time_t, | |
91 | tv_usec: ((ms % 1000) * 1000) as libc::suseconds_t, | |
92 | } | |
93 | } |