Skip to content

Commit

Permalink
Rollup merge of rust-lang#59892 - rylev:as-raw-fd, r=alexcrichton
Browse files Browse the repository at this point in the history
Impl RawFd conversion traits for WASI TcpListener, TcpStream and UdpSocket

r? @alexcrichton
  • Loading branch information
Centril authored Apr 12, 2019
2 parents af4acd0 + 8678164 commit ba17313
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 67 deletions.
55 changes: 55 additions & 0 deletions src/libstd/sys/wasi/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::fs;
use crate::io;
use crate::sys;
use crate::net;
use crate::sys_common::{AsInner, FromInner, IntoInner};

/// Raw file descriptors.
Expand Down Expand Up @@ -50,6 +51,60 @@ pub trait IntoRawFd {
fn into_raw_fd(self) -> RawFd;
}

impl AsRawFd for net::TcpStream {
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
}
}

impl FromRawFd for net::TcpStream {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
net::TcpStream::from_inner(sys::net::TcpStream::from_inner(fd))
}
}

impl IntoRawFd for net::TcpStream {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}

impl AsRawFd for net::TcpListener {
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
}
}

impl FromRawFd for net::TcpListener {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
net::TcpListener::from_inner(sys::net::TcpListener::from_inner(fd))
}
}

impl IntoRawFd for net::TcpListener {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}

impl AsRawFd for net::UdpSocket {
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
}
}

impl FromRawFd for net::UdpSocket {
unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(fd))
}
}

impl IntoRawFd for net::UdpSocket {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}

impl AsRawFd for fs::File {
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
Expand Down
Loading

0 comments on commit ba17313

Please sign in to comment.