Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ libc = "0.2.77"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["winsock2"] }

[features]
io_safety = []

[dev-dependencies]
async-channel = "1"
async-net = "1"
Expand Down
54 changes: 54 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ use std::sync::Arc;
use std::task::{Context, Poll, Waker};
use std::time::{Duration, Instant};

#[cfg(all(feature = "io_safety", unix))]
use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
#[cfg(unix)]
use std::{
os::unix::io::{AsRawFd, RawFd},
Expand All @@ -73,6 +75,8 @@ use std::{

#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, RawSocket};
#[cfg(all(feature = "io_safety", windows))]
use std::os::windows::io::{AsSocket, BorrowedSocket, OwnedSocket};

use futures_lite::io::{AsyncRead, AsyncWrite};
use futures_lite::stream::{self, Stream};
Expand Down Expand Up @@ -552,6 +556,31 @@ impl<T: AsRawFd> AsRawFd for Async<T> {
}
}

#[cfg(all(feature = "io_safety", unix))]
impl<T: AsFd> AsFd for Async<T> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.get_ref().as_fd()
}
}

#[cfg(all(feature = "io_safety", unix))]
impl<T: AsRawFd + From<OwnedFd>> TryFrom<OwnedFd> for Async<T> {
type Error = io::Error;

fn try_from(value: OwnedFd) -> Result<Self, Self::Error> {
Async::new(value.into())
}
}

#[cfg(all(feature = "io_safety", unix))]
impl<T: Into<OwnedFd>> TryFrom<Async<T>> for OwnedFd {
type Error = io::Error;

fn try_from(value: Async<T>) -> Result<Self, Self::Error> {
value.into_inner().map(Into::into)
}
}

#[cfg(windows)]
impl<T: AsRawSocket> Async<T> {
/// Creates an async I/O handle.
Expand Down Expand Up @@ -612,6 +641,31 @@ impl<T: AsRawSocket> AsRawSocket for Async<T> {
}
}

#[cfg(all(feature = "io_safety", windows))]
impl<T: AsSocket> AsSocket for Async<T> {
fn as_socket(&self) -> BorrowedSocket<'_> {
self.get_ref().as_socket()
}
}

#[cfg(all(feature = "io_safety", windows))]
impl<T: AsRawSocket + From<OwnedSocket>> TryFrom<OwnedSocket> for Async<T> {
type Error = io::Error;

fn try_from(value: OwnedSocket) -> Result<Self, Self::Error> {
Async::new(value.into())
}
}

#[cfg(all(feature = "io_safety", windows))]
impl<T: Into<OwnedSocket>> TryFrom<Async<T>> for OwnedSocket {
type Error = io::Error;

fn try_from(value: Async<T>) -> Result<Self, Self::Error> {
value.into_inner().map(Into::into)
}
}

impl<T> Async<T> {
/// Gets a reference to the inner I/O handle.
///
Expand Down