tun_rs/async_device/
mod.rs

1#[cfg(unix)]
2pub(crate) mod unix;
3#[cfg(all(unix, not(target_os = "macos")))]
4pub use unix::AsyncDevice;
5#[cfg(target_os = "macos")]
6mod macos;
7#[cfg(target_os = "macos")]
8pub use macos::AsyncDevice;
9#[cfg(windows)]
10mod windows;
11#[cfg(windows)]
12pub use windows::AsyncDevice;
13
14#[cfg(all(
15    any(feature = "async_io", feature = "async_tokio"),
16    feature = "async_framed"
17))]
18#[cfg_attr(
19    docsrs,
20    doc(cfg(all(
21        any(feature = "async_io", feature = "async_tokio"),
22        feature = "async_framed"
23    )))
24)]
25pub mod async_framed;
26
27#[cfg(all(feature = "async_tokio", feature = "async_io", not(doc)))]
28compile_error! {"More than one asynchronous runtime is simultaneously specified in features"}
29
30#[cfg(unix)]
31pub struct BorrowedAsyncDevice<'dev> {
32    dev: AsyncDevice,
33    _phantom: std::marker::PhantomData<&'dev AsyncDevice>,
34}
35#[cfg(unix)]
36impl std::ops::Deref for BorrowedAsyncDevice<'_> {
37    type Target = AsyncDevice;
38    fn deref(&self) -> &Self::Target {
39        &self.dev
40    }
41}
42#[cfg(unix)]
43impl BorrowedAsyncDevice<'_> {
44    /// # Safety
45    /// The fd passed in must be a valid, open file descriptor.
46    /// Unlike [`from_fd`], this function does **not** take ownership of `fd`,
47    /// and therefore will not close it when dropped.  
48    /// The caller is responsible for ensuring the lifetime and eventual closure of `fd`.
49    pub unsafe fn borrow_raw(fd: std::os::fd::RawFd) -> std::io::Result<Self> {
50        #[allow(unused_unsafe)]
51        unsafe {
52            Ok(Self {
53                dev: AsyncDevice::borrow_raw(fd)?,
54                _phantom: std::marker::PhantomData,
55            })
56        }
57    }
58}