use std::pin::Pin;
use std::task::{Context, Poll};
use ::tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
#[cfg(unix)]
pub(super) type OwnedRead = ::tokio::net::unix::pipe::Receiver;
#[cfg(windows)]
pub(super) type OwnedRead = ::tokio::net::windows::named_pipe::NamedPipeServer;
pub struct ChildStdin {
pub(super) inner: OwnedWrite,
}
#[cfg(unix)]
pub(super) type OwnedWrite = ::tokio::net::unix::pipe::Sender;
#[cfg(windows)]
pub(super) type OwnedWrite = ::tokio::net::windows::named_pipe::NamedPipeServer;
pub struct ChildStdout {
pub(super) inner: OwnedRead,
}
pub struct ChildStderr {
pub(super) inner: OwnedRead,
}
impl AsyncWrite for ChildStdin {
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.inner).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_shutdown(cx)
}
}
impl AsyncRead for ChildStdout {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_read(cx, buf)
}
}
impl AsyncRead for ChildStderr {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_read(cx, buf)
}
}
impl std::fmt::Debug for ChildStdin {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChildStdin").finish_non_exhaustive()
}
}
impl std::fmt::Debug for ChildStdout {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChildStdout").finish_non_exhaustive()
}
}
impl std::fmt::Debug for ChildStderr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChildStderr").finish_non_exhaustive()
}
}
pub(super) fn register_reader(end: crate::PipeReader) -> std::io::Result<OwnedRead> {
#[cfg(unix)]
{
::tokio::net::unix::pipe::Receiver::from_owned_fd(std::os::fd::OwnedFd::from(end))
}
#[cfg(windows)]
{
adopt(end.into())
}
}
pub(super) fn register_writer(end: crate::PipeWriter) -> std::io::Result<OwnedWrite> {
#[cfg(unix)]
{
::tokio::net::unix::pipe::Sender::from_owned_fd(std::os::fd::OwnedFd::from(end))
}
#[cfg(windows)]
{
adopt(end.into())
}
}
#[cfg(windows)]
fn adopt(
end: std::os::windows::io::OwnedHandle,
) -> std::io::Result<::tokio::net::windows::named_pipe::NamedPipeServer> {
use std::os::windows::io::{AsHandle, IntoRawHandle};
debug_assert!(
crate::pipe::windows::is_overlapped(end.as_handle()).expect("query the end's mode"),
"a pipe end registered with the reactor is overlapped"
);
let handle = end.into_raw_handle();
unsafe { ::tokio::net::windows::named_pipe::NamedPipeServer::from_raw_handle(handle) }
}
#[cfg(test)]
#[path = "stdio_tests.rs"]
mod stdio_tests;