Skip to content

Commit

Permalink
AsFd trait added for socket
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 18, 2023
1 parent c0919cc commit 86d38a2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ impl FromRawFd for TcpListener {
}
}

#[cfg(unix)]
impl std::os::fd::AsFd for TcpListener {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) }
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpListener {
fn into_raw_socket(self) -> RawSocket {
Expand Down
7 changes: 7 additions & 0 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ impl FromRawFd for TcpStream {
}
}

#[cfg(unix)]
impl std::os::fd::AsFd for TcpStream {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) }
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpStream {
fn into_raw_socket(self) -> RawSocket {
Expand Down
6 changes: 6 additions & 0 deletions src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,9 @@ impl FromRawFd for UnixDatagram {
UnixDatagram::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl std::os::fd::AsFd for UnixDatagram {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) }
}
}
6 changes: 6 additions & 0 deletions src/net/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@ impl FromRawFd for UnixListener {
UnixListener::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl std::os::fd::AsFd for UnixListener {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) }
}
}
6 changes: 6 additions & 0 deletions src/net/uds/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,9 @@ impl FromRawFd for UnixStream {
UnixStream::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl std::os::fd::AsFd for UnixStream {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) }
}
}

0 comments on commit 86d38a2

Please sign in to comment.