2014-09-30 17:03:56 -07:00
|
|
|
// Copyright 2014 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 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2016-01-22 23:49:57 -08:00
|
|
|
#![allow(missing_docs, bad_style)]
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2015-02-23 10:59:17 -08:00
|
|
|
use ffi::{OsStr, OsString};
|
2015-02-02 21:39:14 -08:00
|
|
|
use io::{self, ErrorKind};
|
std: Stabilize portions of `std::os::$platform`
This commit starts to organize the `std::os::$platform` modules and in the
process stabilizes some of the functionality contained within. The organization
of these modules will reflect the organization of the standard library itself
with extension traits for primitives in the same corresponding module.
The OS-specific modules will grow more functionality over time including
concrete types that are not extending functionality of other structures, and
these will either go into the closest module in `std::os::$platform` or they
will grow a new module in the hierarchy.
The following items are now stable:
* `os::{unix, windows}`
* `unix::ffi`
* `unix::ffi::OsStrExt`
* `unix::ffi::OsStrExt::{from_bytes, as_bytes, to_cstring}`
* `unix::ffi::OsString`
* `unix::ffi::OsStringExt::{from_vec, into_vec}`
* `unix::process`
* `unix::process::CommandExt`
* `unix::process::CommandExt::{uid, gid}`
* `unix::process::ExitStatusExt`
* `unix::process::ExitStatusExt::signal`
* `unix::prelude`
* `windows::ffi`
* `windows::ffi::OsStringExt`
* `windows::ffi::OsStringExt::from_wide`
* `windows::ffi::OsStrExt`
* `windows::ffi::OsStrExt::encode_wide`
* `windows::prelude`
The following items remain unstable:
* `unix::io`
* `unix::io::{Fd, AsRawFd}`
* `unix::fs::{PermissionsExt, OpenOptionsExt}`
* `windows::io`
* `windows::io::{Handle, AsRawHandle}`
* `windows::io::{Socket, AsRawSocket}`
* `windows::fs`
* `windows::fs::OpenOptionsExt`
Due to the reorgnization of the platform extension modules, this commit is a
breaking change. Most imports can be fixed by adding the relevant libstd module
in the `use` path (such as `ffi` or `fs`).
[breaking-change]
2015-03-13 17:12:38 -07:00
|
|
|
use os::windows::ffi::{OsStrExt, OsStringExt};
|
2015-02-23 10:59:17 -08:00
|
|
|
use path::PathBuf;
|
2015-04-28 11:40:04 -07:00
|
|
|
use time::Duration;
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2015-06-26 09:30:35 -07:00
|
|
|
#[macro_use] pub mod compat;
|
|
|
|
|
|
2014-11-23 19:21:17 -08:00
|
|
|
pub mod backtrace;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub mod c;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod condvar;
|
2016-03-07 15:42:29 -08:00
|
|
|
pub mod dynamic_lib;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod ext;
|
2015-05-05 16:35:15 -07:00
|
|
|
pub mod fs;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod handle;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod mutex;
|
2015-02-05 16:50:11 -08:00
|
|
|
pub mod net;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub mod os;
|
2015-01-21 15:55:31 -08:00
|
|
|
pub mod os_str;
|
2016-09-21 19:11:39 +00:00
|
|
|
pub mod path;
|
2015-05-05 16:35:15 -07:00
|
|
|
pub mod pipe;
|
|
|
|
|
pub mod process;
|
2016-02-17 13:56:16 -08:00
|
|
|
pub mod rand;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod rwlock;
|
2014-11-23 19:21:17 -08:00
|
|
|
pub mod stack_overflow;
|
|
|
|
|
pub mod thread;
|
2014-11-14 14:20:57 -08:00
|
|
|
pub mod thread_local;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod time;
|
2015-02-24 23:27:20 -08:00
|
|
|
pub mod stdio;
|
2014-10-10 10:11:49 -07:00
|
|
|
|
2016-01-22 23:49:57 -08:00
|
|
|
#[cfg(not(test))]
|
2016-01-09 18:20:33 +00:00
|
|
|
pub fn init() {
|
2016-01-22 23:49:57 -08:00
|
|
|
::alloc::oom::set_oom_handler(oom_handler);
|
|
|
|
|
|
|
|
|
|
// See comment in sys/unix/mod.rs
|
|
|
|
|
fn oom_handler() -> ! {
|
|
|
|
|
use intrinsics;
|
|
|
|
|
use ptr;
|
|
|
|
|
let msg = "fatal runtime error: out of memory\n";
|
|
|
|
|
unsafe {
|
|
|
|
|
// WriteFile silently fails if it is passed an invalid handle, so
|
|
|
|
|
// there is no need to check the result of GetStdHandle.
|
|
|
|
|
c::WriteFile(c::GetStdHandle(c::STD_ERROR_HANDLE),
|
|
|
|
|
msg.as_ptr() as c::LPVOID,
|
|
|
|
|
msg.len() as c::DWORD,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
ptr::null_mut());
|
|
|
|
|
intrinsics::abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-09 18:20:33 +00:00
|
|
|
}
|
2015-09-08 15:53:46 -07:00
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
pub fn decode_error_kind(errno: i32) -> ErrorKind {
|
2015-11-02 16:23:22 -08:00
|
|
|
match errno as c::DWORD {
|
|
|
|
|
c::ERROR_ACCESS_DENIED => return ErrorKind::PermissionDenied,
|
|
|
|
|
c::ERROR_ALREADY_EXISTS => return ErrorKind::AlreadyExists,
|
2016-06-14 12:04:24 +03:00
|
|
|
c::ERROR_FILE_EXISTS => return ErrorKind::AlreadyExists,
|
2015-11-02 16:23:22 -08:00
|
|
|
c::ERROR_BROKEN_PIPE => return ErrorKind::BrokenPipe,
|
|
|
|
|
c::ERROR_FILE_NOT_FOUND => return ErrorKind::NotFound,
|
|
|
|
|
c::ERROR_PATH_NOT_FOUND => return ErrorKind::NotFound,
|
|
|
|
|
c::ERROR_NO_DATA => return ErrorKind::BrokenPipe,
|
|
|
|
|
c::ERROR_OPERATION_ABORTED => return ErrorKind::TimedOut,
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match errno {
|
|
|
|
|
c::WSAEACCES => ErrorKind::PermissionDenied,
|
|
|
|
|
c::WSAEADDRINUSE => ErrorKind::AddrInUse,
|
|
|
|
|
c::WSAEADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
|
|
|
|
|
c::WSAECONNABORTED => ErrorKind::ConnectionAborted,
|
|
|
|
|
c::WSAECONNREFUSED => ErrorKind::ConnectionRefused,
|
|
|
|
|
c::WSAECONNRESET => ErrorKind::ConnectionReset,
|
|
|
|
|
c::WSAEINVAL => ErrorKind::InvalidInput,
|
|
|
|
|
c::WSAENOTCONN => ErrorKind::NotConnected,
|
|
|
|
|
c::WSAEWOULDBLOCK => ErrorKind::WouldBlock,
|
|
|
|
|
c::WSAETIMEDOUT => ErrorKind::TimedOut,
|
2015-01-31 20:24:36 -08:00
|
|
|
|
|
|
|
|
_ => ErrorKind::Other,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-19 18:01:11 +00:00
|
|
|
pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> io::Result<Vec<u16>> {
|
|
|
|
|
fn inner(s: &OsStr) -> io::Result<Vec<u16>> {
|
|
|
|
|
let mut maybe_result: Vec<u16> = s.encode_wide().collect();
|
|
|
|
|
if maybe_result.iter().any(|&u| u == 0) {
|
|
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidInput,
|
|
|
|
|
"strings passed to WinAPI cannot contain NULs"));
|
|
|
|
|
}
|
|
|
|
|
maybe_result.push(0);
|
|
|
|
|
Ok(maybe_result)
|
|
|
|
|
}
|
|
|
|
|
inner(s.as_ref())
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-07 23:11:25 +01:00
|
|
|
// Many Windows APIs follow a pattern of where we hand a buffer and then they
|
|
|
|
|
// will report back to us how large the buffer should be or how many bytes
|
2015-01-27 12:20:58 -08:00
|
|
|
// currently reside in the buffer. This function is an abstraction over these
|
|
|
|
|
// functions by making them easier to call.
|
|
|
|
|
//
|
|
|
|
|
// The first callback, `f1`, is yielded a (pointer, len) pair which can be
|
|
|
|
|
// passed to a syscall. The `ptr` is valid for `len` items (u16 in this case).
|
|
|
|
|
// The closure is expected to return what the syscall returns which will be
|
|
|
|
|
// interpreted by this function to determine if the syscall needs to be invoked
|
|
|
|
|
// again (with more buffer space).
|
|
|
|
|
//
|
|
|
|
|
// Once the syscall has completed (errors bail out early) the second closure is
|
|
|
|
|
// yielded the data which has been read from the syscall. The return value
|
|
|
|
|
// from this closure is then the return value of the function.
|
2015-04-09 17:42:22 -07:00
|
|
|
fn fill_utf16_buf<F1, F2, T>(mut f1: F1, f2: F2) -> io::Result<T>
|
2015-11-02 16:23:22 -08:00
|
|
|
where F1: FnMut(*mut u16, c::DWORD) -> c::DWORD,
|
2015-01-27 12:20:58 -08:00
|
|
|
F2: FnOnce(&[u16]) -> T
|
|
|
|
|
{
|
|
|
|
|
// Start off with a stack buf but then spill over to the heap if we end up
|
|
|
|
|
// needing more space.
|
|
|
|
|
let mut stack_buf = [0u16; 512];
|
|
|
|
|
let mut heap_buf = Vec::new();
|
|
|
|
|
unsafe {
|
|
|
|
|
let mut n = stack_buf.len();
|
|
|
|
|
loop {
|
|
|
|
|
let buf = if n <= stack_buf.len() {
|
2015-02-18 14:48:57 -05:00
|
|
|
&mut stack_buf[..]
|
2015-01-27 12:20:58 -08:00
|
|
|
} else {
|
|
|
|
|
let extra = n - heap_buf.len();
|
|
|
|
|
heap_buf.reserve(extra);
|
|
|
|
|
heap_buf.set_len(n);
|
2015-02-18 14:48:57 -05:00
|
|
|
&mut heap_buf[..]
|
2015-01-27 12:20:58 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This function is typically called on windows API functions which
|
|
|
|
|
// will return the correct length of the string, but these functions
|
|
|
|
|
// also return the `0` on error. In some cases, however, the
|
|
|
|
|
// returned "correct length" may actually be 0!
|
|
|
|
|
//
|
|
|
|
|
// To handle this case we call `SetLastError` to reset it to 0 and
|
|
|
|
|
// then check it again if we get the "0 error value". If the "last
|
|
|
|
|
// error" is still 0 then we interpret it as a 0 length buffer and
|
|
|
|
|
// not an actual error.
|
|
|
|
|
c::SetLastError(0);
|
2015-11-02 16:23:22 -08:00
|
|
|
let k = match f1(buf.as_mut_ptr(), n as c::DWORD) {
|
|
|
|
|
0 if c::GetLastError() == 0 => 0,
|
2015-04-09 17:42:22 -07:00
|
|
|
0 => return Err(io::Error::last_os_error()),
|
2015-01-27 12:20:58 -08:00
|
|
|
n => n,
|
|
|
|
|
} as usize;
|
2015-11-02 16:23:22 -08:00
|
|
|
if k == n && c::GetLastError() == c::ERROR_INSUFFICIENT_BUFFER {
|
2015-01-27 12:20:58 -08:00
|
|
|
n *= 2;
|
|
|
|
|
} else if k >= n {
|
|
|
|
|
n = k;
|
|
|
|
|
} else {
|
|
|
|
|
return Ok(f2(&buf[..k]))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-23 10:59:17 -08:00
|
|
|
fn os2path(s: &[u16]) -> PathBuf {
|
2015-03-23 15:54:39 -07:00
|
|
|
PathBuf::from(OsString::from_wide(s))
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
|
|
|
|
|
match v.iter().position(|c| *c == 0) {
|
|
|
|
|
// don't include the 0
|
|
|
|
|
Some(i) => &v[..i],
|
|
|
|
|
None => v
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-02 21:39:14 -08:00
|
|
|
|
2016-06-28 08:56:56 -07:00
|
|
|
trait IsZero {
|
|
|
|
|
fn is_zero(&self) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_is_zero {
|
|
|
|
|
($($t:ident)*) => ($(impl IsZero for $t {
|
|
|
|
|
fn is_zero(&self) -> bool {
|
|
|
|
|
*self == 0
|
|
|
|
|
}
|
|
|
|
|
})*)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl_is_zero! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
|
|
|
|
|
|
|
|
|
|
fn cvt<I: IsZero>(i: I) -> io::Result<I> {
|
|
|
|
|
if i.is_zero() {
|
2015-02-02 21:39:14 -08:00
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
fn dur2timeout(dur: Duration) -> c::DWORD {
|
2015-04-28 11:40:04 -07:00
|
|
|
// Note that a duration is a (u64, u32) (seconds, nanoseconds) pair, and the
|
|
|
|
|
// timeouts in windows APIs are typically u32 milliseconds. To translate, we
|
|
|
|
|
// have two pieces to take care of:
|
|
|
|
|
//
|
|
|
|
|
// * Nanosecond precision is rounded up
|
|
|
|
|
// * Greater than u32::MAX milliseconds (50 days) is rounded up to INFINITE
|
|
|
|
|
// (never time out).
|
2015-07-05 23:20:00 -07:00
|
|
|
dur.as_secs().checked_mul(1000).and_then(|ms| {
|
|
|
|
|
ms.checked_add((dur.subsec_nanos() as u64) / 1_000_000)
|
2015-04-28 11:40:04 -07:00
|
|
|
}).and_then(|ms| {
|
2015-07-05 23:20:00 -07:00
|
|
|
ms.checked_add(if dur.subsec_nanos() % 1_000_000 > 0 {1} else {0})
|
2015-04-28 11:40:04 -07:00
|
|
|
}).map(|ms| {
|
2015-11-02 16:23:22 -08:00
|
|
|
if ms > <c::DWORD>::max_value() as u64 {
|
|
|
|
|
c::INFINITE
|
2015-04-28 11:40:04 -07:00
|
|
|
} else {
|
2015-11-02 16:23:22 -08:00
|
|
|
ms as c::DWORD
|
2015-04-28 11:40:04 -07:00
|
|
|
}
|
2015-11-02 16:23:22 -08:00
|
|
|
}).unwrap_or(c::INFINITE)
|
2015-04-28 11:40:04 -07:00
|
|
|
}
|