// Copyright 2017 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! System bindings for the Nintendo 3DS use io::{self, ErrorKind}; use libc; pub use self::rand::hashmap_random_keys; pub use libc::strlen; pub mod args; #[cfg(feature = "backtrace")] pub mod backtrace; pub mod cmath; pub mod condvar; pub mod env; pub mod ext; pub mod fast_thread_local; pub mod fd; pub mod fs; pub mod memchr; pub mod mutex; pub mod net; pub mod os; pub mod os_str; pub mod path; pub mod pipe; pub mod process; pub mod rand; pub mod rwlock; pub mod stack_overflow; pub mod thread; pub mod thread_local; pub mod time; pub mod stdio; #[cfg(not(test))] pub fn init() { } pub fn unsupported() -> io::Result { Err(unsupported_err()) } pub fn unsupported_err() -> io::Error { io::Error::new(io::ErrorKind::Other, "operation not supported on 3DS yet") } pub fn decode_error_kind(errno: i32) -> ErrorKind { match errno as libc::c_int { libc::ECONNREFUSED => ErrorKind::ConnectionRefused, libc::ECONNRESET => ErrorKind::ConnectionReset, libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied, libc::EPIPE => ErrorKind::BrokenPipe, libc::ENOTCONN => ErrorKind::NotConnected, libc::ECONNABORTED => ErrorKind::ConnectionAborted, libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable, libc::EADDRINUSE => ErrorKind::AddrInUse, libc::ENOENT => ErrorKind::NotFound, libc::EINTR => ErrorKind::Interrupted, libc::EINVAL => ErrorKind::InvalidInput, libc::ETIMEDOUT => ErrorKind::TimedOut, libc::EEXIST => ErrorKind::AlreadyExists, // These two constants can have the same value on some systems, // but different values on others, so we can't use a match // clause x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock, _ => ErrorKind::Other, } } // This enum is used as the storage for a bunch of types which can't actually // exist. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] pub enum Void {} #[doc(hidden)] pub trait IsMinusOne { fn is_minus_one(&self) -> bool; } macro_rules! impl_is_minus_one { ($($t:ident)*) => ($(impl IsMinusOne for $t { fn is_minus_one(&self) -> bool { *self == -1 } })*) } impl_is_minus_one! { i8 i16 i32 i64 isize } pub fn cvt(t: T) -> io::Result { if t.is_minus_one() { Err(io::Error::last_os_error()) } else { Ok(t) } } pub fn cvt_r(mut f: F) -> io::Result where T: IsMinusOne, F: FnMut() -> T { loop { match cvt(f()) { Err(ref e) if e.kind() == ErrorKind::Interrupted => {} other => return other, } } } // On Unix-like platforms, libc::abort will unregister signal handlers // including the SIGABRT handler, preventing the abort from being blocked, and // fclose streams, with the side effect of flushing them so libc bufferred // output will be printed. Additionally the shell will generally print a more // understandable error message like "Abort trap" rather than "Illegal // instruction" that intrinsics::abort would cause, as intrinsics::abort is // implemented as an illegal instruction. pub unsafe fn abort_internal() -> ! { ::libc::abort() }