1 // Copyright 2016 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.
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.
11 #![allow(dead_code, missing_docs, nonstandard_style)]
13 use io::{self, ErrorKind};
16 pub use self::rand::hashmap_random_keys;
18 #[path = "../unix/alloc.rs"]
21 #[cfg(feature = "backtrace")]
27 pub mod fast_thread_local;
40 pub mod stack_overflow;
50 pub fn decode_error_kind(errno: i32) -> ErrorKind {
52 syscall::ECONNREFUSED => ErrorKind::ConnectionRefused,
53 syscall::ECONNRESET => ErrorKind::ConnectionReset,
54 syscall::EPERM | syscall::EACCES => ErrorKind::PermissionDenied,
55 syscall::EPIPE => ErrorKind::BrokenPipe,
56 syscall::ENOTCONN => ErrorKind::NotConnected,
57 syscall::ECONNABORTED => ErrorKind::ConnectionAborted,
58 syscall::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
59 syscall::EADDRINUSE => ErrorKind::AddrInUse,
60 syscall::ENOENT => ErrorKind::NotFound,
61 syscall::EINTR => ErrorKind::Interrupted,
62 syscall::EINVAL => ErrorKind::InvalidInput,
63 syscall::ETIMEDOUT => ErrorKind::TimedOut,
64 syscall::EEXIST => ErrorKind::AlreadyExists,
66 // These two constants can have the same value on some systems,
67 // but different values on others, so we can't use a match
69 x if x == syscall::EAGAIN || x == syscall::EWOULDBLOCK =>
70 ErrorKind::WouldBlock,
72 _ => ErrorKind::Other,
76 pub fn cvt(result: Result<usize, syscall::Error>) -> io::Result<usize> {
77 result.map_err(|err| io::Error::from_raw_os_error(err.errno))
81 pub trait IsMinusOne {
82 fn is_minus_one(&self) -> bool;
85 macro_rules! impl_is_minus_one {
86 ($($t:ident)*) => ($(impl IsMinusOne for $t {
87 fn is_minus_one(&self) -> bool {
93 impl_is_minus_one! { i8 i16 i32 i64 isize }
95 pub fn cvt_libc<T: IsMinusOne>(t: T) -> io::Result<T> {
97 Err(io::Error::last_os_error())
103 /// On Redox, use an illegal instruction to abort
104 pub unsafe fn abort_internal() -> ! {
105 ::core::intrinsics::abort();