Skip to content

Commit

Permalink
Support I/O Safety types and traits: AsSocket, BorrowedSocket, OwnedS…
Browse files Browse the repository at this point in the history
…ocket

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
  • Loading branch information
joshtriplett authored and haraldh committed Dec 2, 2023
1 parent 7f7c709 commit 14f3cbe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/stdnet/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand Down
22 changes: 21 additions & 1 deletion src/stdnet/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Socket> for OwnedSocket {
fn from(sock: Socket) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(sock.into_raw_socket()) }
}
}

impl From<OwnedSocket> for Socket {
fn from(owned: OwnedSocket) -> Socket {
unsafe { Socket::from_raw_socket(owned.into_raw_socket()) }
}
}

0 comments on commit 14f3cbe

Please sign in to comment.