Skip to content

Commit

Permalink
feat(raw-fd): implement FromRawFd/FromRawSocket
Browse files Browse the repository at this point in the history
This allows HttpStream and HttpListener to be created from raw
sockets similar to their Tcp counterparts.  It also fixes up the
signature from i32 to RawFd for the AsRawFd method.
  • Loading branch information
mitsuhiko committed Aug 1, 2015
1 parent 0a59d73 commit 664bde5
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,34 @@ impl NetworkListener for HttpListener {
}
}

#[cfg(windows)]
impl ::std::os::windows::io::AsRawSocket for HttpListener {
fn as_raw_socket(&self) -> ::std::os::windows::io::RawSocket {
self.0.as_raw_socket()
}
}

#[cfg(windows)]
impl ::std::os::windows::io::FromRawSocket for HttpListener {
unsafe fn from_raw_socket(sock: ::std::os::windows::io::RawSocket) -> HttpListener {
HttpListener(TcpListener::from_raw_socket(sock))
}
}

#[cfg(unix)]
impl ::std::os::unix::io::AsRawFd for HttpListener {
fn as_raw_fd(&self) -> ::std::os::unix::io::RawFd {
self.0.as_raw_fd()
}
}

#[cfg(unix)]
impl ::std::os::unix::io::FromRawFd for HttpListener {
unsafe fn from_raw_fd(fd: ::std::os::unix::io::RawFd) -> HttpListener {
HttpListener(TcpListener::from_raw_fd(fd))
}
}

/// A wrapper around a TcpStream.
pub struct HttpStream(pub TcpStream);

Expand Down Expand Up @@ -213,13 +241,27 @@ impl ::std::os::windows::io::AsRawSocket for HttpStream {
}
}

#[cfg(windows)]
impl ::std::os::windows::io::FromRawSocket for HttpStream {
unsafe fn from_raw_socket(sock: ::std::os::windows::io::RawSocket) -> HttpStream {
HttpStream(TcpStream::from_raw_socket(sock))
}
}

#[cfg(unix)]
impl ::std::os::unix::io::AsRawFd for HttpStream {
fn as_raw_fd(&self) -> i32 {
fn as_raw_fd(&self) -> ::std::os::unix::io::RawFd {
self.0.as_raw_fd()
}
}

#[cfg(unix)]
impl ::std::os::unix::io::FromRawFd for HttpStream {
unsafe fn from_raw_fd(fd: ::std::os::unix::io::RawFd) -> HttpStream {
HttpStream(TcpStream::from_raw_fd(fd))
}
}

impl NetworkStream for HttpStream {
#[inline]
fn peer_addr(&mut self) -> io::Result<SocketAddr> {
Expand Down

0 comments on commit 664bde5

Please sign in to comment.