Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement I/O-safe traits on Socket #324

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ features = [

[features]
# Enable all API, even ones not available on all OSs.
all = []
all = []
25 changes: 24 additions & 1 deletion src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use std::os::unix::ffi::OsStrExt;
)
))]
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd};
#[cfg(feature = "all")]
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
#[cfg(feature = "all")]
Expand Down Expand Up @@ -2031,6 +2031,29 @@ impl FromRawFd for crate::Socket {
}
}

#[cfg_attr(docsrs, doc(cfg(unix)))]
impl AsFd for crate::Socket {
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: lifetime is bound by "self"
unsafe { BorrowedFd::borrow_raw(self.as_raw()) }
}
}

#[cfg_attr(docsrs, doc(cfg(unix)))]
impl From<OwnedFd> for crate::Socket {
fn from(fd: OwnedFd) -> Self {
Self::from_raw(fd.into_raw_fd())
}
}

#[cfg_attr(docsrs, doc(cfg(unix)))]
impl From<crate::Socket> for OwnedFd {
fn from(sock: crate::Socket) -> Self {
// SAFETY: sock.into_raw() is a valid fd
unsafe { OwnedFd::from_raw_fd(sock.into_raw()) }
}
}

#[cfg(feature = "all")]
from!(UnixStream, crate::Socket);
#[cfg(feature = "all")]
Expand Down
24 changes: 23 additions & 1 deletion src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use std::io::{self, IoSlice};
use std::marker::PhantomData;
use std::mem::{self, size_of, MaybeUninit};
use std::net::{self, Ipv4Addr, Ipv6Addr, Shutdown};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::os::windows::io::{
AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, IntoRawSocket, OwnedSocket, RawSocket,
};
use std::sync::Once;
use std::time::{Duration, Instant};
use std::{process, ptr, slice};
Expand Down Expand Up @@ -818,6 +820,26 @@ impl FromRawSocket for crate::Socket {
}
}

impl AsSocket for crate::Socket {
fn as_socket(&self) -> BorrowedSocket<'_> {
// SAFETY: lifetime is bound by "self"
unsafe { BorrowedSocket::borrow_raw(self.as_raw() as RawSocket) }
}
}

impl From<OwnedSocket> for crate::Socket {
fn from(fd: OwnedSocket) -> Self {
Self::from_raw(fd.into_raw_socket() as Socket)
}
}

impl From<crate::Socket> for OwnedSocket {
fn from(sock: crate::Socket) -> Self {
// SAFETY: sock.into_raw() is a valid fd
unsafe { OwnedSocket::from_raw_socket(sock.into_raw() as RawSocket) }
}
}

#[test]
fn in_addr_convertion() {
let ip = Ipv4Addr::new(127, 0, 0, 1);
Expand Down