]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/unix/mod.rs
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / libstd / sys / unix / mod.rs
CommitLineData
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
14use prelude::v1::*;
15
85aaf69f 16use io::{self, ErrorKind};
1a4d82fc 17use libc;
9346a6ac
AL
18use num::One;
19use ops::Neg;
1a4d82fc 20
1a4d82fc
JJ
21pub mod backtrace;
22pub mod c;
1a4d82fc 23pub mod condvar;
85aaf69f
SL
24pub mod ext;
25pub mod fd;
9346a6ac 26pub mod fs2;
1a4d82fc 27pub mod mutex;
85aaf69f 28pub mod net;
1a4d82fc 29pub mod os;
85aaf69f 30pub mod os_str;
85aaf69f 31pub mod pipe2;
85aaf69f 32pub mod process2;
1a4d82fc
JJ
33pub mod rwlock;
34pub mod stack_overflow;
35pub mod sync;
1a4d82fc
JJ
36pub mod thread;
37pub mod thread_local;
85aaf69f 38pub mod time;
c34b1796 39pub mod stdio;
1a4d82fc 40
85aaf69f
SL
41pub 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
67pub 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 77pub 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
88pub 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}