From 8678164483bd1509137ce0ee7992f6c25e55df10 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Thu, 11 Apr 2019 17:35:36 +0200 Subject: [PATCH] Impl RawFd converstion traits for TcpListener, TcpStream and UdpSocket --- src/libstd/sys/wasi/ext/io.rs | 55 +++++++++ src/libstd/sys/wasi/net.rs | 202 +++++++++++++++++++++++----------- 2 files changed, 190 insertions(+), 67 deletions(-) diff --git a/src/libstd/sys/wasi/ext/io.rs b/src/libstd/sys/wasi/ext/io.rs index cf75a96d28c1a..12afd1d42dc19 100644 --- a/src/libstd/sys/wasi/ext/io.rs +++ b/src/libstd/sys/wasi/ext/io.rs @@ -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. @@ -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() diff --git a/src/libstd/sys/wasi/net.rs b/src/libstd/sys/wasi/net.rs index 1579aa4b572b0..5486cdec4c044 100644 --- a/src/libstd/sys/wasi/net.rs +++ b/src/libstd/sys/wasi/net.rs @@ -4,8 +4,12 @@ use crate::net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr}; use crate::time::Duration; use crate::sys::{unsupported, Void}; use crate::convert::TryFrom; +use crate::sys::fd::{WasiFd}; +use crate::sys_common::FromInner; -pub struct TcpStream(Void); +pub struct TcpStream { + fd: WasiFd, +} impl TcpStream { pub fn connect(_: io::Result<&SocketAddr>) -> io::Result { @@ -17,89 +21,111 @@ impl TcpStream { } pub fn set_read_timeout(&self, _: Option) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn set_write_timeout(&self, _: Option) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn read_timeout(&self) -> io::Result> { - match self.0 {} + unsupported() } pub fn write_timeout(&self) -> io::Result> { - match self.0 {} + unsupported() } pub fn peek(&self, _: &mut [u8]) -> io::Result { - match self.0 {} + unsupported() } pub fn read(&self, _: &mut [u8]) -> io::Result { - match self.0 {} + unsupported() } pub fn read_vectored(&self, _: &mut [IoVecMut<'_>]) -> io::Result { - match self.0 {} + unsupported() } pub fn write(&self, _: &[u8]) -> io::Result { - match self.0 {} + unsupported() } pub fn write_vectored(&self, _: &[IoVec<'_>]) -> io::Result { - match self.0 {} + unsupported() } pub fn peer_addr(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn socket_addr(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn shutdown(&self, _: Shutdown) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn duplicate(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_nodelay(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn nodelay(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_ttl(&self, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn ttl(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn take_error(&self) -> io::Result> { - match self.0 {} + unsupported() } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() + } + + pub fn fd(&self) -> &WasiFd { + &self.fd + } + + pub fn into_fd(self) -> WasiFd { + self.fd + } +} + +impl FromInner for TcpStream { + fn from_inner(fd: u32) -> TcpStream { + unsafe { + TcpStream { + fd: WasiFd::from_raw(fd), + } + } } } impl fmt::Debug for TcpStream { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.0 {} + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("TcpStream") + .field("fd", &self.fd.as_raw()) + .finish() } } -pub struct TcpListener(Void); +pub struct TcpListener { + fd: WasiFd +} impl TcpListener { pub fn bind(_: io::Result<&SocketAddr>) -> io::Result { @@ -107,49 +133,71 @@ impl TcpListener { } pub fn socket_addr(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { - match self.0 {} + unsupported() } pub fn duplicate(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_ttl(&self, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn ttl(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_only_v6(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn only_v6(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn take_error(&self) -> io::Result> { - match self.0 {} + unsupported() } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() + } + + pub fn fd(&self) -> &WasiFd { + &self.fd + } + + pub fn into_fd(self) -> WasiFd { + self.fd + } +} + +impl FromInner for TcpListener { + fn from_inner(fd: u32) -> TcpListener { + unsafe { + TcpListener { + fd: WasiFd::from_raw(fd), + } + } } } impl fmt::Debug for TcpListener { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.0 {} + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("TcpListener") + .field("fd", &self.fd.as_raw()) + .finish() } } -pub struct UdpSocket(Void); +pub struct UdpSocket { + fd: WasiFd, +} impl UdpSocket { pub fn bind(_: io::Result<&SocketAddr>) -> io::Result { @@ -157,133 +205,153 @@ impl UdpSocket { } pub fn peer_addr(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn socket_addr(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - match self.0 {} + unsupported() } pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> { - match self.0 {} + unsupported() } pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result { - match self.0 {} + unsupported() } pub fn duplicate(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_read_timeout(&self, _: Option) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn set_write_timeout(&self, _: Option) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn read_timeout(&self) -> io::Result> { - match self.0 {} + unsupported() } pub fn write_timeout(&self) -> io::Result> { - match self.0 {} + unsupported() } pub fn set_broadcast(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn broadcast(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn multicast_loop_v4(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn multicast_ttl_v4(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn multicast_loop_v6(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn set_ttl(&self, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn ttl(&self) -> io::Result { - match self.0 {} + unsupported() } pub fn take_error(&self) -> io::Result> { - match self.0 {} + unsupported() } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn recv(&self, _: &mut [u8]) -> io::Result { - match self.0 {} + unsupported() } pub fn peek(&self, _: &mut [u8]) -> io::Result { - match self.0 {} + unsupported() } pub fn send(&self, _: &[u8]) -> io::Result { - match self.0 {} + unsupported() } pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> { - match self.0 {} + unsupported() + } + + pub fn fd(&self) -> &WasiFd { + &self.fd + } + + pub fn into_fd(self) -> WasiFd { + self.fd + } +} + +impl FromInner for UdpSocket { + fn from_inner(fd: u32) -> UdpSocket { + unsafe { + UdpSocket { + fd: WasiFd::from_raw(fd), + } + } } } impl fmt::Debug for UdpSocket { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.0 {} + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("UdpSocket") + .field("fd", &self.fd.as_raw()) + .finish() } }