From 664bde58d8a6b2d6ce5624ed96b8d6d68214a782 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 1 Aug 2015 23:08:26 +0200 Subject: [PATCH] feat(raw-fd): implement FromRawFd/FromRawSocket 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. --- src/net.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/net.rs b/src/net.rs index 0eb42c04bf..5c8a1774f4 100644 --- a/src/net.rs +++ b/src/net.rs @@ -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); @@ -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 {