From 14f3cbecc098f9e1ec70477ed1f190ad6135754a Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sun, 11 Jun 2023 18:40:12 -0700 Subject: [PATCH] Support I/O Safety types and traits: AsSocket, BorrowedSocket, OwnedSocket This allows callers of uds_windows to potentially avoid having to use the Raw types and traits. This also allows the use of uds_windows with upcoming changes to async-io to use AsFd. Get compiling from old PR; add AsSocket for UnixStream Cargo format --- src/stdnet/net.rs | 10 +++++++++- src/stdnet/socket.rs | 22 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/stdnet/net.rs b/src/stdnet/net.rs index eda5294..c2829f4 100644 --- a/src/stdnet/net.rs +++ b/src/stdnet/net.rs @@ -3,7 +3,9 @@ use std::io; use std::mem; use std::net::Shutdown; use std::os::raw::c_int; -use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket}; +use std::os::windows::io::{ + AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, IntoRawSocket, RawSocket, +}; use std::path::Path; use std::time::Duration; @@ -298,6 +300,12 @@ impl<'a> io::Write for &'a UnixStream { } } +impl AsSocket for UnixStream { + fn as_socket(&self) -> BorrowedSocket { + self.0.as_socket() + } +} + impl AsRawSocket for UnixStream { fn as_raw_socket(&self) -> RawSocket { self.0.as_raw_socket() diff --git a/src/stdnet/socket.rs b/src/stdnet/socket.rs index 19d0867..982387c 100644 --- a/src/stdnet/socket.rs +++ b/src/stdnet/socket.rs @@ -4,7 +4,9 @@ use std::io; use std::mem; use std::net::Shutdown; use std::os::raw::{c_int, c_ulong}; -use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket}; +use std::os::windows::io::{ + AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, IntoRawSocket, OwnedSocket, RawSocket, +}; use std::ptr; use std::sync::Once; use std::time::Duration; @@ -298,3 +300,21 @@ impl IntoRawSocket for Socket { ret } } + +impl AsSocket for Socket { + fn as_socket(&self) -> BorrowedSocket<'_> { + unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } + } +} + +impl From for OwnedSocket { + fn from(sock: Socket) -> OwnedSocket { + unsafe { OwnedSocket::from_raw_socket(sock.into_raw_socket()) } + } +} + +impl From for Socket { + fn from(owned: OwnedSocket) -> Socket { + unsafe { Socket::from_raw_socket(owned.into_raw_socket()) } + } +}