11a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
21a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
31a0216d1Sopenharmony_ciuse crate::BorrowedFd;
41a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
51a0216d1Sopenharmony_ciuse crate::OwnedFd;
61a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
71a0216d1Sopenharmony_ci#[cfg(windows)]
81a0216d1Sopenharmony_ciuse crate::{BorrowedHandle, BorrowedSocket};
91a0216d1Sopenharmony_ci#[cfg(windows)]
101a0216d1Sopenharmony_ciuse crate::{OwnedHandle, OwnedSocket};
111a0216d1Sopenharmony_ci
121a0216d1Sopenharmony_ci/// A trait to borrow the file descriptor from an underlying object.
131a0216d1Sopenharmony_ci///
141a0216d1Sopenharmony_ci/// This is only available on unix platforms and must be imported in order to
151a0216d1Sopenharmony_ci/// call the method. Windows platforms have a corresponding `AsHandle` and
161a0216d1Sopenharmony_ci/// `AsSocket` set of traits.
171a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
181a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
191a0216d1Sopenharmony_cipub trait AsFd {
201a0216d1Sopenharmony_ci    /// Borrows the file descriptor.
211a0216d1Sopenharmony_ci    ///
221a0216d1Sopenharmony_ci    /// # Example
231a0216d1Sopenharmony_ci    ///
241a0216d1Sopenharmony_ci    /// ```rust,no_run
251a0216d1Sopenharmony_ci    /// use std::fs::File;
261a0216d1Sopenharmony_ci    /// # use std::io;
271a0216d1Sopenharmony_ci    /// use io_lifetimes::{AsFd, BorrowedFd};
281a0216d1Sopenharmony_ci    ///
291a0216d1Sopenharmony_ci    /// let mut f = File::open("foo.txt")?;
301a0216d1Sopenharmony_ci    /// let borrowed_fd: BorrowedFd<'_> = f.as_fd();
311a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
321a0216d1Sopenharmony_ci    /// ```
331a0216d1Sopenharmony_ci    fn as_fd(&self) -> BorrowedFd<'_>;
341a0216d1Sopenharmony_ci}
351a0216d1Sopenharmony_ci
361a0216d1Sopenharmony_ci/// A trait to borrow the handle from an underlying object.
371a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
381a0216d1Sopenharmony_ci#[cfg(windows)]
391a0216d1Sopenharmony_cipub trait AsHandle {
401a0216d1Sopenharmony_ci    /// Borrows the handle.
411a0216d1Sopenharmony_ci    ///
421a0216d1Sopenharmony_ci    /// # Example
431a0216d1Sopenharmony_ci    ///
441a0216d1Sopenharmony_ci    /// ```rust,no_run
451a0216d1Sopenharmony_ci    /// use std::fs::File;
461a0216d1Sopenharmony_ci    /// # use std::io;
471a0216d1Sopenharmony_ci    /// use io_lifetimes::{AsHandle, BorrowedHandle};
481a0216d1Sopenharmony_ci    ///
491a0216d1Sopenharmony_ci    /// let mut f = File::open("foo.txt")?;
501a0216d1Sopenharmony_ci    /// let borrowed_handle: BorrowedHandle<'_> = f.as_handle();
511a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
521a0216d1Sopenharmony_ci    /// ```
531a0216d1Sopenharmony_ci    fn as_handle(&self) -> BorrowedHandle<'_>;
541a0216d1Sopenharmony_ci}
551a0216d1Sopenharmony_ci
561a0216d1Sopenharmony_ci/// A trait to borrow the socket from an underlying object.
571a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
581a0216d1Sopenharmony_ci#[cfg(windows)]
591a0216d1Sopenharmony_cipub trait AsSocket {
601a0216d1Sopenharmony_ci    /// Borrows the socket.
611a0216d1Sopenharmony_ci    fn as_socket(&self) -> BorrowedSocket<'_>;
621a0216d1Sopenharmony_ci}
631a0216d1Sopenharmony_ci
641a0216d1Sopenharmony_ci/// A trait to express the ability to consume an object and acquire ownership
651a0216d1Sopenharmony_ci/// of its file descriptor.
661a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
671a0216d1Sopenharmony_ci#[deprecated(
681a0216d1Sopenharmony_ci    since = "1.0.0",
691a0216d1Sopenharmony_ci    note = "`IntoFd` is replaced by `From<...> for OwnedFd` or `Into<OwnedFd>`"
701a0216d1Sopenharmony_ci)]
711a0216d1Sopenharmony_cipub trait IntoFd {
721a0216d1Sopenharmony_ci    /// Consumes this object, returning the underlying file descriptor.
731a0216d1Sopenharmony_ci    ///
741a0216d1Sopenharmony_ci    /// # Example
751a0216d1Sopenharmony_ci    ///
761a0216d1Sopenharmony_ci    /// ```rust,no_run
771a0216d1Sopenharmony_ci    /// use std::fs::File;
781a0216d1Sopenharmony_ci    /// # use std::io;
791a0216d1Sopenharmony_ci    /// use io_lifetimes::{IntoFd, OwnedFd};
801a0216d1Sopenharmony_ci    ///
811a0216d1Sopenharmony_ci    /// let f = File::open("foo.txt")?;
821a0216d1Sopenharmony_ci    /// let owned_fd: OwnedFd = f.into_fd();
831a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
841a0216d1Sopenharmony_ci    /// ```
851a0216d1Sopenharmony_ci    fn into_fd(self) -> OwnedFd;
861a0216d1Sopenharmony_ci}
871a0216d1Sopenharmony_ci
881a0216d1Sopenharmony_ci/// A trait to express the ability to consume an object and acquire ownership
891a0216d1Sopenharmony_ci/// of its handle.
901a0216d1Sopenharmony_ci#[cfg(windows)]
911a0216d1Sopenharmony_ci#[deprecated(
921a0216d1Sopenharmony_ci    since = "1.0.0",
931a0216d1Sopenharmony_ci    note = "`IntoHandle` is replaced by `From<...> for OwnedHandle` or `Into<OwnedHandle>`"
941a0216d1Sopenharmony_ci)]
951a0216d1Sopenharmony_cipub trait IntoHandle {
961a0216d1Sopenharmony_ci    /// Consumes this object, returning the underlying handle.
971a0216d1Sopenharmony_ci    ///
981a0216d1Sopenharmony_ci    /// # Example
991a0216d1Sopenharmony_ci    ///
1001a0216d1Sopenharmony_ci    /// ```rust,no_run
1011a0216d1Sopenharmony_ci    /// use std::fs::File;
1021a0216d1Sopenharmony_ci    /// # use std::io;
1031a0216d1Sopenharmony_ci    /// use io_lifetimes::{IntoHandle, OwnedHandle};
1041a0216d1Sopenharmony_ci    ///
1051a0216d1Sopenharmony_ci    /// let f = File::open("foo.txt")?;
1061a0216d1Sopenharmony_ci    /// let owned_handle: OwnedHandle = f.into_handle();
1071a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
1081a0216d1Sopenharmony_ci    /// ```
1091a0216d1Sopenharmony_ci    fn into_handle(self) -> OwnedHandle;
1101a0216d1Sopenharmony_ci}
1111a0216d1Sopenharmony_ci
1121a0216d1Sopenharmony_ci/// A trait to express the ability to consume an object and acquire ownership
1131a0216d1Sopenharmony_ci/// of its socket.
1141a0216d1Sopenharmony_ci#[cfg(windows)]
1151a0216d1Sopenharmony_ci#[deprecated(
1161a0216d1Sopenharmony_ci    since = "1.0.0",
1171a0216d1Sopenharmony_ci    note = "`IntoSocket` is replaced by `From<...> for OwnedSocket` or `Into<OwnedSocket>`"
1181a0216d1Sopenharmony_ci)]
1191a0216d1Sopenharmony_cipub trait IntoSocket {
1201a0216d1Sopenharmony_ci    /// Consumes this object, returning the underlying socket.
1211a0216d1Sopenharmony_ci    fn into_socket(self) -> OwnedSocket;
1221a0216d1Sopenharmony_ci}
1231a0216d1Sopenharmony_ci
1241a0216d1Sopenharmony_ci/// A trait to express the ability to construct an object from a file
1251a0216d1Sopenharmony_ci/// descriptor.
1261a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
1271a0216d1Sopenharmony_cipub trait FromFd {
1281a0216d1Sopenharmony_ci    /// Constructs a new instance of `Self` from the given file descriptor.
1291a0216d1Sopenharmony_ci    ///
1301a0216d1Sopenharmony_ci    /// # Example
1311a0216d1Sopenharmony_ci    ///
1321a0216d1Sopenharmony_ci    /// ```rust,no_run
1331a0216d1Sopenharmony_ci    /// use std::fs::File;
1341a0216d1Sopenharmony_ci    /// # use std::io;
1351a0216d1Sopenharmony_ci    /// use io_lifetimes::{FromFd, IntoFd, OwnedFd};
1361a0216d1Sopenharmony_ci    ///
1371a0216d1Sopenharmony_ci    /// let f = File::open("foo.txt")?;
1381a0216d1Sopenharmony_ci    /// let owned_fd: OwnedFd = f.into_fd();
1391a0216d1Sopenharmony_ci    /// let f = File::from_fd(owned_fd);
1401a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
1411a0216d1Sopenharmony_ci    /// ```
1421a0216d1Sopenharmony_ci    #[deprecated(
1431a0216d1Sopenharmony_ci        since = "1.0.0",
1441a0216d1Sopenharmony_ci        note = "`FromFd::from_fd` is replaced by `From<OwnedFd>::from`"
1451a0216d1Sopenharmony_ci    )]
1461a0216d1Sopenharmony_ci    fn from_fd(owned: OwnedFd) -> Self;
1471a0216d1Sopenharmony_ci
1481a0216d1Sopenharmony_ci    /// Constructs a new instance of `Self` from the given file descriptor
1491a0216d1Sopenharmony_ci    /// converted from `into_owned`.
1501a0216d1Sopenharmony_ci    ///
1511a0216d1Sopenharmony_ci    /// # Example
1521a0216d1Sopenharmony_ci    ///
1531a0216d1Sopenharmony_ci    /// ```rust,no_run
1541a0216d1Sopenharmony_ci    /// use std::fs::File;
1551a0216d1Sopenharmony_ci    /// # use std::io;
1561a0216d1Sopenharmony_ci    /// use io_lifetimes::{FromFd, IntoFd};
1571a0216d1Sopenharmony_ci    ///
1581a0216d1Sopenharmony_ci    /// let f = File::open("foo.txt")?;
1591a0216d1Sopenharmony_ci    /// let f = File::from_into_fd(f);
1601a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
1611a0216d1Sopenharmony_ci    /// ```
1621a0216d1Sopenharmony_ci    #[inline]
1631a0216d1Sopenharmony_ci    fn from_into_fd<Owned: Into<OwnedFd>>(into_owned: Owned) -> Self
1641a0216d1Sopenharmony_ci    where
1651a0216d1Sopenharmony_ci        Self: Sized + From<OwnedFd>,
1661a0216d1Sopenharmony_ci    {
1671a0216d1Sopenharmony_ci        Self::from(into_owned.into())
1681a0216d1Sopenharmony_ci    }
1691a0216d1Sopenharmony_ci}
1701a0216d1Sopenharmony_ci
1711a0216d1Sopenharmony_ci/// A trait to express the ability to construct an object from a handle.
1721a0216d1Sopenharmony_ci#[cfg(windows)]
1731a0216d1Sopenharmony_cipub trait FromHandle {
1741a0216d1Sopenharmony_ci    /// Constructs a new instance of `Self` from the given handle.
1751a0216d1Sopenharmony_ci    ///
1761a0216d1Sopenharmony_ci    /// # Example
1771a0216d1Sopenharmony_ci    ///
1781a0216d1Sopenharmony_ci    /// ```rust,no_run
1791a0216d1Sopenharmony_ci    /// use std::fs::File;
1801a0216d1Sopenharmony_ci    /// # use std::io;
1811a0216d1Sopenharmony_ci    /// use io_lifetimes::{FromHandle, IntoHandle, OwnedHandle};
1821a0216d1Sopenharmony_ci    ///
1831a0216d1Sopenharmony_ci    /// let f = File::open("foo.txt")?;
1841a0216d1Sopenharmony_ci    /// let owned_handle: OwnedHandle = f.into_handle();
1851a0216d1Sopenharmony_ci    /// let f = File::from_handle(owned_handle);
1861a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
1871a0216d1Sopenharmony_ci    /// ```
1881a0216d1Sopenharmony_ci    #[deprecated(
1891a0216d1Sopenharmony_ci        since = "1.0.0",
1901a0216d1Sopenharmony_ci        note = "`FromHandle::from_handle` is replaced by `From<OwnedHandle>::from`"
1911a0216d1Sopenharmony_ci    )]
1921a0216d1Sopenharmony_ci    fn from_handle(owned: OwnedHandle) -> Self;
1931a0216d1Sopenharmony_ci
1941a0216d1Sopenharmony_ci    /// Constructs a new instance of `Self` from the given handle converted
1951a0216d1Sopenharmony_ci    /// from `into_owned`.
1961a0216d1Sopenharmony_ci    ///
1971a0216d1Sopenharmony_ci    /// # Example
1981a0216d1Sopenharmony_ci    ///
1991a0216d1Sopenharmony_ci    /// ```rust,no_run
2001a0216d1Sopenharmony_ci    /// use std::fs::File;
2011a0216d1Sopenharmony_ci    /// # use std::io;
2021a0216d1Sopenharmony_ci    /// use io_lifetimes::{FromHandle, IntoHandle};
2031a0216d1Sopenharmony_ci    ///
2041a0216d1Sopenharmony_ci    /// let f = File::open("foo.txt")?;
2051a0216d1Sopenharmony_ci    /// let f = File::from_into_handle(f);
2061a0216d1Sopenharmony_ci    /// # Ok::<(), io::Error>(())
2071a0216d1Sopenharmony_ci    /// ```
2081a0216d1Sopenharmony_ci    #[inline]
2091a0216d1Sopenharmony_ci    fn from_into_handle<Owned: Into<OwnedHandle>>(into_owned: Owned) -> Self
2101a0216d1Sopenharmony_ci    where
2111a0216d1Sopenharmony_ci        Self: Sized + From<OwnedHandle>,
2121a0216d1Sopenharmony_ci    {
2131a0216d1Sopenharmony_ci        Self::from(into_owned.into())
2141a0216d1Sopenharmony_ci    }
2151a0216d1Sopenharmony_ci}
2161a0216d1Sopenharmony_ci
2171a0216d1Sopenharmony_ci/// A trait to express the ability to construct an object from a socket.
2181a0216d1Sopenharmony_ci#[cfg(windows)]
2191a0216d1Sopenharmony_cipub trait FromSocket {
2201a0216d1Sopenharmony_ci    /// Constructs a new instance of `Self` from the given socket.
2211a0216d1Sopenharmony_ci    #[deprecated(
2221a0216d1Sopenharmony_ci        since = "1.0.0",
2231a0216d1Sopenharmony_ci        note = "`FromSocket::from_socket` is replaced by `From<OwnedSocket>::from`"
2241a0216d1Sopenharmony_ci    )]
2251a0216d1Sopenharmony_ci    fn from_socket(owned: OwnedSocket) -> Self;
2261a0216d1Sopenharmony_ci
2271a0216d1Sopenharmony_ci    /// Constructs a new instance of `Self` from the given socket converted
2281a0216d1Sopenharmony_ci    /// from `into_owned`.
2291a0216d1Sopenharmony_ci    #[inline]
2301a0216d1Sopenharmony_ci    fn from_into_socket<Owned: Into<OwnedSocket>>(into_owned: Owned) -> Self
2311a0216d1Sopenharmony_ci    where
2321a0216d1Sopenharmony_ci        Self: Sized + From<OwnedSocket>,
2331a0216d1Sopenharmony_ci    {
2341a0216d1Sopenharmony_ci        Self::from(into_owned.into())
2351a0216d1Sopenharmony_ci    }
2361a0216d1Sopenharmony_ci}
2371a0216d1Sopenharmony_ci
2381a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
2391a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
2401a0216d1Sopenharmony_ciimpl<T: AsFd> AsFd for &T {
2411a0216d1Sopenharmony_ci    #[inline]
2421a0216d1Sopenharmony_ci    fn as_fd(&self) -> BorrowedFd<'_> {
2431a0216d1Sopenharmony_ci        T::as_fd(self)
2441a0216d1Sopenharmony_ci    }
2451a0216d1Sopenharmony_ci}
2461a0216d1Sopenharmony_ci
2471a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
2481a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
2491a0216d1Sopenharmony_ciimpl<T: AsFd> AsFd for &mut T {
2501a0216d1Sopenharmony_ci    #[inline]
2511a0216d1Sopenharmony_ci    fn as_fd(&self) -> BorrowedFd<'_> {
2521a0216d1Sopenharmony_ci        T::as_fd(self)
2531a0216d1Sopenharmony_ci    }
2541a0216d1Sopenharmony_ci}
2551a0216d1Sopenharmony_ci
2561a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
2571a0216d1Sopenharmony_ci#[cfg(windows)]
2581a0216d1Sopenharmony_ciimpl<T: AsHandle> AsHandle for &T {
2591a0216d1Sopenharmony_ci    #[inline]
2601a0216d1Sopenharmony_ci    fn as_handle(&self) -> BorrowedHandle<'_> {
2611a0216d1Sopenharmony_ci        T::as_handle(self)
2621a0216d1Sopenharmony_ci    }
2631a0216d1Sopenharmony_ci}
2641a0216d1Sopenharmony_ci
2651a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
2661a0216d1Sopenharmony_ci#[cfg(windows)]
2671a0216d1Sopenharmony_ciimpl<T: AsHandle> AsHandle for &mut T {
2681a0216d1Sopenharmony_ci    #[inline]
2691a0216d1Sopenharmony_ci    fn as_handle(&self) -> BorrowedHandle<'_> {
2701a0216d1Sopenharmony_ci        T::as_handle(self)
2711a0216d1Sopenharmony_ci    }
2721a0216d1Sopenharmony_ci}
2731a0216d1Sopenharmony_ci
2741a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
2751a0216d1Sopenharmony_ci#[cfg(windows)]
2761a0216d1Sopenharmony_ciimpl<T: AsSocket> AsSocket for &T {
2771a0216d1Sopenharmony_ci    #[inline]
2781a0216d1Sopenharmony_ci    fn as_socket(&self) -> BorrowedSocket<'_> {
2791a0216d1Sopenharmony_ci        T::as_socket(self)
2801a0216d1Sopenharmony_ci    }
2811a0216d1Sopenharmony_ci}
2821a0216d1Sopenharmony_ci
2831a0216d1Sopenharmony_ci#[cfg(not(io_safety_is_in_std))]
2841a0216d1Sopenharmony_ci#[cfg(windows)]
2851a0216d1Sopenharmony_ciimpl<T: AsSocket> AsSocket for &mut T {
2861a0216d1Sopenharmony_ci    #[inline]
2871a0216d1Sopenharmony_ci    fn as_socket(&self) -> BorrowedSocket<'_> {
2881a0216d1Sopenharmony_ci        T::as_socket(self)
2891a0216d1Sopenharmony_ci    }
2901a0216d1Sopenharmony_ci}
291