Rename OwnedFd's private field to match it's debug output.

This commit is contained in:
Dan Gohman 2021-07-27 17:08:27 -07:00
parent 45b5de3376
commit 0cb69dec57
4 changed files with 86 additions and 85 deletions

View File

@ -32,7 +32,7 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct BorrowedFd<'fd> {
raw: RawFd,
fd: RawFd,
_phantom: PhantomData<&'fd OwnedFd>,
}
@ -52,7 +52,7 @@ pub struct BorrowedFd<'fd> {
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct OwnedFd {
raw: RawFd,
fd: RawFd,
}
impl BorrowedFd<'_> {
@ -60,13 +60,13 @@ impl BorrowedFd<'_> {
///
/// # Safety
///
/// The resource pointed to by `raw` must remain open for the duration of
/// The resource pointed to by `fd` must remain open for the duration of
/// the returned `BorrowedFd`, and it must not have the value `-1`.
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub unsafe fn borrow_raw_fd(raw: RawFd) -> Self {
assert_ne!(raw, -1_i32 as RawFd);
Self { raw, _phantom: PhantomData }
pub unsafe fn borrow_raw_fd(fd: RawFd) -> Self {
assert_ne!(fd, -1_i32 as RawFd);
Self { fd, _phantom: PhantomData }
}
}
@ -74,7 +74,7 @@ impl BorrowedFd<'_> {
impl AsRawFd for BorrowedFd<'_> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.raw
self.fd
}
}
@ -82,7 +82,7 @@ impl AsRawFd for BorrowedFd<'_> {
impl AsRawFd for OwnedFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.raw
self.fd
}
}
@ -90,9 +90,9 @@ impl AsRawFd for OwnedFd {
impl IntoRawFd for OwnedFd {
#[inline]
fn into_raw_fd(self) -> RawFd {
let raw = self.raw;
let fd = self.fd;
forget(self);
raw
fd
}
}
@ -102,13 +102,13 @@ impl FromRawFd for OwnedFd {
///
/// # Safety
///
/// The resource pointed to by `raw` must be open and suitable for assuming
/// The resource pointed to by `fd` must be open and suitable for assuming
/// ownership. The resource must not require any cleanup other than `close`.
#[inline]
unsafe fn from_raw_fd(raw: RawFd) -> Self {
assert_ne!(raw, -1i32);
unsafe fn from_raw_fd(fd: RawFd) -> Self {
assert_ne!(fd, -1i32);
// 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)
Self { raw }
Self { fd }
}
}
@ -122,7 +122,7 @@ impl Drop for OwnedFd {
// the file descriptor was closed or not, and if we retried (for
// something like EINTR), we might close another valid file descriptor
// opened after we closed ours.
let _ = libc::close(self.raw as raw::c_int);
let _ = libc::close(self.fd as raw::c_int);
}
}
}
@ -130,14 +130,14 @@ impl Drop for OwnedFd {
#[unstable(feature = "io_safety", issue = "87074")]
impl fmt::Debug for BorrowedFd<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedFd").field("fd", &self.raw).finish()
f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
}
}
#[unstable(feature = "io_safety", issue = "87074")]
impl fmt::Debug for OwnedFd {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedFd").field("fd", &self.raw).finish()
f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
}
}

View File

@ -32,7 +32,7 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct BorrowedFd<'fd> {
raw: RawFd,
fd: RawFd,
_phantom: PhantomData<&'fd OwnedFd>,
}
@ -52,7 +52,7 @@ pub struct BorrowedFd<'fd> {
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct OwnedFd {
raw: RawFd,
fd: RawFd,
}
impl BorrowedFd<'_> {
@ -60,13 +60,13 @@ impl BorrowedFd<'_> {
///
/// # Safety
///
/// The resource pointed to by `raw` must remain open for the duration of
/// The resource pointed to by `fd` must remain open for the duration of
/// the returned `BorrowedFd`, and it must not have the value `-1`.
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub unsafe fn borrow_raw_fd(raw: RawFd) -> Self {
assert_ne!(raw, -1_i32 as RawFd);
unsafe { Self { raw, _phantom: PhantomData } }
pub unsafe fn borrow_raw_fd(fd: RawFd) -> Self {
assert_ne!(fd, -1_i32 as RawFd);
unsafe { Self { fd, _phantom: PhantomData } }
}
}
@ -74,7 +74,7 @@ impl BorrowedFd<'_> {
impl AsRawFd for BorrowedFd<'_> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.raw
self.fd
}
}
@ -82,7 +82,7 @@ impl AsRawFd for BorrowedFd<'_> {
impl AsRawFd for OwnedFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.raw
self.fd
}
}
@ -90,9 +90,9 @@ impl AsRawFd for OwnedFd {
impl IntoRawFd for OwnedFd {
#[inline]
fn into_raw_fd(self) -> RawFd {
let raw = self.raw;
let fd = self.fd;
forget(self);
raw
fd
}
}
@ -102,13 +102,13 @@ impl FromRawFd for OwnedFd {
///
/// # Safety
///
/// The resource pointed to by `raw` must be open and suitable for assuming
/// The resource pointed to by `fd` must be open and suitable for assuming
/// ownership.
#[inline]
unsafe fn from_raw_fd(raw: RawFd) -> Self {
assert_ne!(raw, RawFd::MAX);
unsafe fn from_raw_fd(fd: RawFd) -> Self {
assert_ne!(fd, RawFd::MAX);
// 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 { raw } }
unsafe { Self { fd } }
}
}
@ -122,7 +122,7 @@ impl Drop for OwnedFd {
// the file descriptor was closed or not, and if we retried (for
// something like EINTR), we might close another valid file descriptor
// opened after we closed ours.
let _ = libc::close(self.raw as raw::c_int);
let _ = libc::close(self.fd as raw::c_int);
}
}
}
@ -130,14 +130,14 @@ impl Drop for OwnedFd {
#[unstable(feature = "io_safety", issue = "87074")]
impl fmt::Debug for BorrowedFd<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedFd").field("fd", &self.raw).finish()
f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
}
}
#[unstable(feature = "io_safety", issue = "87074")]
impl fmt::Debug for OwnedFd {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedFd").field("fd", &self.raw).finish()
f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
}
}

View File

@ -30,7 +30,7 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
#[repr(transparent)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct BorrowedHandle<'handle> {
raw: NonNull<c_void>,
handle: NonNull<c_void>,
_phantom: PhantomData<&'handle OwnedHandle>,
}
@ -58,7 +58,7 @@ pub struct BorrowedHandle<'handle> {
#[repr(transparent)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct OwnedHandle {
raw: NonNull<c_void>,
handle: NonNull<c_void>,
}
/// Similar to `Option<OwnedHandle>`, but intended for use in FFI interfaces
@ -78,7 +78,7 @@ pub struct OwnedHandle {
#[repr(transparent)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct OptionFileHandle {
raw: RawHandle,
handle: RawHandle,
}
// The Windows [`HANDLE`] type may be transferred across and shared between
@ -98,13 +98,13 @@ impl BorrowedHandle<'_> {
///
/// # Safety
///
/// The resource pointed to by `raw` must remain open for the duration of
/// the returned `BorrowedHandle`, and it must not be null.
/// The resource pointed to by `handle` must remain open for the duration
/// of the returned `BorrowedHandle`, and it must not be null.
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub unsafe fn borrow_raw_handle(raw: RawHandle) -> Self {
assert!(!raw.is_null());
Self { raw: NonNull::new_unchecked(raw), _phantom: PhantomData }
pub unsafe fn borrow_raw_handle(handle: RawHandle) -> Self {
assert!(!handle.is_null());
Self { handle: NonNull::new_unchecked(handle), _phantom: PhantomData }
}
}
@ -113,7 +113,7 @@ impl OptionFileHandle {
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub const fn none() -> Self {
Self { raw: c::INVALID_HANDLE_VALUE }
Self { handle: c::INVALID_HANDLE_VALUE }
}
}
@ -122,19 +122,19 @@ impl TryFrom<OptionFileHandle> for OwnedHandle {
#[inline]
fn try_from(option: OptionFileHandle) -> Result<Self, ()> {
let raw = option.raw;
let handle = option.handle;
forget(option);
if let Some(non_null) = NonNull::new(raw) {
if let Some(non_null) = NonNull::new(handle) {
if non_null.as_ptr() != c::INVALID_HANDLE_VALUE {
Ok(Self { raw: non_null })
Ok(Self { handle: non_null })
} else {
Err(())
}
} else {
// In theory, we ought to be able to assume that the pointer here
// is never null, change `option.raw` to `NonNull`, and obviate the
// the panic path here. Unfortunately, Win32 documentation doesn't
// explicitly guarantee this anywhere.
// is never null, change `option.handle` to `NonNull`, and obviate
// the the panic path here. Unfortunately, Win32 documentation
// doesn't explicitly guarantee this anywhere.
//
// APIs like [`CreateFileW`] itself have `HANDLE` arguments where a
// null handle indicates an absent value, which wouldn't work if
@ -150,32 +150,32 @@ impl TryFrom<OptionFileHandle> for OwnedHandle {
impl From<OwnedHandle> for OptionFileHandle {
#[inline]
fn from(owned: OwnedHandle) -> Self {
let raw = owned.raw;
let handle = owned.handle;
forget(owned);
Self { raw: raw.as_ptr() }
Self { handle: handle.as_ptr() }
}
}
impl AsRawHandle for BorrowedHandle<'_> {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.raw.as_ptr()
self.handle.as_ptr()
}
}
impl AsRawHandle for OwnedHandle {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.raw.as_ptr()
self.handle.as_ptr()
}
}
impl IntoRawHandle for OwnedHandle {
#[inline]
fn into_raw_handle(self) -> RawHandle {
let raw = self.raw.as_ptr();
let handle = self.handle.as_ptr();
forget(self);
raw
handle
}
}
@ -184,7 +184,7 @@ impl FromRawHandle for OwnedHandle {
///
/// # Safety
///
/// The resource pointed to by `raw` must be open and suitable for
/// The resource pointed to by `handle` must be open and suitable for
/// assuming ownership. The resource must not require any cleanup other
/// than `CloseHandle`.
///
@ -193,9 +193,9 @@ impl FromRawHandle for OwnedHandle {
///
/// [`RegCloseKey`]: https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regclosekey
#[inline]
unsafe fn from_raw_handle(raw: RawHandle) -> Self {
assert!(!raw.is_null());
Self { raw: NonNull::new_unchecked(raw) }
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
assert!(!handle.is_null());
Self { handle: NonNull::new_unchecked(handle) }
}
}
@ -204,16 +204,16 @@ impl FromRawHandle for OptionFileHandle {
///
/// # Safety
///
/// The resource pointed to by `raw` must be either open and otherwise
/// The resource pointed to by `handle` must be either open and otherwise
/// unowned, or equal to `INVALID_HANDLE_VALUE``. Note that not all Windows
/// APIs use `INVALID_HANDLE_VALUE` for errors; see [here] for the full
/// story.
///
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
#[inline]
unsafe fn from_raw_handle(raw: RawHandle) -> Self {
assert!(!raw.is_null());
Self { raw }
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
assert!(!handle.is_null());
Self { handle }
}
}
@ -221,7 +221,7 @@ impl Drop for OwnedHandle {
#[inline]
fn drop(&mut self) {
unsafe {
let _ = c::CloseHandle(self.raw.as_ptr());
let _ = c::CloseHandle(self.handle.as_ptr());
}
}
}
@ -230,26 +230,26 @@ impl Drop for OptionFileHandle {
#[inline]
fn drop(&mut self) {
unsafe {
let _ = c::CloseHandle(self.raw);
let _ = c::CloseHandle(self.handle);
}
}
}
impl fmt::Debug for BorrowedHandle<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedHandle").field("handle", &self.raw).finish()
f.debug_struct("BorrowedHandle").field("handle", &self.handle).finish()
}
}
impl fmt::Debug for OwnedHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedHandle").field("handle", &self.raw).finish()
f.debug_struct("OwnedHandle").field("handle", &self.handle).finish()
}
}
impl fmt::Debug for OptionFileHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OptionFileHandle").field("handle", &self.raw).finish()
f.debug_struct("OptionFileHandle").field("handle", &self.handle).finish()
}
}

View File

@ -28,7 +28,7 @@ use crate::sys::c;
)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct BorrowedSocket<'socket> {
raw: RawSocket,
socket: RawSocket,
_phantom: PhantomData<&'socket OwnedSocket>,
}
@ -50,7 +50,7 @@ pub struct BorrowedSocket<'socket> {
)]
#[unstable(feature = "io_safety", issue = "87074")]
pub struct OwnedSocket {
raw: RawSocket,
socket: RawSocket,
}
impl BorrowedSocket<'_> {
@ -63,32 +63,32 @@ impl BorrowedSocket<'_> {
/// `INVALID_SOCKET`.
#[inline]
#[unstable(feature = "io_safety", issue = "87074")]
pub unsafe fn borrow_raw_socket(raw: RawSocket) -> Self {
debug_assert_ne!(raw, c::INVALID_SOCKET as RawSocket);
Self { raw, _phantom: PhantomData }
pub unsafe fn borrow_raw_socket(socket: RawSocket) -> Self {
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
Self { socket, _phantom: PhantomData }
}
}
impl AsRawSocket for BorrowedSocket<'_> {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
self.raw
self.socket
}
}
impl AsRawSocket for OwnedSocket {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
self.raw
self.socket
}
}
impl IntoRawSocket for OwnedSocket {
#[inline]
fn into_raw_socket(self) -> RawSocket {
let raw = self.raw;
let socket = self.socket;
forget(self);
raw
socket
}
}
@ -97,12 +97,13 @@ impl FromRawSocket for OwnedSocket {
///
/// # Safety
///
/// The resource pointed to by `raw` must be open and suitable for assuming
/// ownership. The resource must not require cleanup other than `closesocket`.
/// The resource pointed to by `socket` must be open and suitable for
/// assuming ownership. The resource must not require cleanup other than
/// `closesocket`.
#[inline]
unsafe fn from_raw_socket(raw: RawSocket) -> Self {
debug_assert_ne!(raw, c::INVALID_SOCKET as RawSocket);
Self { raw }
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
Self { socket }
}
}
@ -110,20 +111,20 @@ impl Drop for OwnedSocket {
#[inline]
fn drop(&mut self) {
unsafe {
let _ = c::closesocket(self.raw);
let _ = c::closesocket(self.socket);
}
}
}
impl fmt::Debug for BorrowedSocket<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedSocket").field("socket", &self.raw).finish()
f.debug_struct("BorrowedSocket").field("socket", &self.socket).finish()
}
}
impl fmt::Debug for OwnedSocket {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedSocket").field("socket", &self.raw).finish()
f.debug_struct("OwnedSocket").field("socket", &self.socket).finish()
}
}