io_extras/os/windows/
mod.rs#[cfg(any(test, feature = "os_pipe"))]
use os_pipe::{PipeReader, PipeWriter};
use std::fs::File;
use std::io::{Stderr, StderrLock, Stdin, StdinLock, Stdout, StdoutLock};
use std::net::{TcpListener, TcpStream, UdpSocket};
use std::os::windows::io::{
AsRawHandle, AsRawSocket, IntoRawHandle, IntoRawSocket, RawHandle, RawSocket,
};
use std::process::{ChildStderr, ChildStdin, ChildStdout};
use stdio::Stdio;
mod stdio;
mod traits;
mod types;
pub use crate::read_write::{AsRawReadWriteHandleOrSocket, AsReadWriteHandleOrSocket};
pub use traits::AsHandleOrSocket;
pub use types::{BorrowedHandleOrSocket, OwnedHandleOrSocket};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
#[repr(transparent)]
pub struct RawHandleOrSocket(pub(crate) RawEnum);
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
pub(crate) enum RawEnum {
Handle(RawHandle),
Socket(RawSocket),
Stdio(Stdio),
}
impl RawHandleOrSocket {
#[inline]
#[must_use]
pub const fn unowned_from_raw_handle(raw_handle: RawHandle) -> Self {
Self(RawEnum::Handle(raw_handle))
}
#[inline]
#[must_use]
pub const fn unowned_from_raw_socket(raw_socket: RawSocket) -> Self {
Self(RawEnum::Socket(raw_socket))
}
#[inline]
#[must_use]
pub fn as_raw_handle(&self) -> Option<RawHandle> {
match self.0 {
RawEnum::Handle(raw_handle) => Some(raw_handle),
RawEnum::Socket(_) => None,
RawEnum::Stdio(ref stdio) => Some(stdio.as_raw_handle()),
}
}
#[inline]
#[must_use]
pub const fn as_raw_socket(&self) -> Option<RawSocket> {
match self.0 {
RawEnum::Handle(_) | RawEnum::Stdio(_) => None,
RawEnum::Socket(raw_socket) => Some(raw_socket),
}
}
#[inline]
#[must_use]
pub const fn stdin() -> Self {
Self(RawEnum::Stdio(Stdio::stdin()))
}
#[inline]
#[must_use]
pub const fn stdout() -> Self {
Self(RawEnum::Stdio(Stdio::stdout()))
}
#[inline]
#[must_use]
pub const fn stderr() -> Self {
Self(RawEnum::Stdio(Stdio::stderr()))
}
}
pub trait AsRawHandleOrSocket {
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket;
}
pub trait IntoRawHandleOrSocket {
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket;
}
pub trait FromRawHandleOrSocket {
unsafe fn from_raw_handle_or_socket(raw_handle_or_socket: RawHandleOrSocket) -> Self;
}
unsafe impl Send for RawHandleOrSocket {}
unsafe impl Sync for RawHandleOrSocket {}
impl AsRawHandleOrSocket for RawHandleOrSocket {
#[inline]
fn as_raw_handle_or_socket(&self) -> Self {
*self
}
}
impl AsRawHandleOrSocket for Stdin {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for StdinLock<'_> {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for Stdout {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for StdoutLock<'_> {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for Stderr {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for StderrLock<'_> {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for File {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for ChildStdin {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for ChildStdout {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for ChildStderr {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
impl AsRawHandleOrSocket for TcpStream {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
impl AsRawHandleOrSocket for TcpListener {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
impl AsRawHandleOrSocket for UdpSocket {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "async-std")]
impl AsRawHandleOrSocket for async_std::io::Stdin {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "async-std")]
impl AsRawHandleOrSocket for async_std::io::Stdout {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "async-std")]
impl AsRawHandleOrSocket for async_std::io::Stderr {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "async-std")]
impl AsRawHandleOrSocket for async_std::fs::File {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "async-std")]
impl AsRawHandleOrSocket for async_std::net::TcpStream {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "async-std")]
impl AsRawHandleOrSocket for async_std::net::TcpListener {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "async-std")]
impl AsRawHandleOrSocket for async_std::net::UdpSocket {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::io::Stdin {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::io::Stdout {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::io::Stderr {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::fs::File {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::net::TcpStream {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::net::TcpListener {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::net::UdpSocket {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::process::ChildStdin {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::process::ChildStdout {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "tokio")]
impl AsRawHandleOrSocket for tokio::process::ChildStderr {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(any(test, feature = "os_pipe"))]
impl AsRawHandleOrSocket for PipeReader {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(any(test, feature = "os_pipe"))]
impl AsRawHandleOrSocket for PipeWriter {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::as_raw_handle(self))
}
}
#[cfg(feature = "socket2")]
impl AsRawHandleOrSocket for socket2::Socket {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "use_mio_net")]
impl AsRawHandleOrSocket for mio::net::TcpStream {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "use_mio_net")]
impl AsRawHandleOrSocket for mio::net::TcpListener {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
#[cfg(feature = "use_mio_net")]
impl AsRawHandleOrSocket for mio::net::UdpSocket {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::as_raw_socket(self))
}
}
impl IntoRawHandleOrSocket for File {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::into_raw_handle(self))
}
}
impl IntoRawHandleOrSocket for ChildStdin {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::into_raw_handle(self))
}
}
impl IntoRawHandleOrSocket for ChildStdout {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::into_raw_handle(self))
}
}
impl IntoRawHandleOrSocket for ChildStderr {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::into_raw_handle(self))
}
}
impl IntoRawHandleOrSocket for TcpStream {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
impl IntoRawHandleOrSocket for TcpListener {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
impl IntoRawHandleOrSocket for UdpSocket {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
#[cfg(feature = "os_pipe")]
impl IntoRawHandleOrSocket for PipeReader {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::into_raw_handle(self))
}
}
#[cfg(feature = "os_pipe")]
impl IntoRawHandleOrSocket for PipeWriter {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::into_raw_handle(self))
}
}
#[cfg(feature = "socket2")]
impl IntoRawHandleOrSocket for socket2::Socket {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
#[cfg(feature = "use_mio_net")]
impl IntoRawHandleOrSocket for mio::net::TcpStream {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
#[cfg(feature = "use_mio_net")]
impl IntoRawHandleOrSocket for mio::net::TcpListener {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
#[cfg(feature = "use_mio_net")]
impl IntoRawHandleOrSocket for mio::net::UdpSocket {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
#[cfg(feature = "async-std")]
impl IntoRawHandleOrSocket for async_std::fs::File {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_handle(Self::into_raw_handle(self))
}
}
#[cfg(feature = "async-std")]
impl IntoRawHandleOrSocket for async_std::net::TcpStream {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
#[cfg(feature = "async-std")]
impl IntoRawHandleOrSocket for async_std::net::TcpListener {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}
#[cfg(feature = "async-std")]
impl IntoRawHandleOrSocket for async_std::net::UdpSocket {
#[inline]
fn into_raw_handle_or_socket(self) -> RawHandleOrSocket {
RawHandleOrSocket::unowned_from_raw_socket(Self::into_raw_socket(self))
}
}