Skip to content

Commit

Permalink
Handle conversions for Windows Tokio unnamed pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
kotauskas committed Jun 13, 2024
1 parent 0257e92 commit 30fa27a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
50 changes: 25 additions & 25 deletions src/macros/forward_handle_and_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,62 +98,62 @@ macro_rules! forward_handle {
}

macro_rules! forward_try_into_handle {
(@impl $({$($lt:tt)*})? $ty:ty, $fldt:path, $hty:ident, $cfg:ident) => {
(@impl $({$($lt:tt)*})? $ty:ty, $ety:path, $hty:ident, $cfg:ident) => {
#[cfg($cfg)]
impl $(<$($lt)*>)? ::std::convert::TryFrom<$ty> for ::std::os::$cfg::io::$hty {
type Error = <::std::os::$cfg::io::$hty as ::std::convert::TryFrom<$fldt>>::Error;
type Error = $ety;
#[inline]
fn try_from(x: $ty) -> Result<Self, Self::Error> {
::std::convert::TryFrom::try_from(x.0)
}
}
};
($({$($lt:tt)*})? $ty:ty, $fldt:path, windows) => {
forward_try_into_handle!(@impl $({$($lt)*})? $ty, $fldt, OwnedHandle, windows);
($({$($lt:tt)*})? $ty:ty, $ety:path, windows) => {
forward_try_into_handle!(@impl $({$($lt)*})? $ty, $ety, OwnedHandle, windows);
};
($({$($lt:tt)*})? $ty:ty, $fldt:path, unix) => {
forward_try_into_handle!(@impl $({$($lt)*})? $ty, $fldt, OwnedFd, unix);
($({$($lt:tt)*})? $ty:ty, $ety:path, unix) => {
forward_try_into_handle!(@impl $({$($lt)*})? $ty, $ety, OwnedFd, unix);
};
($({$($lt:tt)*})? $ty:ty, $fldt:path) => {
($({$($lt:tt)*})? $ty:ty, $ety:path) => {
forward_try_into_handle!($({$($lt)*})? $ty, windows);
forward_try_into_handle!($({$($lt)*})? $ty, unix);
};
}

macro_rules! forward_try_from_handle {
(@impl $({$($lt:tt)*})? $ty:ty, $fldt:path, $hty:ident, $cfg:ident) => {
(@impl $({$($lt:tt)*})? $ty:ty, $ety:path, $hty:ident, $cfg:ident) => {
#[cfg($cfg)]
impl $(<$($lt)*>)? ::std::convert::TryFrom<::std::os::$cfg::io::$hty> for $ty {
type Error = <$fldt as ::std::convert::TryFrom<::std::os::$cfg::io::$hty>>::Error;
type Error = $ety;
#[inline]
fn try_from(x: ::std::os::$cfg::io::$hty) -> Result<Self, Self::Error> {
Ok(Self(::std::convert::TryFrom::try_from(x)?))
}
}
};
($({$($lt:tt)*})? $ty:ty, $fldt:path, windows) => {
forward_try_from_handle!(@impl $({$($lt)*})? $ty, $fldt, OwnedHandle, windows);
($({$($lt:tt)*})? $ty:ty, $ety:path, windows) => {
forward_try_from_handle!(@impl $({$($lt)*})? $ty, $ety, OwnedHandle, windows);
};
($({$($lt:tt)*})? $ty:ty, $fldt:path, unix) => {
forward_try_from_handle!(@impl $({$($lt)*})? $ty, $fldt, OwnedFd, unix);
($({$($lt:tt)*})? $ty:ty, $ety:path, unix) => {
forward_try_from_handle!(@impl $({$($lt)*})? $ty, $ety, OwnedFd, unix);
};
($({$($lt:tt)*})? $ty:ty, $fldt:path) => {
forward_try_from_handle!($({$($lt)*})? $ty, $fldt, windows);
forward_try_from_handle!($({$($lt)*})? $ty, $fldt, unix);
($({$($lt:tt)*})? $ty:ty, $ety:path) => {
forward_try_from_handle!($({$($lt)*})? $ty, $ety, windows);
forward_try_from_handle!($({$($lt)*})? $ty, $ety, unix);
};
}

macro_rules! forward_try_handle {
($({$($lt:tt)*})? $ty:ty, $fldt:path, windows) => {
forward_try_into_handle!($({$($lt)*})? $ty, $fldt, windows);
forward_try_from_handle!($({$($lt)*})? $ty, $fldt, windows);
($({$($lt:tt)*})? $ty:ty, $ety:path, windows) => {
forward_try_into_handle!($({$($lt)*})? $ty, $ety, windows);
forward_try_from_handle!($({$($lt)*})? $ty, $ety, windows);
};
($({$($lt:tt)*})? $ty:ty, $fldt:path, unix) => {
forward_try_into_handle!($({$($lt)*})? $ty, $fldt, unix);
forward_try_from_handle!($({$($lt)*})? $ty, $fldt, unix);
($({$($lt:tt)*})? $ty:ty, $ety:path, unix) => {
forward_try_into_handle!($({$($lt)*})? $ty, $ety, unix);
forward_try_from_handle!($({$($lt)*})? $ty, $ety, unix);
};
($({$($lt:tt)*})? $ty:ty, $fldt:path) => {
forward_try_handle!($({$($lt)*})? $ty, $fldt, windows);
forward_try_handle!($({$($lt)*})? $ty, $fldt, unix);
($({$($lt:tt)*})? $ty:ty, $ety:path) => {
forward_try_handle!($({$($lt)*})? $ty, $ety, windows);
forward_try_handle!($({$($lt)*})? $ty, $ety, unix);
};
}
51 changes: 44 additions & 7 deletions src/os/windows/unnamed_pipe/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ use crate::{
};
use std::{
io,
mem::ManuallyDrop,
pin::Pin,
task::{ready, Context, Poll},
};
use tokio::{fs::File, io::AsyncWrite};

static INFLIGHT_ERR: &str =
"cannot deregister unnamed pipe from the Tokio runtime with in-flight operations";

fn pair2pair((tx, rx): (SyncSender, SyncRecver)) -> io::Result<(PubSender, PubRecver)> {
Ok((PubSender(tx.try_into()?), PubRecver(rx.try_into()?)))
}
Expand Down Expand Up @@ -50,10 +54,24 @@ impl CreationOptionsExt for CreationOptions<'_> {
pub(crate) struct Recver(File);
impl TryFrom<SyncRecver> for Recver {
type Error = io::Error;
#[inline]
fn try_from(rx: SyncRecver) -> io::Result<Self> {
Ok(Self(File::from_std(
<std::fs::File as From<OwnedHandle>>::from(rx.into()),
)))
Self::try_from(OwnedHandle::from(rx.0))
}
}
impl TryFrom<Recver> for OwnedHandle {
type Error = io::Error;
fn try_from(rx: Recver) -> io::Result<Self> {
rx.0.try_into_std()
.map(OwnedHandle::from)
.map_err(|_| io::Error::new(io::ErrorKind::Other, INFLIGHT_ERR))
}
}
// TODO can this be infallible?
impl TryFrom<OwnedHandle> for Recver {
type Error = io::Error;
fn try_from(handle: OwnedHandle) -> io::Result<Self> {
Ok(Self(File::from_std(handle.into())))
}
}
multimacro! {
Expand Down Expand Up @@ -112,12 +130,31 @@ impl Drop for Sender {

impl TryFrom<SyncSender> for Sender {
type Error = io::Error;
fn try_from(tx: SyncSender) -> io::Result<Self> {
let handle = OwnedHandle::from(tx);
#[inline]
fn try_from(rx: SyncSender) -> io::Result<Self> {
Self::try_from(OwnedHandle::from(rx.0))
}
}
impl TryFrom<Sender> for OwnedHandle {
type Error = io::Error;
fn try_from(tx: Sender) -> io::Result<Self> {
ManuallyDrop::new(tx)
.io
.take()
.expect(LIMBO_ERR)
.try_into_std()
.map(OwnedHandle::from)
.map_err(|_| io::Error::new(io::ErrorKind::Other, INFLIGHT_ERR))
}
}
// TODO can this be infallible?
impl TryFrom<OwnedHandle> for Sender {
type Error = io::Error;
fn try_from(handle: OwnedHandle) -> io::Result<Self> {
Ok(Self {
io: Some(File::from_std(std::fs::File::from(handle))),
io: Some(File::from_std(handle.into())),
flusher: TokioFlusher::new(),
needs_flush: false,
needs_flush: true,
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/unnamed_pipe/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ multimacro! {
pinproj_for_unpin(RecverImpl),
forward_tokio_read,
forward_as_handle,
forward_try_handle(io::Error),
forward_debug,
derive_asraw,
}
Expand All @@ -55,6 +56,7 @@ multimacro! {
forward_rbv(SenderImpl, &),
forward_tokio_write,
forward_as_handle,
forward_try_handle(io::Error),
forward_debug,
derive_asraw,
}

0 comments on commit 30fa27a

Please sign in to comment.