diff options
| author | Michael Howell <michael@notriddle.com> | 2020-07-27 21:25:36 -0700 |
|---|---|---|
| committer | Michael Howell <michael@notriddle.com> | 2020-12-10 13:31:52 -0700 |
| commit | 59abdb6a7eef003b1a1b0711ceb9a1edb1d1b84c (patch) | |
| tree | 6b6a7ec3769332eae8d899a0987a7c2b7d9437f4 /library/std/src | |
| parent | d32c320d7eee56706486fef6be778495303afe9e (diff) | |
| download | rust-59abdb6a7eef003b1a1b0711ceb9a1edb1d1b84c.tar.gz rust-59abdb6a7eef003b1a1b0711ceb9a1edb1d1b84c.zip | |
Mark `-1` as an available niche for file descriptors
Based on discussion from https://internals.rust-lang.org/t/can-the-standard-library-shrink-option-file/12768, the file descriptor -1 is chosen based on the POSIX API designs that use it as a sentinel to report errors. A bigger niche could've been chosen, particularly on Linux, but would not necessarily be portable. This PR also adds a test case to ensure that the -1 niche (which is kind of hacky and has no obvious test case) works correctly. It requires the "upper" bound, which is actually -1, to be expressed in two's complement.
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/sys/unix/fd.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/library/std/src/sys/unix/fd.rs b/library/std/src/sys/unix/fd.rs index d3a279a2355..0eeaa68d55a 100644 --- a/library/std/src/sys/unix/fd.rs +++ b/library/std/src/sys/unix/fd.rs @@ -12,6 +12,11 @@ use crate::sys_common::AsInner; use libc::{c_int, c_void}; #[derive(Debug)] +#[rustc_layout_scalar_valid_range_start(0)] +// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a +// 32-bit c_int. Below is -2, in two's complement, but that only works out +// because c_int is 32 bits. +#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)] pub struct FileDesc { fd: c_int, } @@ -63,7 +68,8 @@ const fn max_iov() -> usize { impl FileDesc { pub fn new(fd: c_int) -> FileDesc { - FileDesc { fd } + assert_ne!(fd, -1); + unsafe { FileDesc { fd } } } pub fn raw(&self) -> c_int { |
