Skip to content

Commit

Permalink
Add RawFd traits for net
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Dec 22, 2016
1 parent 92c8e0f commit 7d3ae87
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/libstd/sys/redox/ext/io.rs
Expand Up @@ -13,8 +13,9 @@
#![stable(feature = "rust1", since = "1.0.0")]

use fs;
use net;
use sys;
use sys_common::{AsInner, FromInner, IntoInner};
use sys_common::{self, AsInner, FromInner, IntoInner};

/// Raw file descriptors.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -89,58 +90,62 @@ impl IntoRawFd for fs::File {
}
}

/*
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::TcpStream {
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_inner().fd().raw()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::TcpListener {
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_inner().fd().raw()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::UdpSocket {
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_inner().fd().raw()
}
}

#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::TcpStream {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
let socket = sys::net::Socket::from_inner(fd);
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
let file = sys::fs::File::from_inner(fd);
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(file))
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::TcpListener {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
let socket = sys::net::Socket::from_inner(fd);
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
let file = sys::fs::File::from_inner(fd);
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(file))
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::UdpSocket {
unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
let socket = sys::net::Socket::from_inner(fd);
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
let file = sys::fs::File::from_inner(fd);
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(file))
}
}

#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::TcpStream {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
self.into_inner().into_inner().into_fd().into_raw()
}
}
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::TcpListener {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
self.into_inner().into_inner().into_fd().into_raw()
}
}
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::UdpSocket {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
self.into_inner().into_inner().into_fd().into_raw()
}
}
*/
29 changes: 29 additions & 0 deletions src/libstd/sys/redox/net/tcp.rs
Expand Up @@ -12,6 +12,7 @@ use io::{Error, ErrorKind, Result};
use net::{SocketAddr, Shutdown};
use path::Path;
use sys::fs::{File, OpenOptions};
use sys_common::{AsInner, FromInner, IntoInner};
use time::Duration;
use vec::Vec;

Expand Down Expand Up @@ -112,6 +113,20 @@ impl TcpStream {
}
}

impl AsInner<File> for TcpStream {
fn as_inner(&self) -> &File { &self.0 }
}

impl FromInner<File> for TcpStream {
fn from_inner(file: File) -> TcpStream {
TcpStream(file)
}
}

impl IntoInner<File> for TcpStream {
fn into_inner(self) -> File { self.0 }
}

#[derive(Debug)]
pub struct TcpListener(File);

Expand Down Expand Up @@ -168,3 +183,17 @@ impl TcpListener {
Err(Error::new(ErrorKind::Other, "TcpListener::set_ttl not implemented"))
}
}

impl AsInner<File> for TcpListener {
fn as_inner(&self) -> &File { &self.0 }
}

impl FromInner<File> for TcpListener {
fn from_inner(file: File) -> TcpListener {
TcpListener(file)
}
}

impl IntoInner<File> for TcpListener {
fn into_inner(self) -> File { self.0 }
}
15 changes: 15 additions & 0 deletions src/libstd/sys/redox/net/udp.rs
Expand Up @@ -13,6 +13,7 @@ use io::{Error, ErrorKind, Result};
use net::{SocketAddr, Ipv4Addr, Ipv6Addr};
use path::Path;
use sys::fs::{File, OpenOptions};
use sys_common::{AsInner, FromInner, IntoInner};
use time::Duration;

use super::{path_to_peer_addr, path_to_local_addr};
Expand Down Expand Up @@ -171,3 +172,17 @@ impl UdpSocket {
Err(Error::new(ErrorKind::Other, "UdpSocket::leave_multicast_v6 not implemented"))
}
}

impl AsInner<File> for UdpSocket {
fn as_inner(&self) -> &File { &self.0 }
}

impl FromInner<File> for UdpSocket {
fn from_inner(file: File) -> UdpSocket {
UdpSocket(file, UnsafeCell::new(None))
}
}

impl IntoInner<File> for UdpSocket {
fn into_inner(self) -> File { self.0 }
}

0 comments on commit 7d3ae87

Please sign in to comment.