diff options
Diffstat (limited to 'src/libstd/sys/wasi/net.rs')
| -rw-r--r-- | src/libstd/sys/wasi/net.rs | 202 |
1 files changed, 135 insertions, 67 deletions
diff --git a/src/libstd/sys/wasi/net.rs b/src/libstd/sys/wasi/net.rs index 1579aa4b572..5486cdec4c0 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<TcpStream> { @@ -17,89 +21,111 @@ impl TcpStream { } pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn read_timeout(&self) -> io::Result<Option<Duration>> { - match self.0 {} + unsupported() } pub fn write_timeout(&self) -> io::Result<Option<Duration>> { - match self.0 {} + unsupported() } pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> { - match self.0 {} + unsupported() } pub fn read(&self, _: &mut [u8]) -> io::Result<usize> { - match self.0 {} + unsupported() } pub fn read_vectored(&self, _: &mut [IoVecMut<'_>]) -> io::Result<usize> { - match self.0 {} + unsupported() } pub fn write(&self, _: &[u8]) -> io::Result<usize> { - match self.0 {} + unsupported() } pub fn write_vectored(&self, _: &[IoVec<'_>]) -> io::Result<usize> { - match self.0 {} + unsupported() } pub fn peer_addr(&self) -> io::Result<SocketAddr> { - match self.0 {} + unsupported() } pub fn socket_addr(&self) -> io::Result<SocketAddr> { - match self.0 {} + unsupported() } pub fn shutdown(&self, _: Shutdown) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn duplicate(&self) -> io::Result<TcpStream> { - match self.0 {} + unsupported() } pub fn set_nodelay(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn nodelay(&self) -> io::Result<bool> { - match self.0 {} + unsupported() } pub fn set_ttl(&self, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn ttl(&self) -> io::Result<u32> { - match self.0 {} + unsupported() } pub fn take_error(&self) -> io::Result<Option<io::Error>> { - 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<u32> 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<TcpListener> { @@ -107,49 +133,71 @@ impl TcpListener { } pub fn socket_addr(&self) -> io::Result<SocketAddr> { - match self.0 {} + unsupported() } pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { - match self.0 {} + unsupported() } pub fn duplicate(&self) -> io::Result<TcpListener> { - match self.0 {} + unsupported() } pub fn set_ttl(&self, _: u32) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn ttl(&self) -> io::Result<u32> { - match self.0 {} + unsupported() } pub fn set_only_v6(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn only_v6(&self) -> io::Result<bool> { - match self.0 {} + unsupported() } pub fn take_error(&self) -> io::Result<Option<io::Error>> { - 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<u32> 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<UdpSocket> { @@ -157,133 +205,153 @@ impl UdpSocket { } pub fn peer_addr(&self) -> io::Result<SocketAddr> { - match self.0 {} + unsupported() } pub fn socket_addr(&self) -> io::Result<SocketAddr> { - 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<usize> { - match self.0 {} + unsupported() } pub fn duplicate(&self) -> io::Result<UdpSocket> { - match self.0 {} + unsupported() } pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn read_timeout(&self) -> io::Result<Option<Duration>> { - match self.0 {} + unsupported() } pub fn write_timeout(&self) -> io::Result<Option<Duration>> { - match self.0 {} + unsupported() } pub fn set_broadcast(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn broadcast(&self) -> io::Result<bool> { - 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<bool> { - 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<u32> { - 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<bool> { - 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<u32> { - match self.0 {} + unsupported() } pub fn take_error(&self) -> io::Result<Option<io::Error>> { - match self.0 {} + unsupported() } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { - match self.0 {} + unsupported() } pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> { - match self.0 {} + unsupported() } pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> { - match self.0 {} + unsupported() } pub fn send(&self, _: &[u8]) -> io::Result<usize> { - 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<u32> 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() } } |
