Update a bunch of library types for MCP807
This greatly reduces the number of places that actually use the `rustc_layout_scalar_valid_range_*` attributes down to just 3: ``` library/core\src\ptr\non_null.rs 68:#[rustc_layout_scalar_valid_range_start(1)] library/core\src\num\niche_types.rs 19: #[rustc_layout_scalar_valid_range_start($low)] 20: #[rustc_layout_scalar_valid_range_end($high)] ``` Everything else -- PAL Nanoseconds, alloc's `Cap`, niched FDs, etc -- all just wrap those `niche_types` types.
This commit is contained in:
parent
88ab2d8acb
commit
6f2a78345e
17 changed files with 345 additions and 214 deletions
|
@ -11,6 +11,8 @@ use crate::sys::cvt;
|
|||
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
||||
use crate::{fmt, fs, io};
|
||||
|
||||
type ValidRawFd = core::num::niche_types::NotAllOnes<RawFd>;
|
||||
|
||||
/// A borrowed file descriptor.
|
||||
///
|
||||
/// This has a lifetime parameter to tie it to the lifetime of something that owns the file
|
||||
|
@ -32,15 +34,10 @@ use crate::{fmt, fs, io};
|
|||
/// instead, but this is not supported on all platforms.
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(transparent)]
|
||||
#[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)]
|
||||
#[rustc_nonnull_optimization_guaranteed]
|
||||
#[stable(feature = "io_safety", since = "1.63.0")]
|
||||
pub struct BorrowedFd<'fd> {
|
||||
fd: RawFd,
|
||||
fd: ValidRawFd,
|
||||
_phantom: PhantomData<&'fd OwnedFd>,
|
||||
}
|
||||
|
||||
|
@ -56,15 +53,10 @@ pub struct BorrowedFd<'fd> {
|
|||
///
|
||||
/// You can use [`AsFd::as_fd`] to obtain a [`BorrowedFd`].
|
||||
#[repr(transparent)]
|
||||
#[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)]
|
||||
#[rustc_nonnull_optimization_guaranteed]
|
||||
#[stable(feature = "io_safety", since = "1.63.0")]
|
||||
pub struct OwnedFd {
|
||||
fd: RawFd,
|
||||
fd: ValidRawFd,
|
||||
}
|
||||
|
||||
impl BorrowedFd<'_> {
|
||||
|
@ -80,7 +72,8 @@ impl BorrowedFd<'_> {
|
|||
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
|
||||
assert!(fd != u32::MAX as RawFd);
|
||||
// SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
|
||||
unsafe { Self { fd, _phantom: PhantomData } }
|
||||
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
|
||||
Self { fd, _phantom: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,7 +123,7 @@ impl BorrowedFd<'_> {
|
|||
impl AsRawFd for BorrowedFd<'_> {
|
||||
#[inline]
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.fd
|
||||
self.fd.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,7 +131,7 @@ impl AsRawFd for BorrowedFd<'_> {
|
|||
impl AsRawFd for OwnedFd {
|
||||
#[inline]
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.fd
|
||||
self.fd.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,7 +139,7 @@ impl AsRawFd for OwnedFd {
|
|||
impl IntoRawFd for OwnedFd {
|
||||
#[inline]
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
ManuallyDrop::new(self).fd
|
||||
ManuallyDrop::new(self).fd.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,7 +157,8 @@ impl FromRawFd for OwnedFd {
|
|||
unsafe fn from_raw_fd(fd: RawFd) -> Self {
|
||||
assert_ne!(fd, u32::MAX as RawFd);
|
||||
// SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
|
||||
unsafe { Self { fd } }
|
||||
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
|
||||
Self { fd }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,12 +181,12 @@ impl Drop for OwnedFd {
|
|||
#[cfg(not(target_os = "hermit"))]
|
||||
{
|
||||
#[cfg(unix)]
|
||||
crate::sys::fs::debug_assert_fd_is_open(self.fd);
|
||||
crate::sys::fs::debug_assert_fd_is_open(self.fd.as_inner());
|
||||
|
||||
let _ = libc::close(self.fd);
|
||||
let _ = libc::close(self.fd.as_inner());
|
||||
}
|
||||
#[cfg(target_os = "hermit")]
|
||||
let _ = hermit_abi::close(self.fd);
|
||||
let _ = hermit_abi::close(self.fd.as_inner());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,6 +54,9 @@ use crate::{fmt, net, sys};
|
|||
/// Raw file descriptors.
|
||||
pub type RawFd = i32;
|
||||
|
||||
// The max of this is -2, in two's complement. -1 is `SOLID_NET_INVALID_FD`.
|
||||
type ValidRawFd = core::num::niche_types::NotAllOnes<RawFd>;
|
||||
|
||||
/// A borrowed SOLID Sockets file descriptor.
|
||||
///
|
||||
/// This has a lifetime parameter to tie it to the lifetime of something that
|
||||
|
@ -69,12 +72,9 @@ pub type RawFd = i32;
|
|||
/// socket, which is then borrowed under the same lifetime.
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(transparent)]
|
||||
#[rustc_layout_scalar_valid_range_start(0)]
|
||||
// This is -2, in two's complement. -1 is `SOLID_NET_INVALID_FD`.
|
||||
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
|
||||
#[rustc_nonnull_optimization_guaranteed]
|
||||
pub struct BorrowedFd<'socket> {
|
||||
fd: RawFd,
|
||||
fd: ValidRawFd,
|
||||
_phantom: PhantomData<&'socket OwnedFd>,
|
||||
}
|
||||
|
||||
|
@ -87,12 +87,9 @@ pub struct BorrowedFd<'socket> {
|
|||
/// an argument, it is not captured or consumed, and it never has the value
|
||||
/// `SOLID_NET_INVALID_FD`.
|
||||
#[repr(transparent)]
|
||||
#[rustc_layout_scalar_valid_range_start(0)]
|
||||
// This is -2, in two's complement. -1 is `SOLID_NET_INVALID_FD`.
|
||||
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
|
||||
#[rustc_nonnull_optimization_guaranteed]
|
||||
pub struct OwnedFd {
|
||||
fd: RawFd,
|
||||
fd: ValidRawFd,
|
||||
}
|
||||
|
||||
impl BorrowedFd<'_> {
|
||||
|
@ -108,7 +105,8 @@ impl BorrowedFd<'_> {
|
|||
assert!(fd != -1 as RawFd);
|
||||
// SAFETY: we just asserted that the value is in the valid range and
|
||||
// isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
|
||||
unsafe { Self { fd, _phantom: PhantomData } }
|
||||
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
|
||||
Self { fd, _phantom: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,21 +130,21 @@ impl BorrowedFd<'_> {
|
|||
impl AsRawFd for BorrowedFd<'_> {
|
||||
#[inline]
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.fd
|
||||
self.fd.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for OwnedFd {
|
||||
#[inline]
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.fd
|
||||
self.fd.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoRawFd for OwnedFd {
|
||||
#[inline]
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
ManuallyDrop::new(self).fd
|
||||
ManuallyDrop::new(self).fd.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -162,14 +160,15 @@ impl FromRawFd for OwnedFd {
|
|||
assert_ne!(fd, -1 as RawFd);
|
||||
// SAFETY: we just asserted that the value is in the valid range and
|
||||
// isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
|
||||
unsafe { Self { fd } }
|
||||
let fd = unsafe { ValidRawFd::new_unchecked(fd) };
|
||||
Self { fd }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OwnedFd {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe { sys::net::netc::close(self.fd) };
|
||||
unsafe { sys::net::netc::close(self.fd.as_inner()) };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,9 @@ use crate::mem::{self, ManuallyDrop};
|
|||
use crate::sys::cvt;
|
||||
use crate::{fmt, io, sys};
|
||||
|
||||
// The max here is -2, in two's complement. -1 is `INVALID_SOCKET`.
|
||||
type ValidRawSocket = core::num::niche_types::NotAllOnes<RawSocket>;
|
||||
|
||||
/// A borrowed socket.
|
||||
///
|
||||
/// This has a lifetime parameter to tie it to the lifetime of something that
|
||||
|
@ -24,17 +27,10 @@ use crate::{fmt, io, sys};
|
|||
/// socket, which is then borrowed under the same lifetime.
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(transparent)]
|
||||
#[rustc_layout_scalar_valid_range_start(0)]
|
||||
// This is -2, in two's complement. -1 is `INVALID_SOCKET`.
|
||||
#[cfg_attr(target_pointer_width = "32", rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE))]
|
||||
#[cfg_attr(
|
||||
target_pointer_width = "64",
|
||||
rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FF_FF_FF_FF_FE)
|
||||
)]
|
||||
#[rustc_nonnull_optimization_guaranteed]
|
||||
#[stable(feature = "io_safety", since = "1.63.0")]
|
||||
pub struct BorrowedSocket<'socket> {
|
||||
socket: RawSocket,
|
||||
socket: ValidRawSocket,
|
||||
_phantom: PhantomData<&'socket OwnedSocket>,
|
||||
}
|
||||
|
||||
|
@ -47,17 +43,10 @@ pub struct BorrowedSocket<'socket> {
|
|||
/// argument or returned as an owned value, and it never has the value
|
||||
/// `INVALID_SOCKET`.
|
||||
#[repr(transparent)]
|
||||
#[rustc_layout_scalar_valid_range_start(0)]
|
||||
// This is -2, in two's complement. -1 is `INVALID_SOCKET`.
|
||||
#[cfg_attr(target_pointer_width = "32", rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE))]
|
||||
#[cfg_attr(
|
||||
target_pointer_width = "64",
|
||||
rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FF_FF_FF_FF_FE)
|
||||
)]
|
||||
#[rustc_nonnull_optimization_guaranteed]
|
||||
#[stable(feature = "io_safety", since = "1.63.0")]
|
||||
pub struct OwnedSocket {
|
||||
socket: RawSocket,
|
||||
socket: ValidRawSocket,
|
||||
}
|
||||
|
||||
impl BorrowedSocket<'_> {
|
||||
|
@ -73,7 +62,8 @@ impl BorrowedSocket<'_> {
|
|||
#[stable(feature = "io_safety", since = "1.63.0")]
|
||||
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
|
||||
assert!(socket != sys::c::INVALID_SOCKET as RawSocket);
|
||||
unsafe { Self { socket, _phantom: PhantomData } }
|
||||
let socket = unsafe { ValidRawSocket::new_unchecked(socket) };
|
||||
Self { socket, _phantom: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,7 +162,7 @@ fn last_error() -> io::Error {
|
|||
impl AsRawSocket for BorrowedSocket<'_> {
|
||||
#[inline]
|
||||
fn as_raw_socket(&self) -> RawSocket {
|
||||
self.socket
|
||||
self.socket.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,7 +170,7 @@ impl AsRawSocket for BorrowedSocket<'_> {
|
|||
impl AsRawSocket for OwnedSocket {
|
||||
#[inline]
|
||||
fn as_raw_socket(&self) -> RawSocket {
|
||||
self.socket
|
||||
self.socket.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,7 +178,7 @@ impl AsRawSocket for OwnedSocket {
|
|||
impl IntoRawSocket for OwnedSocket {
|
||||
#[inline]
|
||||
fn into_raw_socket(self) -> RawSocket {
|
||||
ManuallyDrop::new(self).socket
|
||||
ManuallyDrop::new(self).socket.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,10 +186,9 @@ impl IntoRawSocket for OwnedSocket {
|
|||
impl FromRawSocket for OwnedSocket {
|
||||
#[inline]
|
||||
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
|
||||
unsafe {
|
||||
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
|
||||
Self { socket }
|
||||
}
|
||||
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
|
||||
let socket = unsafe { ValidRawSocket::new_unchecked(socket) };
|
||||
Self { socket }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +197,7 @@ impl Drop for OwnedSocket {
|
|||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
let _ = sys::c::closesocket(self.socket as sys::c::SOCKET);
|
||||
let _ = sys::c::closesocket(self.socket.as_inner() as sys::c::SOCKET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue