diff options
Diffstat (limited to 'vendor/io-lifetimes/src')
-rw-r--r-- | vendor/io-lifetimes/src/example_ffi.rs | 75 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_async_std.rs | 525 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_fs_err.rs | 62 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_mio.rs | 478 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_os_pipe.rs | 179 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_socket2.rs | 97 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_std.rs | 888 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_std_views.rs | 31 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/impls_tokio.rs | 214 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/lib.rs | 179 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/portability.rs | 518 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/raw.rs | 253 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/traits.rs | 274 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/types.rs | 894 | ||||
-rw-r--r-- | vendor/io-lifetimes/src/views.rs | 204 |
15 files changed, 4871 insertions, 0 deletions
diff --git a/vendor/io-lifetimes/src/example_ffi.rs b/vendor/io-lifetimes/src/example_ffi.rs new file mode 100644 index 000000000..8f7c238ba --- /dev/null +++ b/vendor/io-lifetimes/src/example_ffi.rs @@ -0,0 +1,75 @@ +//! This is just a sample of what FFI using this crate can look like. + +#![cfg_attr(not(rustc_attrs), allow(unused_imports))] +#![allow(missing_docs)] + +#[cfg(any(unix, target_os = "wasi"))] +use crate::{BorrowedFd, OwnedFd}; +#[cfg(windows)] +use crate::{BorrowedHandle, HandleOrInvalid}; + +#[cfg(any(unix, target_os = "wasi"))] +use libc::{c_char, c_int, c_void, size_t, ssize_t}; +#[cfg(windows)] +use { + core::ffi::c_void, + windows_sys::core::PCWSTR, + windows_sys::Win32::Foundation::BOOL, + windows_sys::Win32::Security::SECURITY_ATTRIBUTES, + windows_sys::Win32::Storage::FileSystem::{ + FILE_ACCESS_FLAGS, FILE_CREATION_DISPOSITION, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE, + }, + windows_sys::Win32::System::IO::OVERLAPPED, +}; + +// Declare a few FFI functions ourselves, to show off the FFI ergonomics. +#[cfg(all(rustc_attrs, any(unix, target_os = "wasi")))] +extern "C" { + pub fn open(pathname: *const c_char, flags: c_int, ...) -> Option<OwnedFd>; +} +#[cfg(any(unix, target_os = "wasi"))] +extern "C" { + pub fn read(fd: BorrowedFd<'_>, ptr: *mut c_void, size: size_t) -> ssize_t; + pub fn write(fd: BorrowedFd<'_>, ptr: *const c_void, size: size_t) -> ssize_t; +} +#[cfg(any(unix, target_os = "wasi"))] +pub use libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY}; + +// The Windows analogs of the above. Note the use of [`HandleOrInvalid`] as +// the return type for `CreateFileW`, since that function is defined to return +// [`INVALID_HANDLE_VALUE`] on error instead of null. +#[cfg(windows)] +extern "system" { + pub fn CreateFileW( + lpfilename: PCWSTR, + dwdesiredaccess: FILE_ACCESS_FLAGS, + dwsharemode: FILE_SHARE_MODE, + lpsecurityattributes: *const SECURITY_ATTRIBUTES, + dwcreationdisposition: FILE_CREATION_DISPOSITION, + dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES, + htemplatefile: HANDLE, + ) -> HandleOrInvalid; + pub fn ReadFile( + hfile: BorrowedHandle<'_>, + lpbuffer: *mut c_void, + nnumberofbytestoread: u32, + lpnumberofbytesread: *mut u32, + lpoverlapped: *mut OVERLAPPED, + ) -> BOOL; + pub fn WriteFile( + hfile: BorrowedHandle<'_>, + lpbuffer: *const c_void, + nnumberofbytestowrite: u32, + lpnumberofbyteswritten: *mut u32, + lpoverlapped: *mut OVERLAPPED, + ) -> BOOL; +} + +#[cfg(windows)] +pub use { + windows_sys::Win32::Foundation::HANDLE, + windows_sys::Win32::Storage::FileSystem::{ + CREATE_ALWAYS, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, FILE_GENERIC_WRITE, + OPEN_EXISTING, + }, +}; diff --git a/vendor/io-lifetimes/src/impls_async_std.rs b/vendor/io-lifetimes/src/impls_async_std.rs new file mode 100644 index 000000000..534700486 --- /dev/null +++ b/vendor/io-lifetimes/src/impls_async_std.rs @@ -0,0 +1,525 @@ +//! Implementations of io-lifetimes' traits for async-std's types. In the +//! future, we'll prefer to have crates provide their own impls; this is +//! just a temporary measure. + +use crate::views::{FilelikeViewType, SocketlikeViewType}; +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd}; +#[cfg(windows)] +use crate::{ + AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, FromHandle, FromSocket, IntoHandle, + IntoSocket, OwnedHandle, OwnedSocket, +}; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(windows)] +use std::os::windows::io::{ + AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket, +}; + +unsafe impl FilelikeViewType for async_std::fs::File {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for async_std::fs::File { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for async_std::fs::File { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for async_std::fs::File { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<async_std::fs::File> for OwnedFd { + #[inline] + fn from(owned: async_std::fs::File) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for async_std::fs::File { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<async_std::fs::File> for OwnedHandle { + #[inline] + fn from(owned: async_std::fs::File) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for async_std::fs::File { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for async_std::fs::File { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromHandle for async_std::fs::File { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<OwnedHandle> for async_std::fs::File { + #[inline] + fn from(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +unsafe impl SocketlikeViewType for async_std::net::TcpStream {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for async_std::net::TcpStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for async_std::net::TcpStream { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for async_std::net::TcpStream { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<async_std::net::TcpStream> for OwnedFd { + #[inline] + fn from(owned: async_std::net::TcpStream) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for async_std::net::TcpStream { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<async_std::net::TcpStream> for OwnedSocket { + #[inline] + fn from(owned: async_std::net::TcpStream) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for async_std::net::TcpStream { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for async_std::net::TcpStream { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for async_std::net::TcpStream { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for async_std::net::TcpStream { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +unsafe impl SocketlikeViewType for async_std::net::TcpListener {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for async_std::net::TcpListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for async_std::net::TcpListener { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for async_std::net::TcpListener { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<async_std::net::TcpListener> for OwnedFd { + #[inline] + fn from(owned: async_std::net::TcpListener) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for async_std::net::TcpListener { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<async_std::net::TcpListener> for OwnedSocket { + #[inline] + fn from(owned: async_std::net::TcpListener) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for async_std::net::TcpListener { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for async_std::net::TcpListener { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for async_std::net::TcpListener { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for async_std::net::TcpListener { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +unsafe impl SocketlikeViewType for async_std::net::UdpSocket {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for async_std::net::UdpSocket { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for async_std::net::UdpSocket { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for async_std::net::UdpSocket { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<async_std::net::UdpSocket> for OwnedFd { + #[inline] + fn from(owned: async_std::net::UdpSocket) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for async_std::net::UdpSocket { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<async_std::net::UdpSocket> for OwnedSocket { + #[inline] + fn from(owned: async_std::net::UdpSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for async_std::net::UdpSocket { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for async_std::net::UdpSocket { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for async_std::net::UdpSocket { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for async_std::net::UdpSocket { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for async_std::io::Stdin { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for async_std::io::Stdin { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for async_std::io::Stdout { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for async_std::io::Stdout { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for async_std::io::Stderr { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for async_std::io::Stderr { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for async_std::os::unix::net::UnixStream {} + +#[cfg(unix)] +impl AsFd for async_std::os::unix::net::UnixStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for async_std::os::unix::net::UnixStream { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<async_std::os::unix::net::UnixStream> for OwnedFd { + #[inline] + fn from(owned: async_std::os::unix::net::UnixStream) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for async_std::os::unix::net::UnixStream { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for async_std::os::unix::net::UnixStream { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for async_std::os::unix::net::UnixListener {} + +#[cfg(unix)] +impl AsFd for async_std::os::unix::net::UnixListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for async_std::os::unix::net::UnixListener { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<async_std::os::unix::net::UnixListener> for OwnedFd { + #[inline] + fn from(owned: async_std::os::unix::net::UnixListener) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for async_std::os::unix::net::UnixListener { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for async_std::os::unix::net::UnixListener { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for async_std::os::unix::net::UnixDatagram {} + +#[cfg(unix)] +impl AsFd for async_std::os::unix::net::UnixDatagram { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for async_std::os::unix::net::UnixDatagram { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<async_std::os::unix::net::UnixDatagram> for OwnedFd { + #[inline] + fn from(owned: async_std::os::unix::net::UnixDatagram) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for async_std::os::unix::net::UnixDatagram { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for async_std::os::unix::net::UnixDatagram { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} diff --git a/vendor/io-lifetimes/src/impls_fs_err.rs b/vendor/io-lifetimes/src/impls_fs_err.rs new file mode 100644 index 000000000..6fcef362b --- /dev/null +++ b/vendor/io-lifetimes/src/impls_fs_err.rs @@ -0,0 +1,62 @@ +//! Implementations of io-lifetimes' traits for fs_err's types. In the +//! future, we'll prefer to have crates provide their own impls; this is +//! just a temporary measure. + +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, BorrowedFd, IntoFd, OwnedFd}; +#[cfg(windows)] +use crate::{AsHandle, BorrowedHandle, IntoHandle, OwnedHandle}; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(windows)] +use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle}; + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for fs_err::File { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for fs_err::File { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for fs_err::File { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<fs_err::File> for OwnedFd { + #[inline] + fn from(owned: fs_err::File) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for fs_err::File { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<fs_err::File> for OwnedHandle { + #[inline] + fn from(owned: fs_err::File) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} diff --git a/vendor/io-lifetimes/src/impls_mio.rs b/vendor/io-lifetimes/src/impls_mio.rs new file mode 100644 index 000000000..246f3b93f --- /dev/null +++ b/vendor/io-lifetimes/src/impls_mio.rs @@ -0,0 +1,478 @@ +//! Implementations of io-lifetimes' traits for mio's types. In the +//! future, we'll prefer to have crates provide their own impls; this is +//! just a temporary measure. + +#[cfg(unix)] +use crate::views::FilelikeViewType; +use crate::views::SocketlikeViewType; +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd}; +#[cfg(windows)] +use crate::{AsSocket, BorrowedSocket, FromSocket, IntoSocket, OwnedSocket}; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(windows)] +use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket}; + +unsafe impl SocketlikeViewType for mio::net::TcpStream {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for mio::net::TcpStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for mio::net::TcpStream { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for mio::net::TcpStream { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<mio::net::TcpStream> for OwnedFd { + #[inline] + fn from(owned: mio::net::TcpStream) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for mio::net::TcpStream { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<mio::net::TcpStream> for OwnedSocket { + #[inline] + fn from(owned: mio::net::TcpStream) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for mio::net::TcpStream { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for mio::net::TcpStream { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for mio::net::TcpStream { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for mio::net::TcpStream { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +unsafe impl SocketlikeViewType for mio::net::TcpListener {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for mio::net::TcpListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for mio::net::TcpListener { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for mio::net::TcpListener { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<mio::net::TcpListener> for OwnedFd { + #[inline] + fn from(owned: mio::net::TcpListener) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for mio::net::TcpListener { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<mio::net::TcpListener> for OwnedSocket { + #[inline] + fn from(owned: mio::net::TcpListener) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for mio::net::TcpListener { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for mio::net::TcpListener { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for mio::net::TcpListener { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for mio::net::TcpListener { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +unsafe impl SocketlikeViewType for mio::net::UdpSocket {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for mio::net::UdpSocket { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for mio::net::UdpSocket { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for mio::net::UdpSocket { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<mio::net::UdpSocket> for OwnedFd { + #[inline] + fn from(owned: mio::net::UdpSocket) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for mio::net::UdpSocket { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<mio::net::UdpSocket> for OwnedSocket { + #[inline] + fn from(owned: mio::net::UdpSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for mio::net::UdpSocket { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for mio::net::UdpSocket { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for mio::net::UdpSocket { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for mio::net::UdpSocket { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for mio::net::UnixDatagram {} + +#[cfg(unix)] +impl AsFd for mio::net::UnixDatagram { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for mio::net::UnixDatagram { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<mio::net::UnixDatagram> for OwnedFd { + #[inline] + fn from(owned: mio::net::UnixDatagram) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for mio::net::UnixDatagram { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for mio::net::UnixDatagram { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for mio::net::UnixListener {} + +#[cfg(unix)] +impl AsFd for mio::net::UnixListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for mio::net::UnixListener { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<mio::net::UnixListener> for OwnedFd { + #[inline] + fn from(owned: mio::net::UnixListener) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for mio::net::UnixListener { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for mio::net::UnixListener { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for mio::net::UnixStream {} + +#[cfg(unix)] +impl AsFd for mio::net::UnixStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for mio::net::UnixStream { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<mio::net::UnixStream> for OwnedFd { + #[inline] + fn from(owned: mio::net::UnixStream) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for mio::net::UnixStream { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for mio::net::UnixStream { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +unsafe impl FilelikeViewType for mio::unix::pipe::Receiver {} + +#[cfg(unix)] +impl AsFd for mio::unix::pipe::Receiver { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for mio::unix::pipe::Receiver { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<mio::unix::pipe::Receiver> for OwnedFd { + #[inline] + fn from(owned: mio::unix::pipe::Receiver) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for mio::unix::pipe::Receiver { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for mio::unix::pipe::Receiver { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +unsafe impl FilelikeViewType for mio::unix::pipe::Sender {} + +#[cfg(unix)] +impl AsFd for mio::unix::pipe::Sender { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for mio::unix::pipe::Sender { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<mio::unix::pipe::Sender> for OwnedFd { + #[inline] + fn from(owned: mio::unix::pipe::Sender) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for mio::unix::pipe::Sender { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for mio::unix::pipe::Sender { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} diff --git a/vendor/io-lifetimes/src/impls_os_pipe.rs b/vendor/io-lifetimes/src/impls_os_pipe.rs new file mode 100644 index 000000000..e0544873e --- /dev/null +++ b/vendor/io-lifetimes/src/impls_os_pipe.rs @@ -0,0 +1,179 @@ +//! Implementations of io-lifetimes' traits for os_pipe's types. In the +//! future, we'll prefer to have crates provide their own impls; this is +//! just a temporary measure. + +use crate::views::FilelikeViewType; +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd}; +#[cfg(windows)] +use crate::{AsHandle, BorrowedHandle, FromHandle, IntoHandle, OwnedHandle}; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(windows)] +use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle}; + +unsafe impl FilelikeViewType for os_pipe::PipeReader {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for os_pipe::PipeReader { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for os_pipe::PipeReader { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for os_pipe::PipeReader { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<os_pipe::PipeReader> for OwnedFd { + #[inline] + fn from(owned: os_pipe::PipeReader) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for os_pipe::PipeReader { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<os_pipe::PipeReader> for OwnedHandle { + #[inline] + fn from(owned: os_pipe::PipeReader) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for os_pipe::PipeReader { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for os_pipe::PipeReader { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromHandle for os_pipe::PipeReader { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<OwnedHandle> for os_pipe::PipeReader { + #[inline] + fn from(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +unsafe impl FilelikeViewType for os_pipe::PipeWriter {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for os_pipe::PipeWriter { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for os_pipe::PipeWriter { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for os_pipe::PipeWriter { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<os_pipe::PipeWriter> for OwnedFd { + #[inline] + fn from(owned: os_pipe::PipeWriter) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for os_pipe::PipeWriter { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<os_pipe::PipeWriter> for OwnedHandle { + #[inline] + fn from(owned: os_pipe::PipeWriter) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for os_pipe::PipeWriter { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for os_pipe::PipeWriter { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromHandle for os_pipe::PipeWriter { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<OwnedHandle> for os_pipe::PipeWriter { + #[inline] + fn from(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} diff --git a/vendor/io-lifetimes/src/impls_socket2.rs b/vendor/io-lifetimes/src/impls_socket2.rs new file mode 100644 index 000000000..fbfeb67d7 --- /dev/null +++ b/vendor/io-lifetimes/src/impls_socket2.rs @@ -0,0 +1,97 @@ +//! Implementations of io-lifetimes' traits for socket2's types. In the +//! future, we'll prefer to have crates provide their own impls; this is +//! just a temporary measure. + +use crate::views::SocketlikeViewType; +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd}; +#[cfg(windows)] +use crate::{AsSocket, BorrowedSocket, FromSocket, IntoSocket, OwnedSocket}; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(windows)] +use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket}; + +unsafe impl SocketlikeViewType for socket2::Socket {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for socket2::Socket { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for socket2::Socket { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for socket2::Socket { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<socket2::Socket> for OwnedFd { + #[inline] + fn from(owned: socket2::Socket) -> Self { + unsafe { OwnedFd::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for socket2::Socket { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<socket2::Socket> for OwnedSocket { + #[inline] + fn from(owned: socket2::Socket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for socket2::Socket { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for socket2::Socket { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for socket2::Socket { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for socket2::Socket { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} diff --git a/vendor/io-lifetimes/src/impls_std.rs b/vendor/io-lifetimes/src/impls_std.rs new file mode 100644 index 000000000..27275fa68 --- /dev/null +++ b/vendor/io-lifetimes/src/impls_std.rs @@ -0,0 +1,888 @@ +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, FromFd, IntoFd}; +#[cfg(windows)] +use crate::{AsHandle, AsSocket, FromHandle, FromSocket, IntoHandle, IntoSocket}; +#[cfg(any(unix, target_os = "wasi"))] +use crate::{BorrowedFd, OwnedFd}; +#[cfg(windows)] +use crate::{BorrowedHandle, BorrowedSocket, HandleOrInvalid, OwnedHandle, OwnedSocket}; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(windows)] +use std::os::windows::io::{ + AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket, +}; + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for BorrowedFd<'_> { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for BorrowedHandle<'_> { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(windows)] +impl AsSocket for BorrowedSocket<'_> { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for OwnedFd { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for OwnedHandle { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(windows)] +impl AsSocket for OwnedSocket { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for OwnedFd { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { Self::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for OwnedHandle { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { Self::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl IntoSocket for OwnedSocket { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { Self::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for OwnedFd { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromHandle for OwnedHandle { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl FromSocket for OwnedSocket { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl FromHandle for HandleOrInvalid { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<OwnedHandle> for HandleOrInvalid { + #[inline] + fn from(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for std::fs::File { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::fs::File { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for std::fs::File { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<std::fs::File> for OwnedFd { + #[inline] + fn from(owned: std::fs::File) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for std::fs::File { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<std::fs::File> for OwnedHandle { + #[inline] + fn from(owned: std::fs::File) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for std::fs::File { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for std::fs::File { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromHandle for std::fs::File { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<OwnedHandle> for std::fs::File { + #[inline] + fn from(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for std::net::TcpStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for std::net::TcpStream { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for std::net::TcpStream { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<std::net::TcpStream> for OwnedFd { + #[inline] + fn from(owned: std::net::TcpStream) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for std::net::TcpStream { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<std::net::TcpStream> for OwnedSocket { + #[inline] + fn from(owned: std::net::TcpStream) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for std::net::TcpStream { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for std::net::TcpStream { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for std::net::TcpStream { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for std::net::TcpStream { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for std::net::TcpListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for std::net::TcpListener { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for std::net::TcpListener { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<std::net::TcpListener> for OwnedFd { + #[inline] + fn from(owned: std::net::TcpListener) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for std::net::TcpListener { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<std::net::TcpListener> for OwnedSocket { + #[inline] + fn from(owned: std::net::TcpListener) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for std::net::TcpListener { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for std::net::TcpListener { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for std::net::TcpListener { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for std::net::TcpListener { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for std::net::UdpSocket { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for std::net::UdpSocket { + #[inline] + fn as_socket(&self) -> BorrowedSocket { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoFd for std::net::UdpSocket { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<std::net::UdpSocket> for OwnedFd { + #[inline] + fn from(owned: std::net::UdpSocket) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoSocket for std::net::UdpSocket { + #[inline] + fn into_socket(self) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<std::net::UdpSocket> for OwnedSocket { + #[inline] + fn from(owned: std::net::UdpSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for std::net::UdpSocket { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for std::net::UdpSocket { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromSocket for std::net::UdpSocket { + #[inline] + fn from_socket(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(windows)] +impl From<OwnedSocket> for std::net::UdpSocket { + #[inline] + fn from(owned: OwnedSocket) -> Self { + unsafe { Self::from_raw_socket(owned.into_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for std::io::Stdin { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::io::Stdin { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<'a> AsFd for std::io::StdinLock<'a> { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl<'a> AsHandle for std::io::StdinLock<'a> { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for std::io::Stdout { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::io::Stdout { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<'a> AsFd for std::io::StdoutLock<'a> { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl<'a> AsHandle for std::io::StdoutLock<'a> { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for std::io::Stderr { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::io::Stderr { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<'a> AsFd for std::io::StderrLock<'a> { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl<'a> AsHandle for std::io::StderrLock<'a> { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(unix)] +impl AsFd for std::process::ChildStdin { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::process::ChildStdin { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(unix)] +impl IntoFd for std::process::ChildStdin { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<std::process::ChildStdin> for OwnedFd { + #[inline] + fn from(owned: std::process::ChildStdin) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for std::process::ChildStdin { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<std::process::ChildStdin> for OwnedHandle { + #[inline] + fn from(owned: std::process::ChildStdin) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(unix)] +impl AsFd for std::process::ChildStdout { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::process::ChildStdout { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(unix)] +impl IntoFd for std::process::ChildStdout { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<std::process::ChildStdout> for OwnedFd { + #[inline] + fn from(owned: std::process::ChildStdout) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for std::process::ChildStdout { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<std::process::ChildStdout> for OwnedHandle { + #[inline] + fn from(owned: std::process::ChildStdout) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(unix)] +impl AsFd for std::process::ChildStderr { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::process::ChildStderr { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(unix)] +impl IntoFd for std::process::ChildStderr { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<std::process::ChildStderr> for OwnedFd { + #[inline] + fn from(owned: std::process::ChildStderr) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl IntoHandle for std::process::ChildStderr { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<std::process::ChildStderr> for OwnedHandle { + #[inline] + fn from(owned: std::process::ChildStderr) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(unix)] +impl FromFd for std::process::Stdio { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for std::process::Stdio { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromHandle for std::process::Stdio { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<OwnedHandle> for std::process::Stdio { + #[inline] + fn from(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl AsHandle for std::process::Child { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(windows)] +impl IntoHandle for std::process::Child { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<std::process::Child> for OwnedHandle { + #[inline] + fn from(owned: std::process::Child) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(unix)] +impl AsFd for std::os::unix::net::UnixStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for std::os::unix::net::UnixStream { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<std::os::unix::net::UnixStream> for OwnedFd { + #[inline] + fn from(owned: std::os::unix::net::UnixStream) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for std::os::unix::net::UnixStream { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for std::os::unix::net::UnixStream { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl AsFd for std::os::unix::net::UnixListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for std::os::unix::net::UnixListener { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<std::os::unix::net::UnixListener> for OwnedFd { + #[inline] + fn from(owned: std::os::unix::net::UnixListener) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for std::os::unix::net::UnixListener { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for std::os::unix::net::UnixListener { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl AsFd for std::os::unix::net::UnixDatagram { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl IntoFd for std::os::unix::net::UnixDatagram { + #[inline] + fn into_fd(self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<std::os::unix::net::UnixDatagram> for OwnedFd { + #[inline] + fn from(owned: std::os::unix::net::UnixDatagram) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl FromFd for std::os::unix::net::UnixDatagram { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(unix)] +impl From<OwnedFd> for std::os::unix::net::UnixDatagram { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl<T> AsHandle for std::thread::JoinHandle<T> { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(windows)] +impl<T> IntoHandle for std::thread::JoinHandle<T> { + #[inline] + fn into_handle(self) -> OwnedHandle { + unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl<T> From<std::thread::JoinHandle<T>> for OwnedHandle { + #[inline] + fn from(owned: std::thread::JoinHandle<T>) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} diff --git a/vendor/io-lifetimes/src/impls_std_views.rs b/vendor/io-lifetimes/src/impls_std_views.rs new file mode 100644 index 000000000..ffa013ffa --- /dev/null +++ b/vendor/io-lifetimes/src/impls_std_views.rs @@ -0,0 +1,31 @@ +use crate::views::{FilelikeViewType, SocketlikeViewType}; +#[cfg(any(unix, target_os = "wasi"))] +use crate::OwnedFd; +#[cfg(windows)] +use crate::{OwnedHandle, OwnedSocket}; + +#[cfg(any(unix, target_os = "wasi"))] +unsafe impl FilelikeViewType for OwnedFd {} + +#[cfg(windows)] +unsafe impl FilelikeViewType for OwnedHandle {} + +#[cfg(windows)] +unsafe impl SocketlikeViewType for OwnedSocket {} + +unsafe impl FilelikeViewType for std::fs::File {} + +unsafe impl SocketlikeViewType for std::net::TcpStream {} + +unsafe impl SocketlikeViewType for std::net::TcpListener {} + +unsafe impl SocketlikeViewType for std::net::UdpSocket {} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for std::os::unix::net::UnixStream {} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for std::os::unix::net::UnixListener {} + +#[cfg(unix)] +unsafe impl SocketlikeViewType for std::os::unix::net::UnixDatagram {} diff --git a/vendor/io-lifetimes/src/impls_tokio.rs b/vendor/io-lifetimes/src/impls_tokio.rs new file mode 100644 index 000000000..6dfde9136 --- /dev/null +++ b/vendor/io-lifetimes/src/impls_tokio.rs @@ -0,0 +1,214 @@ +//! Implementations of io-lifetimes' traits for tokio's types. In the +//! future, we'll prefer to have crates provide their own impls; this is +//! just a temporary measure. + +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, BorrowedFd, FromFd, OwnedFd}; +#[cfg(windows)] +use crate::{AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, FromHandle, OwnedHandle}; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd}; +#[cfg(windows)] +use std::os::windows::io::{AsRawHandle, AsRawSocket, FromRawHandle, IntoRawHandle}; + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::fs::File { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for tokio::fs::File { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromFd for tokio::fs::File { + #[inline] + fn from_fd(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl From<OwnedFd> for tokio::fs::File { + #[inline] + fn from(owned: OwnedFd) -> Self { + unsafe { Self::from_raw_fd(owned.into_raw_fd()) } + } +} + +#[cfg(windows)] +impl FromHandle for tokio::fs::File { + #[inline] + fn from_handle(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(windows)] +impl From<OwnedHandle> for tokio::fs::File { + #[inline] + fn from(owned: OwnedHandle) -> Self { + unsafe { Self::from_raw_handle(owned.into_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::net::TcpStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for tokio::net::TcpStream { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::net::TcpListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for tokio::net::TcpListener { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::net::UdpSocket { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsSocket for tokio::net::UdpSocket { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::io::Stdin { + #[inline] + fn as_fd(&self) -> BorrowedFd { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for tokio::io::Stdin { + #[inline] + fn as_handle(&self) -> BorrowedHandle { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::io::Stdout { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for tokio::io::Stdout { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::io::Stderr { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for tokio::io::Stderr { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(unix)] +impl AsFd for tokio::net::UnixStream { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl AsFd for tokio::net::UnixListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(unix)] +impl AsFd for tokio::net::UnixDatagram { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for tokio::process::ChildStdout { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } +} + +#[cfg(windows)] +impl AsHandle for tokio::process::ChildStdin { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(windows)] +impl AsHandle for tokio::process::ChildStdout { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} + +#[cfg(windows)] +impl AsHandle for tokio::process::ChildStderr { + #[inline] + fn as_handle(&self) -> BorrowedHandle { + unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) } + } +} diff --git a/vendor/io-lifetimes/src/lib.rs b/vendor/io-lifetimes/src/lib.rs new file mode 100644 index 000000000..5e2a9d19f --- /dev/null +++ b/vendor/io-lifetimes/src/lib.rs @@ -0,0 +1,179 @@ +//! Experimental new types and traits to replace the `Raw` family of types and +//! traits. +//! +//! This API has much conceptual similarity with the `Raw` API, but introduces +//! explicit concepts of ownership and borrowing: +//! +//! | `Raw` API | This experimental API | +//! | ---------- | ------------------------ | +//! | `Raw*` | `Borrowed*` and `Owned*` | +//! | `AsRaw*` | `As*` | +//! | `IntoRaw*` | `Into*` | +//! | `FromRaw*` | `From*` | +//! +//! This gives it several advantages: +//! +//! - Less `unsafe` in user code! +//! +//! - Easier to understand ownership. +//! +//! - It avoids the inconsistency where `AsRawFd` and `IntoRawFd` return +//! `RawFd` values that users ought to be able to trust, but aren't unsafe, +//! so it's possible to fail to uphold this trust in purely safe Rust. +//! +//! - It enables a number of safe and portable convenience features, such as +//! [safe typed views] and [from+into conversions]. +//! +//! [safe typed views]: AsFilelike::as_filelike_view +//! [from+into conversions]: FromFilelike::from_into_filelike + +#![deny(missing_docs)] +#![cfg_attr(rustc_attrs, feature(rustc_attrs))] +#![cfg_attr(all(io_lifetimes_use_std, target_os = "wasi"), feature(wasi_ext))] +#![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + +mod portability; +mod traits; +#[cfg(not(io_lifetimes_use_std))] +mod types; + +#[cfg(not(io_lifetimes_use_std))] +mod impls_std; +mod impls_std_views; + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(any(unix, target_os = "wasi"))] +pub use traits::AsFd; +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +pub use traits::{AsHandle, AsSocket}; +#[cfg(any(unix, target_os = "wasi"))] +pub use traits::{FromFd, IntoFd}; +#[cfg(windows)] +pub use traits::{FromHandle, FromSocket, IntoHandle, IntoSocket}; + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(any(unix, target_os = "wasi"))] +pub use types::{BorrowedFd, OwnedFd}; +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +pub use types::{ + BorrowedHandle, BorrowedSocket, HandleOrInvalid, InvalidHandleError, NullHandleError, + OwnedHandle, OwnedSocket, +}; + +#[cfg(io_lifetimes_use_std)] +#[cfg(unix)] +pub use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; +#[cfg(io_lifetimes_use_std)] +#[cfg(target_os = "wasi")] +pub use std::os::wasi::io::{AsFd, BorrowedFd, OwnedFd}; +#[cfg(io_lifetimes_use_std)] +#[cfg(windows)] +pub use std::os::windows::io::{ + AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, HandleOrInvalid, InvalidHandleError, + NullHandleError, OwnedHandle, OwnedSocket, +}; + +// io-lifetimes defined `FromFd`/`IntoFd` traits instead of just using +// `From`/`Into` because that allowed it to implement them for foreign types, +// including std types like File and TcpStream, and popular third-party types. +// +// std just uses `From`/`Into`, because it defines those traits itself so it +// can implement them for std types itself, and std won't be implementing them +// for third-party types. However, this means that until `OwnedFd` et al are +// stabilized, there will be no impls for third-party traits. +// +// So we define `FromFd`/`IntoFd` traits, and implement them in terms of +// `From`/`Into`, +#[cfg(io_lifetimes_use_std)] +#[cfg(any(unix, target_os = "wasi"))] +impl<T: From<OwnedFd>> FromFd for T { + #[inline] + fn from_fd(owned_fd: OwnedFd) -> Self { + owned_fd.into() + } +} +#[cfg(io_lifetimes_use_std)] +#[cfg(any(unix, target_os = "wasi"))] +impl<T> IntoFd for T +where + OwnedFd: From<T>, +{ + #[inline] + fn into_fd(self) -> OwnedFd { + self.into() + } +} + +#[cfg(io_lifetimes_use_std)] +#[cfg(windows)] +impl<T: From<OwnedHandle>> FromHandle for T { + #[inline] + fn from_handle(owned_handle: OwnedHandle) -> Self { + owned_handle.into() + } +} +#[cfg(io_lifetimes_use_std)] +#[cfg(windows)] +impl<T> IntoHandle for T +where + OwnedHandle: From<T>, +{ + #[inline] + fn into_handle(self) -> OwnedHandle { + self.into() + } +} + +#[cfg(io_lifetimes_use_std)] +#[cfg(windows)] +impl<T: From<OwnedSocket>> FromSocket for T { + #[inline] + fn from_socket(owned_socket: OwnedSocket) -> Self { + owned_socket.into() + } +} +#[cfg(io_lifetimes_use_std)] +#[cfg(windows)] +impl<T> IntoSocket for T +where + OwnedSocket: From<T>, +{ + #[inline] + fn into_socket(self) -> OwnedSocket { + self.into() + } +} + +pub use portability::{ + AsFilelike, AsSocketlike, BorrowedFilelike, BorrowedSocketlike, FromFilelike, FromSocketlike, + IntoFilelike, IntoSocketlike, OwnedFilelike, OwnedSocketlike, +}; + +#[cfg(feature = "close")] +pub mod example_ffi; +pub mod raw; +pub mod views; + +// Ideally, we'd want crates to implement our traits themselves. But for now, +// while we're prototyping, we provide a few impls on foreign types. +#[cfg(not(io_lifetimes_use_std))] +#[cfg(feature = "async-std")] +mod impls_async_std; +#[cfg(not(io_lifetimes_use_std))] +#[cfg(feature = "fs-err")] +mod impls_fs_err; +#[cfg(not(io_lifetimes_use_std))] +#[cfg(feature = "mio")] +mod impls_mio; +#[cfg(not(target_os = "wasi"))] +#[cfg(not(io_lifetimes_use_std))] +#[cfg(feature = "os_pipe")] +mod impls_os_pipe; +#[cfg(not(io_lifetimes_use_std))] +#[cfg(feature = "socket2")] +mod impls_socket2; +#[cfg(not(io_lifetimes_use_std))] +#[cfg(feature = "tokio")] +mod impls_tokio; diff --git a/vendor/io-lifetimes/src/portability.rs b/vendor/io-lifetimes/src/portability.rs new file mode 100644 index 000000000..ee9057501 --- /dev/null +++ b/vendor/io-lifetimes/src/portability.rs @@ -0,0 +1,518 @@ +//! Portability abstractions over `Owned*` and `Borrowed*`. +//! +//! On Unix, "everything is a file descriptor". On Windows, file/pipe/process +//! handles are distinct from socket descriptors. This file provides a minimal +//! layer of portability over this difference. + +use crate::views::{FilelikeView, FilelikeViewType, SocketlikeView, SocketlikeViewType}; +#[cfg(any(unix, target_os = "wasi"))] +use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd}; +#[cfg(windows)] +use crate::{ + AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, FromHandle, FromSocket, IntoHandle, + IntoSocket, OwnedHandle, OwnedSocket, +}; + +/// A reference to a filelike object. +/// +/// This is a portability abstraction over Unix-like [`BorrowedFd`] and +/// Windows' `BorrowedHandle`. +#[cfg(any(unix, target_os = "wasi"))] +pub type BorrowedFilelike<'filelike> = BorrowedFd<'filelike>; + +/// A reference to a filelike object. +/// +/// This is a portability abstraction over Unix-like `BorrowedFd` and +/// Windows' [`BorrowedHandle`]. +#[cfg(windows)] +pub type BorrowedFilelike<'filelike> = BorrowedHandle<'filelike>; + +/// A reference to a socketlike object. +/// +/// This is a portability abstraction over Unix-like [`BorrowedFd`] and +/// Windows' `BorrowedSocket`. +#[cfg(any(unix, target_os = "wasi"))] +pub type BorrowedSocketlike<'socketlike> = BorrowedFd<'socketlike>; + +/// A reference to a socketlike object. +/// +/// This is a portability abstraction over Unix-like `BorrowedFd` and +/// Windows' [`BorrowedSocket`]. +#[cfg(windows)] +pub type BorrowedSocketlike<'socketlike> = BorrowedSocket<'socketlike>; + +/// An owned filelike object. +/// +/// This is a portability abstraction over Unix-like [`OwnedFd`] and +/// Windows' `OwnedHandle`. +#[cfg(any(unix, target_os = "wasi"))] +pub type OwnedFilelike = OwnedFd; + +/// An owned filelike object. +/// +/// This is a portability abstraction over Unix-like `OwnedFd` and +/// Windows' [`OwnedHandle`]. +#[cfg(windows)] +pub type OwnedFilelike = OwnedHandle; + +/// An owned socketlike object. +/// +/// This is a portability abstraction over Unix-like [`OwnedFd`] and +/// Windows' `OwnedSocket`. +#[cfg(any(unix, target_os = "wasi"))] +pub type OwnedSocketlike = OwnedFd; + +/// An owned socketlike object. +/// +/// This is a portability abstraction over Unix-like `OwnedFd` and +/// Windows' [`OwnedSocket`]. +#[cfg(windows)] +pub type OwnedSocketlike = OwnedSocket; + +/// A portable trait to borrow a reference from an underlying filelike object. +/// +/// This is a portability abstraction over Unix-like [`AsFd`] and Windows' +/// `AsHandle`. It also provides the `as_filelike_view` convenience function +/// providing typed views. +#[cfg(any(unix, target_os = "wasi"))] +pub trait AsFilelike: AsFd { + /// Borrows the reference. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{AsFilelike, BorrowedFilelike}; + /// + /// let mut f = File::open("foo.txt")?; + /// let borrowed_filelike: BorrowedFilelike<'_> = f.as_filelike(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn as_filelike(&self) -> BorrowedFilelike<'_>; + + /// Return a borrowing view of a resource which dereferences to a `&Target`. + /// + /// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases, + /// such as [`File`], `Read` and `Write` are implemented for `&Target` in + /// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on + /// the resuting view, like this: + /// + /// ```rust,ignore + /// let v = f.as_filelike_view::<std::fs::File>(); + /// (&*v).read(&mut buf).unwrap(); + /// ``` + /// + /// [`File`]: std::fs::File + fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target>; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: AsFd> AsFilelike for T { + #[inline] + fn as_filelike(&self) -> BorrowedFilelike<'_> { + self.as_fd() + } + + #[inline] + fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target> { + FilelikeView::new(self) + } +} + +/// A portable trait to borrow a reference from an underlying filelike object. +/// +/// This is a portability abstraction over Unix-like `AsFd` and Windows' +/// [`AsHandle`]. It also provides the `as_filelike_view` convenience function +/// providing typed views. +#[cfg(windows)] +pub trait AsFilelike: AsHandle { + /// Borrows the reference. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{AsFilelike, BorrowedFilelike}; + /// + /// let mut f = File::open("foo.txt")?; + /// let borrowed_filelike: BorrowedFilelike<'_> = f.as_filelike(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn as_filelike(&self) -> BorrowedFilelike<'_>; + + /// Return a borrowing view of a resource which dereferences to a `&Target`. + /// + /// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases, + /// such as [`File`], `Read` and `Write` are implemented for `&Target` in + /// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on + /// the resuting view, like this: + /// + /// ```rust,ignore + /// let v = f.as_filelike_view::<std::fs::File>(); + /// (&*v).read(&mut buf).unwrap(); + /// ``` + /// + /// [`File`]: std::fs::File + fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target>; +} + +#[cfg(windows)] +impl<T: AsHandle> AsFilelike for T { + #[inline] + fn as_filelike(&self) -> BorrowedFilelike<'_> { + self.as_handle() + } + + #[inline] + fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target> { + FilelikeView::new(self) + } +} + +/// A portable trait to borrow a reference from an underlying socketlike +/// object. +/// +/// This is a portability abstraction over Unix-like [`AsFd`] and Windows' +/// `AsSocket`. It also provides the `as_socketlike_view` convenience +/// function providing typed views. +#[cfg(any(unix, target_os = "wasi"))] +pub trait AsSocketlike: AsFd { + /// Borrows the reference. + fn as_socketlike(&self) -> BorrowedSocketlike<'_>; + + /// Return a borrowing view of a resource which dereferences to a `&Target`. + /// + /// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases, + /// such as [`TcpStream`], `Read` and `Write` are implemented for `&Target` in + /// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on + /// the resuting view, like this: + /// + /// ```rust,ignore + /// let v = s.as_socketlike_view::<std::net::TcpStream>(); + /// (&*v).read(&mut buf).unwrap(); + /// ``` + /// + /// [`TcpStream`]: std::net::TcpStream + fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target>; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: AsFd> AsSocketlike for T { + #[inline] + fn as_socketlike(&self) -> BorrowedSocketlike<'_> { + self.as_fd() + } + + #[inline] + fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target> { + SocketlikeView::new(self) + } +} + +/// A portable trait to borrow a reference from an underlying socketlike +/// object. +/// +/// This is a portability abstraction over Unix-like `AsFd` and Windows' +/// [`AsSocket`]. It also provides the `as_socketlike_view` convenience +/// function providing typed views. +#[cfg(windows)] +pub trait AsSocketlike: AsSocket { + /// Borrows the reference. + fn as_socketlike(&self) -> BorrowedSocketlike; + + /// Return a borrowing view of a resource which dereferences to a `&Target`. + /// + /// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases, + /// such as [`TcpStream`], `Read` and `Write` are implemented for `&Target` in + /// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on + /// the resuting view, like this: + /// + /// ```rust,ignore + /// let v = s.as_socketlike_view::<std::net::TcpStream>(); + /// (&*v).read(&mut buf).unwrap(); + /// ``` + /// + /// [`TcpStream`]: std::net::TcpStream + fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target>; +} + +#[cfg(windows)] +impl<T: AsSocket> AsSocketlike for T { + #[inline] + fn as_socketlike(&self) -> BorrowedSocketlike<'_> { + self.as_socket() + } + + #[inline] + fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target> { + SocketlikeView::new(self) + } +} + +/// A portable trait to express the ability to consume an object and acquire +/// ownership of its filelike object. +/// +/// This is a portability abstraction over Unix-like [`IntoFd`] and Windows' +/// `IntoHandle`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait IntoFilelike: IntoFd { + /// Consumes this object, returning the underlying filelike object. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{IntoFilelike, OwnedFilelike}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_filelike: OwnedFilelike = f.into_filelike(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn into_filelike(self) -> OwnedFilelike; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: IntoFd> IntoFilelike for T { + #[inline] + fn into_filelike(self) -> OwnedFilelike { + self.into_fd() + } +} + +/// A portable trait to express the ability to consume an object and acquire +/// ownership of its filelike object. +/// +/// This is a portability abstraction over Unix-like `IntoFd` and Windows' +/// [`IntoHandle`]. +#[cfg(windows)] +pub trait IntoFilelike: IntoHandle { + /// Consumes this object, returning the underlying filelike object. + fn into_filelike(self) -> OwnedFilelike; +} + +#[cfg(windows)] +impl<T: IntoHandle> IntoFilelike for T { + #[inline] + fn into_filelike(self) -> OwnedFilelike { + self.into_handle() + } +} + +/// A portable trait to express the ability to consume an object and acquire +/// ownership of its socketlike object. +/// +/// This is a portability abstraction over Unix-like [`IntoFd`] and Windows' +/// `IntoSocket`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait IntoSocketlike: IntoFd { + /// Consumes this object, returning the underlying socketlike object. + fn into_socketlike(self) -> OwnedSocketlike; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: IntoFd> IntoSocketlike for T { + #[inline] + fn into_socketlike(self) -> OwnedSocketlike { + self.into_fd() + } +} + +/// A portable trait to express the ability to consume an object and acquire +/// ownership of its socketlike object. +/// +/// This is a portability abstraction over Unix-like `IntoFd` and Windows' +/// [`IntoSocket`]. +#[cfg(windows)] +pub trait IntoSocketlike: IntoSocket { + /// Consumes this object, returning the underlying socketlike object. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{IntoFilelike, OwnedFilelike}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_filelike: OwnedFilelike = f.into_filelike(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn into_socketlike(self) -> OwnedSocketlike; +} + +#[cfg(windows)] +impl<T: IntoSocket> IntoSocketlike for T { + #[inline] + fn into_socketlike(self) -> OwnedSocketlike { + self.into_socket() + } +} + +/// A portable trait to express the ability to construct an object from a +/// filelike object. +/// +/// This is a portability abstraction over Unix-like [`FromFd`] and Windows' +/// `FromHandle`. It also provides the `from_into_filelike` convenience +/// function providing simplified from+into conversions. +#[cfg(any(unix, target_os = "wasi"))] +pub trait FromFilelike: FromFd { + /// Constructs a new instance of `Self` from the given filelike object. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromFilelike, IntoFilelike, OwnedFilelike}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_filelike: OwnedFilelike = f.into_filelike(); + /// let f = File::from_filelike(owned_filelike); + /// # Ok::<(), io::Error>(()) + /// ``` + fn from_filelike(owned: OwnedFilelike) -> Self; + + /// Constructs a new instance of `Self` from the given filelike object + /// converted from `into_owned`. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromFilelike, IntoFilelike}; + /// + /// let f = File::open("foo.txt")?; + /// let f = File::from_into_filelike(f); + /// # Ok::<(), io::Error>(()) + /// ``` + fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: FromFd> FromFilelike for T { + #[inline] + fn from_filelike(owned: OwnedFilelike) -> Self { + Self::from_fd(owned) + } + + #[inline] + fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self { + Self::from_filelike(owned.into_filelike()) + } +} + +/// A portable trait to express the ability to construct an object from a +/// filelike object. +/// +/// This is a portability abstraction over Unix-like `FromFd` and Windows' +/// [`FromHandle`]. It also provides the `from_into_filelike` convenience +/// function providing simplified from+into conversions. +#[cfg(windows)] +pub trait FromFilelike: FromHandle { + /// Constructs a new instance of `Self` from the given filelike object. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromFilelike, IntoFilelike, OwnedFilelike}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_filelike: OwnedFilelike = f.into_filelike(); + /// let f = File::from_filelike(owned_filelike); + /// # Ok::<(), io::Error>(()) + /// ``` + fn from_filelike(owned: OwnedFilelike) -> Self; + + /// Constructs a new instance of `Self` from the given filelike object + /// converted from `into_owned`. + /// + /// # Example + /// + /// ```rust,no_run + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromFilelike, IntoFilelike}; + /// + /// let f = File::open("foo.txt")?; + /// let f = File::from_into_filelike(f); + /// # Ok::<(), io::Error>(()) + /// ``` + fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self; +} + +#[cfg(windows)] +impl<T: FromHandle> FromFilelike for T { + #[inline] + fn from_filelike(owned: OwnedFilelike) -> Self { + Self::from_handle(owned) + } + + #[inline] + fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self { + Self::from_filelike(owned.into_filelike()) + } +} + +/// A portable trait to express the ability to construct an object from a +/// socketlike object. +/// +/// This is a portability abstraction over Unix-like [`FromFd`] and Windows' +/// `FromSocket`. It also provides the `from_into_socketlike` convenience +/// function providing simplified from+into conversions. +#[cfg(any(unix, target_os = "wasi"))] +pub trait FromSocketlike: FromFd { + /// Constructs a new instance of `Self` from the given socketlike object. + fn from_socketlike(owned: OwnedSocketlike) -> Self; + + /// Constructs a new instance of `Self` from the given socketlike object + /// converted from `into_owned`. + fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: FromFd> FromSocketlike for T { + #[inline] + fn from_socketlike(owned: OwnedSocketlike) -> Self { + Self::from_fd(owned) + } + + #[inline] + fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self { + Self::from_socketlike(owned.into_socketlike()) + } +} + +/// A portable trait to express the ability to construct an object from a +/// socketlike object. +/// +/// This is a portability abstraction over Unix-like `FromFd` and Windows' +/// [`FromSocket`]. It also provides the `from_into_socketlike` convenience +/// function providing simplified from+into conversions. +#[cfg(windows)] +pub trait FromSocketlike: FromSocket { + /// Constructs a new instance of `Self` from the given socketlike object. + fn from_socketlike(owned: OwnedSocketlike) -> Self; + + /// Constructs a new instance of `Self` from the given socketlike object + /// converted from `into_owned`. + fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self; +} + +#[cfg(windows)] +impl<T: FromSocket> FromSocketlike for T { + #[inline] + fn from_socketlike(owned: OwnedSocketlike) -> Self { + Self::from_socket(owned) + } + + #[inline] + fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self { + Self::from_socketlike(owned.into_socketlike()) + } +} diff --git a/vendor/io-lifetimes/src/raw.rs b/vendor/io-lifetimes/src/raw.rs new file mode 100644 index 000000000..0264f4084 --- /dev/null +++ b/vendor/io-lifetimes/src/raw.rs @@ -0,0 +1,253 @@ +//! Portability abstractions over `Raw*`. +//! +//! On Unix, "everything is a file descriptor". On Windows, file/pipe/process +//! handles are distinct from socket descriptors. This file provides a minimal +//! layer of portability over this difference. + +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +#[cfg(windows)] +use std::os::windows::io::{ + AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket, + RawHandle, RawSocket, +}; + +/// A raw filelike object. +/// +/// This is a portability abstraction over Unix-like [`RawFd`] and +/// Windows' `RawHandle`. +#[cfg(any(unix, target_os = "wasi"))] +pub type RawFilelike = RawFd; + +/// A raw filelike object. +/// +/// This is a portability abstraction over Unix-like `RawFd` and +/// Windows' [`RawHandle`]. +#[cfg(windows)] +pub type RawFilelike = RawHandle; + +/// A raw socketlike object. +/// +/// This is a portability abstraction over Unix-like [`RawFd`] and +/// Windows' `RawSocket`. +#[cfg(any(unix, target_os = "wasi"))] +pub type RawSocketlike = RawFd; + +/// A raw socketlike object. +/// +/// This is a portability abstraction over Unix-like `RawFd` and +/// Windows' [`RawSocket`]. +#[cfg(windows)] +pub type RawSocketlike = RawSocket; + +/// A portable trait to obtain the raw value of an underlying filelike object. +/// +/// This is a portability abstraction over Unix-like [`AsRawFd`] and Windows' +/// `AsRawHandle`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait AsRawFilelike: AsRawFd { + /// Returns the raw value. + fn as_raw_filelike(&self) -> RawFilelike; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: AsRawFd> AsRawFilelike for T { + #[inline] + fn as_raw_filelike(&self) -> RawFilelike { + self.as_raw_fd() + } +} + +/// This is a portability abstraction over Unix-like `AsRawFd` and Windows' +/// [`AsRawHandle`]. +#[cfg(windows)] +pub trait AsRawFilelike: AsRawHandle { + /// Returns the raw value. + fn as_raw_filelike(&self) -> RawFilelike; +} + +#[cfg(windows)] +impl<T: AsRawHandle> AsRawFilelike for T { + #[inline] + fn as_raw_filelike(&self) -> RawFilelike { + self.as_raw_handle() + } +} + +/// This is a portability abstraction over Unix-like [`AsRawFd`] and Windows' +/// `AsRawSocket`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait AsRawSocketlike: AsRawFd { + /// Returns the raw value. + fn as_raw_socketlike(&self) -> RawSocketlike; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: AsRawFd> AsRawSocketlike for T { + #[inline] + fn as_raw_socketlike(&self) -> RawSocketlike { + self.as_raw_fd() + } +} + +/// This is a portability abstraction over Unix-like `AsRawFd` and Windows' +/// [`AsRawSocket`]. +#[cfg(windows)] +pub trait AsRawSocketlike: AsRawSocket { + /// Returns the raw value. + fn as_raw_socketlike(&self) -> RawSocketlike; +} + +#[cfg(windows)] +impl<T: AsRawSocket> AsRawSocketlike for T { + #[inline] + fn as_raw_socketlike(&self) -> RawSocketlike { + self.as_raw_socket() + } +} + +/// This is a portability abstraction over Unix-like [`IntoRawFd`] and Windows' +/// `IntoRawHandle`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait IntoRawFilelike: IntoRawFd { + /// Returns the raw value. + fn into_raw_filelike(self) -> RawFilelike; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: IntoRawFd> IntoRawFilelike for T { + #[inline] + fn into_raw_filelike(self) -> RawFilelike { + self.into_raw_fd() + } +} + +/// This is a portability abstraction over Unix-like `IntoRawFd` and Windows' +/// [`IntoRawHandle`]. +#[cfg(windows)] +pub trait IntoRawFilelike: IntoRawHandle { + /// Returns the raw value. + fn into_raw_filelike(self) -> RawFilelike; +} + +#[cfg(windows)] +impl<T: IntoRawHandle> IntoRawFilelike for T { + #[inline] + fn into_raw_filelike(self) -> RawFilelike { + self.into_raw_handle() + } +} + +/// This is a portability abstraction over Unix-like [`IntoRawFd`] and Windows' +/// `IntoRawSocket`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait IntoRawSocketlike: IntoRawFd { + /// Returns the raw value. + fn into_raw_socketlike(self) -> RawSocketlike; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: IntoRawFd> IntoRawSocketlike for T { + #[inline] + fn into_raw_socketlike(self) -> RawSocketlike { + self.into_raw_fd() + } +} + +/// This is a portability abstraction over Unix-like `IntoRawFd` and Windows' +/// [`IntoRawSocket`]. +#[cfg(windows)] +pub trait IntoRawSocketlike: IntoRawSocket { + /// Returns the raw value. + fn into_raw_socketlike(self) -> RawSocketlike; +} + +#[cfg(windows)] +impl<T: IntoRawSocket> IntoRawSocketlike for T { + #[inline] + fn into_raw_socketlike(self) -> RawSocketlike { + self.into_raw_socket() + } +} + +/// This is a portability abstraction over Unix-like [`FromRawFd`] and Windows' +/// `FromRawHandle`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait FromRawFilelike: FromRawFd { + /// Constructs `Self` from the raw value. + /// + /// # Safety + /// + /// This is `unsafe` for the same reason as [`from_raw_fd`] and + /// [`from_raw_handle`]. + /// + /// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.from_raw_fd + /// [`from_raw_handle`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.FromRawHandle.html#tymethod.from_raw_handle + unsafe fn from_raw_filelike(raw: RawFilelike) -> Self; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: FromRawFd> FromRawFilelike for T { + #[inline] + unsafe fn from_raw_filelike(raw: RawFilelike) -> Self { + Self::from_raw_fd(raw) + } +} + +/// This is a portability abstraction over Unix-like `FromRawFd` and Windows' +/// [`FromRawHandle`]. +#[cfg(windows)] +pub trait FromRawFilelike: FromRawHandle { + /// Constructs `Self` from the raw value. + unsafe fn from_raw_filelike(raw: RawFilelike) -> Self; +} + +#[cfg(windows)] +impl<T: FromRawHandle> FromRawFilelike for T { + #[inline] + unsafe fn from_raw_filelike(raw: RawFilelike) -> Self { + Self::from_raw_handle(raw) + } +} + +/// This is a portability abstraction over Unix-like [`FromRawFd`] and Windows' +/// `FromRawSocket`. +#[cfg(any(unix, target_os = "wasi"))] +pub trait FromRawSocketlike: FromRawFd { + /// Constructs `Self` from the raw value. + /// + /// # Safety + /// + /// This is `unsafe` for the same reason as [`from_raw_fd`] and + /// [`from_raw_socket`]. + /// + /// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.from_raw_fd + /// [`from_raw_socket`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.FromRawSocket.html#tymethod.from_raw_socket + unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self; +} + +#[cfg(any(unix, target_os = "wasi"))] +impl<T: FromRawFd> FromRawSocketlike for T { + #[inline] + unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self { + Self::from_raw_fd(raw) + } +} + +/// This is a portability abstraction over Unix-like `FromRawFd` and Windows' +/// [`FromRawSocket`]. +#[cfg(windows)] +pub trait FromRawSocketlike: FromRawSocket { + /// Constructs `Self` from the raw value. + unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self; +} + +#[cfg(windows)] +impl<T: FromRawSocket> FromRawSocketlike for T { + #[inline] + unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self { + Self::from_raw_socket(raw) + } +} diff --git a/vendor/io-lifetimes/src/traits.rs b/vendor/io-lifetimes/src/traits.rs new file mode 100644 index 000000000..788045208 --- /dev/null +++ b/vendor/io-lifetimes/src/traits.rs @@ -0,0 +1,274 @@ +#[cfg(not(io_lifetimes_use_std))] +#[cfg(any(unix, target_os = "wasi"))] +use crate::BorrowedFd; +#[cfg(any(unix, target_os = "wasi"))] +use crate::OwnedFd; +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +use crate::{BorrowedHandle, BorrowedSocket}; +#[cfg(windows)] +use crate::{OwnedHandle, OwnedSocket}; + +/// A trait to borrow the file descriptor from an underlying object. +/// +/// This is only available on unix platforms and must be imported in order to +/// call the method. Windows platforms have a corresponding `AsHandle` and +/// `AsSocket` set of traits. +#[cfg(not(io_lifetimes_use_std))] +#[cfg(any(unix, target_os = "wasi"))] +pub trait AsFd { + /// Borrows the file descriptor. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{AsFd, BorrowedFd}; + /// + /// let mut f = File::open("foo.txt")?; + /// let borrowed_fd: BorrowedFd<'_> = f.as_fd(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn as_fd(&self) -> BorrowedFd<'_>; +} + +/// A trait to borrow the handle from an underlying object. +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +pub trait AsHandle { + /// Borrows the handle. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{AsHandle, BorrowedHandle}; + /// + /// let mut f = File::open("foo.txt")?; + /// let borrowed_handle: BorrowedHandle<'_> = f.as_handle(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn as_handle(&self) -> BorrowedHandle<'_>; +} + +/// A trait to borrow the socket from an underlying object. +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +pub trait AsSocket { + /// Borrows the socket. + fn as_socket(&self) -> BorrowedSocket<'_>; +} + +/// A trait to express the ability to consume an object and acquire ownership +/// of its file descriptor. +#[cfg(any(unix, target_os = "wasi"))] +pub trait IntoFd { + /// Consumes this object, returning the underlying file descriptor. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{IntoFd, OwnedFd}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_fd: OwnedFd = f.into_fd(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn into_fd(self) -> OwnedFd; +} + +/// A trait to express the ability to consume an object and acquire ownership +/// of its handle. +#[cfg(windows)] +pub trait IntoHandle { + /// Consumes this object, returning the underlying handle. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{IntoHandle, OwnedHandle}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_handle: OwnedHandle = f.into_handle(); + /// # Ok::<(), io::Error>(()) + /// ``` + fn into_handle(self) -> OwnedHandle; +} + +/// A trait to express the ability to consume an object and acquire ownership +/// of its socket. +#[cfg(windows)] +pub trait IntoSocket { + /// Consumes this object, returning the underlying socket. + fn into_socket(self) -> OwnedSocket; +} + +/// A trait to express the ability to construct an object from a file +/// descriptor. +#[cfg(any(unix, target_os = "wasi"))] +pub trait FromFd { + /// Constructs a new instance of `Self` from the given file descriptor. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromFd, IntoFd, OwnedFd}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_fd: OwnedFd = f.into_fd(); + /// let f = File::from_fd(owned_fd); + /// # Ok::<(), io::Error>(()) + /// ``` + fn from_fd(owned: OwnedFd) -> Self; + + /// Constructs a new instance of `Self` from the given file descriptor + /// converted from `into_owned`. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromFd, IntoFd}; + /// + /// let f = File::open("foo.txt")?; + /// let f = File::from_into_fd(f); + /// # Ok::<(), io::Error>(()) + /// ``` + #[inline] + fn from_into_fd<Owned: IntoFd>(into_owned: Owned) -> Self + where + Self: Sized, + { + Self::from_fd(into_owned.into_fd()) + } +} + +/// A trait to express the ability to construct an object from a handle. +#[cfg(windows)] +pub trait FromHandle { + /// Constructs a new instance of `Self` from the given handle. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromHandle, IntoHandle, OwnedHandle}; + /// + /// let f = File::open("foo.txt")?; + /// let owned_handle: OwnedHandle = f.into_handle(); + /// let f = File::from_handle(owned_handle); + /// # Ok::<(), io::Error>(()) + /// ``` + fn from_handle(owned: OwnedHandle) -> Self; + + /// Constructs a new instance of `Self` from the given handle converted + /// from `into_owned`. + /// + /// # Example + /// + /// ```rust,no_run + /// # #![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + /// use std::fs::File; + /// # use std::io; + /// use io_lifetimes::{FromHandle, IntoHandle}; + /// + /// let f = File::open("foo.txt")?; + /// let f = File::from_into_handle(f); + /// # Ok::<(), io::Error>(()) + /// ``` + #[inline] + fn from_into_handle<Owned: IntoHandle>(into_owned: Owned) -> Self + where + Self: Sized, + { + Self::from_handle(into_owned.into_handle()) + } +} + +/// A trait to express the ability to construct an object from a socket. +#[cfg(windows)] +pub trait FromSocket { + /// Constructs a new instance of `Self` from the given socket. + fn from_socket(owned: OwnedSocket) -> Self; + + /// Constructs a new instance of `Self` from the given socket converted + /// from `into_owned`. + #[inline] + fn from_into_socket<Owned: IntoSocket>(into_owned: Owned) -> Self + where + Self: Sized, + { + Self::from_socket(into_owned.into_socket()) + } +} + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(any(unix, target_os = "wasi"))] +impl<T: AsFd> AsFd for &T { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + T::as_fd(self) + } +} + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(any(unix, target_os = "wasi"))] +impl<T: AsFd> AsFd for &mut T { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + T::as_fd(self) + } +} + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +impl<T: AsHandle> AsHandle for &T { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + T::as_handle(self) + } +} + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +impl<T: AsHandle> AsHandle for &mut T { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + T::as_handle(self) + } +} + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +impl<T: AsSocket> AsSocket for &T { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + T::as_socket(self) + } +} + +#[cfg(not(io_lifetimes_use_std))] +#[cfg(windows)] +impl<T: AsSocket> AsSocket for &mut T { + #[inline] + fn as_socket(&self) -> BorrowedSocket<'_> { + T::as_socket(self) + } +} diff --git a/vendor/io-lifetimes/src/types.rs b/vendor/io-lifetimes/src/types.rs new file mode 100644 index 000000000..695ae513c --- /dev/null +++ b/vendor/io-lifetimes/src/types.rs @@ -0,0 +1,894 @@ +use std::fmt; +use std::marker::PhantomData; +use std::mem::forget; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +#[cfg(target_os = "wasi")] +use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +#[cfg(windows)] +use std::{ + convert::TryFrom, + os::windows::io::{ + AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket, + RawHandle, RawSocket, + }, +}; +#[cfg(all(windows, feature = "close"))] +use { + windows_sys::Win32::Foundation::{ + CloseHandle, DuplicateHandle, SetHandleInformation, BOOL, DUPLICATE_HANDLE_OPTIONS, + DUPLICATE_SAME_ACCESS, HANDLE, HANDLE_FLAG_INHERIT, INVALID_HANDLE_VALUE, + }, + windows_sys::Win32::Networking::WinSock::{ + closesocket, WSADuplicateSocketW, WSAGetLastError, WSASocketW, INVALID_SOCKET, SOCKET, + SOCKET_ERROR, WSAEINVAL, WSAEPROTOTYPE, WSAPROTOCOL_INFOW, WSA_FLAG_NO_HANDLE_INHERIT, + WSA_FLAG_OVERLAPPED, + }, + windows_sys::Win32::System::Threading::{GetCurrentProcess, GetCurrentProcessId}, +}; + +#[cfg(all(windows, not(feature = "close")))] +type HANDLE = isize; +#[cfg(all(windows, not(feature = "close")))] +const INVALID_HANDLE_VALUE: HANDLE = !0 as _; +#[cfg(all(windows, not(feature = "close")))] +const INVALID_SOCKET: usize = !0 as _; + +/// A borrowed file descriptor. +/// +/// This has a lifetime parameter to tie it to the lifetime of something that +/// owns the file descriptor. +/// +/// This uses `repr(transparent)` and has the representation of a host file +/// descriptor, so it can be used in FFI in places where a file descriptor is +/// passed as an argument, it is not captured or consumed, and it never has the +/// value `-1`. +/// +/// This type's `.to_owned()` implementation returns another `BorrowedFd` +/// rather than an `OwnedFd`. It just makes a trivial copy of the raw file +/// descriptor, which is then borrowed under the same lifetime. +#[cfg(any(unix, target_os = "wasi"))] +#[derive(Copy, Clone)] +#[repr(transparent)] +#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)] +#[cfg_attr(rustc_attrs, 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. +#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE))] +pub struct BorrowedFd<'fd> { + fd: RawFd, + _phantom: PhantomData<&'fd OwnedFd>, +} + +/// A borrowed handle. +/// +/// This has a lifetime parameter to tie it to the lifetime of something that +/// owns the handle. +/// +/// This uses `repr(transparent)` and has the representation of a host handle, +/// so it can be used in FFI in places where a handle is passed as an argument, +/// it is not captured or consumed, and it is never null. +/// +/// Note that it *may* have the value `-1`, which in `BorrowedHandle` always +/// represents a valid handle value, such as [the current process handle], and +/// not `INVALID_HANDLE_VALUE`, despite the two having the same value. See +/// [here] for the full story. +/// +/// This type's `.to_owned()` implementation returns another `BorrowedHandle` +/// rather than an `OwnedHandle`. It just makes a trivial copy of the raw +/// handle, which is then borrowed under the same lifetime. +/// +/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443 +/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks +#[cfg(windows)] +#[derive(Copy, Clone)] +#[repr(transparent)] +pub struct BorrowedHandle<'handle> { + handle: RawHandle, + _phantom: PhantomData<&'handle OwnedHandle>, +} + +/// A borrowed socket. +/// +/// This has a lifetime parameter to tie it to the lifetime of something that +/// owns the socket. +/// +/// This uses `repr(transparent)` and has the representation of a host socket, +/// so it can be used in FFI in places where a socket is passed as an argument, +/// it is not captured or consumed, and it never has the value +/// [`INVALID_SOCKET`]. +/// +/// This type's `.to_owned()` implementation returns another `BorrowedSocket` +/// rather than an `OwnedSocket`. It just makes a trivial copy of the raw +/// socket, which is then borrowed under the same lifetime. +#[cfg(windows)] +#[derive(Copy, Clone)] +#[repr(transparent)] +#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)] +#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_start(0))] +// This is -2, in two's complement. -1 is `INVALID_SOCKET`. +#[cfg_attr( + all(rustc_attrs, target_pointer_width = "32"), + rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE) +)] +#[cfg_attr( + all(rustc_attrs, target_pointer_width = "64"), + rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FF_FF_FF_FF_FE) +)] +pub struct BorrowedSocket<'socket> { + socket: RawSocket, + _phantom: PhantomData<&'socket OwnedSocket>, +} + +/// An owned file descriptor. +/// +/// This closes the file descriptor on drop. +/// +/// This uses `repr(transparent)` and has the representation of a host file +/// descriptor, so it can be used in FFI in places where a file descriptor is +/// passed as a consumed argument or returned as an owned value, and it never +/// has the value `-1`. +#[cfg(any(unix, target_os = "wasi"))] +#[repr(transparent)] +#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)] +#[cfg_attr(rustc_attrs, 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. +#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE))] +pub struct OwnedFd { + fd: RawFd, +} + +#[cfg(any(unix, target_os = "wasi"))] +impl OwnedFd { + /// Creates a new `OwnedFd` instance that shares the same underlying file + /// description as the existing `OwnedFd` instance. + pub fn try_clone(&self) -> std::io::Result<Self> { + crate::AsFd::as_fd(self).try_clone_to_owned() + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl BorrowedFd<'_> { + /// Creates a new `OwnedFd` instance that shares the same underlying file + /// description as the existing `BorrowedFd` instance. + pub fn try_clone_to_owned(&self) -> std::io::Result<OwnedFd> { + #[cfg(feature = "close")] + { + #[cfg(unix)] + { + // We want to atomically duplicate this file descriptor and set the + // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This + // is a POSIX flag that was added to Linux in 2.6.24. + #[cfg(not(target_os = "espidf"))] + let cmd = libc::F_DUPFD_CLOEXEC; + + // For ESP-IDF, F_DUPFD is used instead, because the CLOEXEC semantics + // will never be supported, as this is a bare metal framework with + // no capabilities for multi-process execution. While F_DUPFD is also + // not supported yet, it might be (currently it returns ENOSYS). + #[cfg(target_os = "espidf")] + let cmd = libc::F_DUPFD; + + let fd = match unsafe { libc::fcntl(self.as_raw_fd(), cmd, 0) } { + -1 => return Err(std::io::Error::last_os_error()), + fd => fd, + }; + + Ok(unsafe { OwnedFd::from_raw_fd(fd) }) + } + + #[cfg(target_os = "wasi")] + { + unreachable!("try_clone is not yet suppported on wasi"); + } + } + + // If the `close` feature is disabled, we expect users to avoid cloning + // `OwnedFd` instances, so that we don't have to call `fcntl`. + #[cfg(not(feature = "close"))] + { + unreachable!("try_clone called without the \"close\" feature in io-lifetimes"); + } + } +} + +/// An owned handle. +/// +/// This closes the handle on drop. +/// +/// Note that it *may* have the value `-1`, which in `OwnedHandle` always +/// represents a valid handle value, such as [the current process handle], and +/// not `INVALID_HANDLE_VALUE`, despite the two having the same value. See +/// [here] for the full story. +/// +/// And, it *may* have the value `NULL` (0), which can occur when consoles are +/// detached from processes, or when `windows_subsystem` is used. +/// +/// `OwnedHandle` uses [`CloseHandle`] to close its handle on drop. As such, +/// it must not be used with handles to open registry keys which need to be +/// closed with [`RegCloseKey`] instead. +/// +/// [`CloseHandle`]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle +/// [`RegCloseKey`]: https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regclosekey +/// +/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443 +/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks +#[cfg(windows)] +#[repr(transparent)] +pub struct OwnedHandle { + handle: RawHandle, +} + +#[cfg(windows)] +impl OwnedHandle { + /// Creates a new `OwnedHandle` instance that shares the same underlying + /// object as the existing `OwnedHandle` instance. + pub fn try_clone(&self) -> std::io::Result<Self> { + crate::AsHandle::as_handle(self).try_clone_to_owned() + } +} + +#[cfg(windows)] +impl BorrowedHandle<'_> { + /// Creates a new `OwnedHandle` instance that shares the same underlying + /// object as the existing `BorrowedHandle` instance. + pub fn try_clone_to_owned(&self) -> std::io::Result<OwnedHandle> { + #[cfg(feature = "close")] + { + self.duplicate(0, false, DUPLICATE_SAME_ACCESS) + } + + // If the `close` feature is disabled, we expect users to avoid cloning + // `OwnedHandle` instances, so that we don't have to call `fcntl`. + #[cfg(not(feature = "close"))] + { + unreachable!("try_clone called without the \"close\" feature in io-lifetimes"); + } + } + + #[cfg(feature = "close")] + pub(crate) fn duplicate( + &self, + access: u32, + inherit: bool, + options: DUPLICATE_HANDLE_OPTIONS, + ) -> std::io::Result<OwnedHandle> { + let mut ret = 0 as HANDLE; + match unsafe { + let cur_proc = GetCurrentProcess(); + DuplicateHandle( + cur_proc, + self.as_raw_handle() as HANDLE, + cur_proc, + &mut ret, + access, + inherit as BOOL, + options, + ) + } { + 0 => return Err(std::io::Error::last_os_error()), + _ => (), + } + unsafe { Ok(OwnedHandle::from_raw_handle(ret as RawHandle)) } + } +} + +/// An owned socket. +/// +/// This closes the socket on drop. +/// +/// This uses `repr(transparent)` and has the representation of a host socket, +/// so it can be used in FFI in places where a socket is passed as a consumed +/// argument or returned as an owned value, and it never has the value +/// [`INVALID_SOCKET`]. +#[cfg(windows)] +#[repr(transparent)] +#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)] +#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_start(0))] +// This is -2, in two's complement. -1 is `INVALID_SOCKET`. +#[cfg_attr( + all(rustc_attrs, target_pointer_width = "32"), + rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE) +)] +#[cfg_attr( + all(rustc_attrs, target_pointer_width = "64"), + rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FF_FF_FF_FF_FE) +)] +pub struct OwnedSocket { + socket: RawSocket, +} + +#[cfg(windows)] +impl OwnedSocket { + /// Creates a new `OwnedSocket` instance that shares the same underlying + /// object as the existing `OwnedSocket` instance. + pub fn try_clone(&self) -> std::io::Result<Self> { + crate::AsSocket::as_socket(self).try_clone_to_owned() + } + + #[cfg(feature = "close")] + #[cfg(not(target_vendor = "uwp"))] + fn set_no_inherit(&self) -> std::io::Result<()> { + match unsafe { + SetHandleInformation(self.as_raw_socket() as HANDLE, HANDLE_FLAG_INHERIT, 0) + } { + 0 => return Err(std::io::Error::last_os_error()), + _ => Ok(()), + } + } + + #[cfg(feature = "close")] + #[cfg(target_vendor = "uwp")] + fn set_no_inherit(&self) -> std::io::Result<()> { + Err(io::Error::new_const( + std::io::ErrorKind::Unsupported, + &"Unavailable on UWP", + )) + } +} + +#[cfg(windows)] +impl BorrowedSocket<'_> { + /// Creates a new `OwnedSocket` instance that shares the same underlying + /// object as the existing `BorrowedSocket` instance. + pub fn try_clone_to_owned(&self) -> std::io::Result<OwnedSocket> { + #[cfg(feature = "close")] + { + let mut info = unsafe { std::mem::zeroed::<WSAPROTOCOL_INFOW>() }; + let result = unsafe { + WSADuplicateSocketW(self.as_raw_socket() as _, GetCurrentProcessId(), &mut info) + }; + match result { + SOCKET_ERROR => return Err(std::io::Error::last_os_error()), + 0 => (), + _ => panic!(), + } + let socket = unsafe { + WSASocketW( + info.iAddressFamily, + info.iSocketType, + info.iProtocol, + &mut info, + 0, + WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT, + ) + }; + + if socket != INVALID_SOCKET { + unsafe { Ok(OwnedSocket::from_raw_socket(socket as _)) } + } else { + let error = unsafe { WSAGetLastError() }; + + if error != WSAEPROTOTYPE && error != WSAEINVAL { + return Err(std::io::Error::from_raw_os_error(error)); + } + + let socket = unsafe { + WSASocketW( + info.iAddressFamily, + info.iSocketType, + info.iProtocol, + &mut info, + 0, + WSA_FLAG_OVERLAPPED, + ) + }; + + if socket == INVALID_SOCKET { + return Err(std::io::Error::last_os_error()); + } + + unsafe { + let socket = OwnedSocket::from_raw_socket(socket as _); + socket.set_no_inherit()?; + Ok(socket) + } + } + } + + // If the `close` feature is disabled, we expect users to avoid cloning + // `OwnedSocket` instances, so that we don't have to call `fcntl`. + #[cfg(not(feature = "close"))] + { + unreachable!("try_clone called without the \"close\" feature in io-lifetimes"); + } + } +} + +/// FFI type for handles in return values or out parameters, where `INVALID_HANDLE_VALUE` is used +/// as a sentry value to indicate errors, such as in the return value of `CreateFileW`. This uses +/// `repr(transparent)` and has the representation of a host handle, so that it can be used in such +/// FFI declarations. +/// +/// The only thing you can usefully do with a `HandleOrInvalid` is to convert it into an +/// `OwnedHandle` using its [`TryFrom`] implementation; this conversion takes care of the check for +/// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without +/// checking for `INVALID_HANDLE_VALUE` first. +/// +/// This type may hold any handle value that [`OwnedHandle`] may hold, except that when it holds +/// `-1`, that value is interpreted to mean `INVALID_HANDLE_VALUE`. +/// +/// If holds a handle other than `INVALID_HANDLE_VALUE`, it will close the handle on drop. +#[cfg(windows)] +#[repr(transparent)] +#[derive(Debug)] +pub struct HandleOrInvalid(RawHandle); + +/// FFI type for handles in return values or out parameters, where `NULL` is used +/// as a sentry value to indicate errors, such as in the return value of `CreateThread`. This uses +/// `repr(transparent)` and has the representation of a host handle, so that it can be used in such +/// FFI declarations. +/// +/// The only thing you can usefully do with a `HandleOrNull` is to convert it into an +/// `OwnedHandle` using its [`TryFrom`] implementation; this conversion takes care of the check for +/// `NULL`. This ensures that such FFI calls cannot start using the handle without +/// checking for `NULL` first. +/// +/// This type may hold any handle value that [`OwnedHandle`] may hold. As with `OwnedHandle`, when +/// it holds `-1`, that value is interpreted as a valid handle value, such as +/// [the current process handle], and not `INVALID_HANDLE_VALUE`. +/// +/// If this holds a non-null handle, it will close the handle on drop. +/// +/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks +#[cfg(windows)] +#[repr(transparent)] +#[derive(Debug)] +pub struct HandleOrNull(RawHandle); + +// The Windows [`HANDLE`] type may be transferred across and shared between +// thread boundaries (despite containing a `*mut void`, which in general isn't +// `Send` or `Sync`). +// +// [`HANDLE`]: std::os::windows::raw::HANDLE +#[cfg(windows)] +unsafe impl Send for OwnedHandle {} +#[cfg(windows)] +unsafe impl Send for HandleOrInvalid {} +#[cfg(windows)] +unsafe impl Send for HandleOrNull {} +#[cfg(windows)] +unsafe impl Send for BorrowedHandle<'_> {} +#[cfg(windows)] +unsafe impl Sync for OwnedHandle {} +#[cfg(windows)] +unsafe impl Sync for HandleOrInvalid {} +#[cfg(windows)] +unsafe impl Sync for HandleOrNull {} +#[cfg(windows)] +unsafe impl Sync for BorrowedHandle<'_> {} + +#[cfg(any(unix, target_os = "wasi"))] +impl BorrowedFd<'_> { + /// Return a `BorrowedFd` holding the given raw file descriptor. + /// + /// # Safety + /// + /// The resource pointed to by `raw` must remain open for the duration of + /// the returned `BorrowedFd`, and it must not have the value `-1`. + #[inline] + pub const unsafe fn borrow_raw(fd: RawFd) -> Self { + #[cfg(panic_in_const_fn)] + debug_assert!(fd != -1_i32 as RawFd); + + Self { + fd, + _phantom: PhantomData, + } + } +} + +#[cfg(windows)] +impl BorrowedHandle<'_> { + /// Return a `BorrowedHandle` holding the given raw handle. + /// + /// # Safety + /// + /// The resource pointed to by `handle` must be a valid open handle, it + /// must remain open for the duration of the returned `BorrowedHandle`. + /// + /// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is + /// sometimes a valid handle value. See [here] for the full story. + /// + /// And, it *may* have the value `NULL` (0), which can occur when consoles are + /// detached from processes, or when `windows_subsystem` is used. + /// + /// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443 + #[inline] + pub const unsafe fn borrow_raw(handle: RawHandle) -> Self { + Self { + handle, + _phantom: PhantomData, + } + } +} + +#[cfg(windows)] +impl BorrowedSocket<'_> { + /// Return a `BorrowedSocket` holding the given raw socket. + /// + /// # Safety + /// + /// The resource pointed to by `raw` must remain open for the duration of + /// the returned `BorrowedSocket`, and it must not have the value + /// [`INVALID_SOCKET`]. + #[inline] + pub const unsafe fn borrow_raw(socket: RawSocket) -> Self { + #[cfg(panic_in_const_fn)] + debug_assert!(socket != INVALID_SOCKET as RawSocket); + Self { + socket, + _phantom: PhantomData, + } + } +} + +#[cfg(windows)] +impl TryFrom<HandleOrInvalid> for OwnedHandle { + type Error = InvalidHandleError; + + #[inline] + fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> { + let raw = handle_or_invalid.0; + if raw as HANDLE == INVALID_HANDLE_VALUE { + // Don't call `CloseHandle`; it'd be harmless, except that it could + // overwrite the `GetLastError` error. + forget(handle_or_invalid); + + Err(InvalidHandleError(())) + } else { + Ok(OwnedHandle { handle: raw }) + } + } +} + +#[cfg(windows)] +impl TryFrom<HandleOrNull> for OwnedHandle { + type Error = NullHandleError; + + #[inline] + fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> { + let raw = handle_or_null.0; + if raw.is_null() { + // Don't call `CloseHandle`; it'd be harmless, except that it could + // overwrite the `GetLastError` error. + forget(handle_or_null); + + Err(NullHandleError(())) + } else { + Ok(OwnedHandle { handle: raw }) + } + } +} + +/// This is the error type used by [`HandleOrNull`] when attempting to convert +/// into a handle, to indicate that the value is null. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NullHandleError(()); + +impl fmt::Display for NullHandleError { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + "A HandleOrNull could not be converted to a handle because it was null".fmt(fmt) + } +} + +impl std::error::Error for NullHandleError {} + +/// This is the error type used by [`HandleOrInvalid`] when attempting to +/// convert into a handle, to indicate that the value is +/// `INVALID_HANDLE_VALUE`. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct InvalidHandleError(()); + +impl fmt::Display for InvalidHandleError { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + "A HandleOrInvalid could not be converted to a handle because it was INVALID_HANDLE_VALUE" + .fmt(fmt) + } +} + +impl std::error::Error for InvalidHandleError {} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsRawFd for BorrowedFd<'_> { + #[inline] + fn as_raw_fd(&self) -> RawFd { + self.fd + } +} + +#[cfg(windows)] +impl AsRawHandle for BorrowedHandle<'_> { + #[inline] + fn as_raw_handle(&self) -> RawHandle { + self.handle + } +} + +#[cfg(windows)] +impl AsRawSocket for BorrowedSocket<'_> { + #[inline] + fn as_raw_socket(&self) -> RawSocket { + self.socket + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl AsRawFd for OwnedFd { + #[inline] + fn as_raw_fd(&self) -> RawFd { + self.fd + } +} + +#[cfg(windows)] +impl AsRawHandle for OwnedHandle { + #[inline] + fn as_raw_handle(&self) -> RawHandle { + self.handle + } +} + +#[cfg(windows)] +impl AsRawSocket for OwnedSocket { + #[inline] + fn as_raw_socket(&self) -> RawSocket { + self.socket + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl IntoRawFd for OwnedFd { + #[inline] + fn into_raw_fd(self) -> RawFd { + let fd = self.fd; + forget(self); + fd + } +} + +#[cfg(windows)] +impl IntoRawHandle for OwnedHandle { + #[inline] + fn into_raw_handle(self) -> RawHandle { + let handle = self.handle; + forget(self); + handle + } +} + +#[cfg(windows)] +impl IntoRawSocket for OwnedSocket { + #[inline] + fn into_raw_socket(self) -> RawSocket { + let socket = self.socket; + forget(self); + socket + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl FromRawFd for OwnedFd { + /// Constructs a new instance of `Self` from the given raw file descriptor. + /// + /// # Safety + /// + /// The resource pointed to by `raw` must be open and suitable for assuming + /// ownership. + #[inline] + unsafe fn from_raw_fd(fd: RawFd) -> Self { + debug_assert_ne!(fd, -1_i32 as RawFd); + Self { fd } + } +} + +#[cfg(windows)] +impl FromRawHandle for OwnedHandle { + #[inline] + unsafe fn from_raw_handle(handle: RawHandle) -> Self { + Self { handle } + } +} + +#[cfg(windows)] +impl FromRawSocket for OwnedSocket { + #[inline] + unsafe fn from_raw_socket(socket: RawSocket) -> Self { + debug_assert_ne!(socket, INVALID_SOCKET as RawSocket); + Self { socket } + } +} + +#[cfg(windows)] +impl HandleOrInvalid { + /// Constructs a new instance of `Self` from the given `RawHandle` returned + /// from a Windows API that uses `INVALID_HANDLE_VALUE` to indicate + /// failure, such as `CreateFileW`. + /// + /// Use `HandleOrNull` instead of `HandleOrInvalid` for APIs that + /// use null to indicate failure. + /// + /// # Safety + /// + /// The passed `handle` value must either satisfy the safety requirements + /// of [`FromRawHandle::from_raw_handle`], or be + /// `INVALID_HANDLE_VALUE` (-1). 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] + pub unsafe fn from_raw_handle(handle: RawHandle) -> Self { + Self(handle) + } +} + +#[cfg(windows)] +impl HandleOrNull { + /// Constructs a new instance of `Self` from the given `RawHandle` returned + /// from a Windows API that uses null to indicate failure, such as + /// `CreateThread`. + /// + /// Use `HandleOrInvalid` instead of `HandleOrNull` for APIs that + /// use `INVALID_HANDLE_VALUE` to indicate failure. + /// + /// # Safety + /// + /// The passed `handle` value must either satisfy the safety requirements + /// of [`FromRawHandle::from_raw_handle`], or be null. Note that not all + /// Windows APIs use null for errors; see [here] for the full story. + /// + /// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443 + #[inline] + pub unsafe fn from_raw_handle(handle: RawHandle) -> Self { + Self(handle) + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl Drop for OwnedFd { + #[inline] + fn drop(&mut self) { + #[cfg(feature = "close")] + unsafe { + let _ = libc::close(self.fd as std::os::raw::c_int); + } + + // If the `close` feature is disabled, we expect users to avoid letting + // `OwnedFd` instances drop, so that we don't have to call `close`. + #[cfg(not(feature = "close"))] + { + unreachable!("drop called without the \"close\" feature in io-lifetimes"); + } + } +} + +#[cfg(windows)] +impl Drop for OwnedHandle { + #[inline] + fn drop(&mut self) { + #[cfg(feature = "close")] + unsafe { + let _ = CloseHandle(self.handle as HANDLE); + } + + // If the `close` feature is disabled, we expect users to avoid letting + // `OwnedHandle` instances drop, so that we don't have to call `close`. + #[cfg(not(feature = "close"))] + { + unreachable!("drop called without the \"close\" feature in io-lifetimes"); + } + } +} + +#[cfg(windows)] +impl Drop for HandleOrInvalid { + #[inline] + fn drop(&mut self) { + #[cfg(feature = "close")] + unsafe { + let _ = CloseHandle(self.0 as HANDLE); + } + + // If the `close` feature is disabled, we expect users to avoid letting + // `HandleOrInvalid` instances drop, so that we don't have to call `close`. + #[cfg(not(feature = "close"))] + { + unreachable!("drop called without the \"close\" feature in io-lifetimes"); + } + } +} + +#[cfg(windows)] +impl Drop for HandleOrNull { + #[inline] + fn drop(&mut self) { + #[cfg(feature = "close")] + unsafe { + let _ = CloseHandle(self.0 as HANDLE); + } + + // If the `close` feature is disabled, we expect users to avoid letting + // `HandleOrNull` instances drop, so that we don't have to call `close`. + #[cfg(not(feature = "close"))] + { + unreachable!("drop called without the \"close\" feature in io-lifetimes"); + } + } +} + +#[cfg(windows)] +impl Drop for OwnedSocket { + #[inline] + fn drop(&mut self) { + #[cfg(feature = "close")] + unsafe { + let _ = closesocket(self.socket as SOCKET); + } + + // If the `close` feature is disabled, we expect users to avoid letting + // `OwnedSocket` instances drop, so that we don't have to call `close`. + #[cfg(not(feature = "close"))] + { + unreachable!("drop called without the \"close\" feature in io-lifetimes"); + } + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl fmt::Debug for BorrowedFd<'_> { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("BorrowedFd").field("fd", &self.fd).finish() + } +} + +#[cfg(windows)] +impl fmt::Debug for BorrowedHandle<'_> { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("BorrowedHandle") + .field("handle", &self.handle) + .finish() + } +} + +#[cfg(windows)] +impl fmt::Debug for BorrowedSocket<'_> { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("BorrowedSocket") + .field("socket", &self.socket) + .finish() + } +} + +#[cfg(any(unix, target_os = "wasi"))] +impl fmt::Debug for OwnedFd { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("OwnedFd").field("fd", &self.fd).finish() + } +} + +#[cfg(windows)] +impl fmt::Debug for OwnedHandle { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("OwnedHandle") + .field("handle", &self.handle) + .finish() + } +} + +#[cfg(windows)] +impl fmt::Debug for OwnedSocket { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("OwnedSocket") + .field("socket", &self.socket) + .finish() + } +} diff --git a/vendor/io-lifetimes/src/views.rs b/vendor/io-lifetimes/src/views.rs new file mode 100644 index 000000000..aec3750a0 --- /dev/null +++ b/vendor/io-lifetimes/src/views.rs @@ -0,0 +1,204 @@ +//! Typed views using temporary objects. +//! +//! This module defines the return types for [`AsFilelike::as_filelike_view`] +//! and [`AsSocketlike::as_socketlike_view`]. +//! +//! [`AsSocketlike::as_socketlike_view`]: crate::AsSocketlike::as_socketlike_view + +use crate::raw::{ + AsRawFilelike, AsRawSocketlike, FromRawFilelike, FromRawSocketlike, IntoRawFilelike, + IntoRawSocketlike, RawFilelike, RawSocketlike, +}; +use crate::{ + AsFilelike, AsSocketlike, FromFilelike, FromSocketlike, IntoFilelike, IntoSocketlike, + OwnedFilelike, OwnedSocketlike, +}; +use std::fmt; +use std::marker::PhantomData; +use std::mem::ManuallyDrop; +use std::ops::Deref; + +/// Declare that a type is safe to use in a [`FilelikeView`]. +/// +/// # Safety +/// +/// Types implementing this trait declare that if they are constructed with +/// [`FromFilelike`] and consumed with [`IntoFilelike`], their `IntoFilelike` +/// will return the same `OwnedFd` value that was passed to their +/// `FromFilelike`. +pub unsafe trait FilelikeViewType: FromFilelike + IntoFilelike {} + +/// Declare that a type is safe to use in a [`SocketlikeView`]. +/// +/// # Safety +/// +/// Types implementing this trait declare that if they are constructed with +/// [`FromSocketlike`] and consumed with [`IntoSocketlike`], their +/// `IntoSocketlike` will return the same `OwnedFd` value that was passed to +/// their `FromSocketlike`. +pub unsafe trait SocketlikeViewType: FromSocketlike + IntoSocketlike {} + +/// A non-owning view of a resource which dereferences to a `&Target` or +/// `&mut Target`. These are returned by [`AsFilelike::as_filelike_view`]. +pub struct FilelikeView<'filelike, Target: FilelikeViewType> { + /// The value to dereference to. This is a `ManuallyDrop` so that we can + /// consume it in our `Drop` impl. + target: ManuallyDrop<Target>, + + /// `FilelikeViewType` implementors guarantee that their `Into<OwnedFd>` + /// returns the same fd as their `From<OwnedFd>` gave them. This field + /// allows us to verify this. + #[cfg(debug_assertions)] + orig: RawFilelike, + + /// This field exists because we don't otherwise explicitly use + /// `'filelike`. + _phantom: PhantomData<&'filelike OwnedFilelike>, +} + +/// A non-owning view of a resource which dereferences to a `&Target` or +/// `&mut Target`. These are returned by [`AsSocketlike::as_socketlike_view`]. +pub struct SocketlikeView<'socketlike, Target: SocketlikeViewType> { + /// The value to dereference to. This is a `ManuallyDrop` so that we can + /// consume it in our `Drop` impl. + target: ManuallyDrop<Target>, + + /// `SocketlikeViewType` implementors guarantee that their `Into<OwnedFd>` + /// returns the same fd as their `From<OwnedFd>` gave them. This field + /// allows us to verify this. + #[cfg(debug_assertions)] + orig: RawSocketlike, + + /// This field exists because we don't otherwise explicitly use + /// `'socketlike`. + _phantom: PhantomData<&'socketlike OwnedSocketlike>, +} + +impl<Target: FilelikeViewType> FilelikeView<'_, Target> { + /// Construct a temporary `Target` and wrap it in a `FilelikeView` object. + #[inline] + pub(crate) fn new<T: AsFilelike>(filelike: &T) -> Self { + // Safety: The returned `FilelikeView` is scoped to the lifetime of + // `filelike`, which we've borrowed here, so the view won't outlive + // the object it's borrowed from. + unsafe { Self::view_raw(filelike.as_filelike().as_raw_filelike()) } + } + + /// Construct a temporary `Target` from raw and wrap it in a `FilelikeView` + /// object. + /// + /// # Safety + /// + /// `raw` must be a valid raw filelike referencing a resource that outlives + /// the resulting view. + #[inline] + pub unsafe fn view_raw(raw: RawFilelike) -> Self { + let owned = OwnedFilelike::from_raw_filelike(raw); + Self { + target: ManuallyDrop::new(Target::from_filelike(owned)), + #[cfg(debug_assertions)] + orig: raw, + _phantom: PhantomData, + } + } +} + +impl<Target: SocketlikeViewType> SocketlikeView<'_, Target> { + /// Construct a temporary `Target` and wrap it in a `SocketlikeView` + /// object. + #[inline] + pub(crate) fn new<T: AsSocketlike>(socketlike: &T) -> Self { + // Safety: The returned `SocketlikeView` is scoped to the lifetime of + // `socketlike`, which we've borrowed here, so the view won't outlive + // the object it's borrowed from. + unsafe { Self::view_raw(socketlike.as_socketlike().as_raw_socketlike()) } + } + + /// Construct a temporary `Target` from raw and wrap it in a + /// `SocketlikeView` object. + /// + /// # Safety + /// + /// `raw` must be a valid raw socketlike referencing a resource that + /// outlives the resulting view. + #[inline] + pub unsafe fn view_raw(raw: RawSocketlike) -> Self { + let owned = OwnedSocketlike::from_raw_socketlike(raw); + Self { + target: ManuallyDrop::new(Target::from_socketlike(owned)), + #[cfg(debug_assertions)] + orig: raw, + _phantom: PhantomData, + } + } +} + +impl<Target: FilelikeViewType> Deref for FilelikeView<'_, Target> { + type Target = Target; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.target + } +} + +impl<Target: SocketlikeViewType> Deref for SocketlikeView<'_, Target> { + type Target = Target; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.target + } +} + +impl<Target: FilelikeViewType> Drop for FilelikeView<'_, Target> { + #[inline] + fn drop(&mut self) { + // Use `Into*` to consume `self.target` without freeing its resource. + // + // Safety: Using `ManuallyDrop::take` requires us to ensure that + // `self.target` is not used again. We don't use it again here, and + // this is the `drop` function, so we know it's not used afterward. + let _raw = unsafe { ManuallyDrop::take(&mut self.target) } + .into_filelike() + .into_raw_filelike(); + + #[cfg(debug_assertions)] + debug_assert_eq!(self.orig, _raw); + } +} + +impl<Target: SocketlikeViewType> Drop for SocketlikeView<'_, Target> { + #[inline] + fn drop(&mut self) { + // Use `Into*` to consume `self.target` without freeing its resource. + // + // Safety: Using `ManuallyDrop::take` requires us to ensure that + // `self.target` is not used again. We don't use it again here, and + // this is the `drop` function, so we know it's not used afterward. + let _raw = unsafe { ManuallyDrop::take(&mut self.target) } + .into_socketlike() + .into_raw_socketlike(); + + #[cfg(debug_assertions)] + debug_assert_eq!(self.orig, _raw); + } +} + +impl<Target: FilelikeViewType> fmt::Debug for FilelikeView<'_, Target> { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FilelikeView") + .field("target", &*self) + .finish() + } +} + +impl<Target: SocketlikeViewType> fmt::Debug for SocketlikeView<'_, Target> { + #[allow(clippy::missing_inline_in_public_items)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("SocketlikeView") + .field("target", &*self) + .finish() + } +} |