Skip to content

Commit 664bde5

Browse files
committed
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.
1 parent 0a59d73 commit 664bde5

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/net.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ impl NetworkListener for HttpListener {
172172
}
173173
}
174174

175+
#[cfg(windows)]
176+
impl ::std::os::windows::io::AsRawSocket for HttpListener {
177+
fn as_raw_socket(&self) -> ::std::os::windows::io::RawSocket {
178+
self.0.as_raw_socket()
179+
}
180+
}
181+
182+
#[cfg(windows)]
183+
impl ::std::os::windows::io::FromRawSocket for HttpListener {
184+
unsafe fn from_raw_socket(sock: ::std::os::windows::io::RawSocket) -> HttpListener {
185+
HttpListener(TcpListener::from_raw_socket(sock))
186+
}
187+
}
188+
189+
#[cfg(unix)]
190+
impl ::std::os::unix::io::AsRawFd for HttpListener {
191+
fn as_raw_fd(&self) -> ::std::os::unix::io::RawFd {
192+
self.0.as_raw_fd()
193+
}
194+
}
195+
196+
#[cfg(unix)]
197+
impl ::std::os::unix::io::FromRawFd for HttpListener {
198+
unsafe fn from_raw_fd(fd: ::std::os::unix::io::RawFd) -> HttpListener {
199+
HttpListener(TcpListener::from_raw_fd(fd))
200+
}
201+
}
202+
175203
/// A wrapper around a TcpStream.
176204
pub struct HttpStream(pub TcpStream);
177205

@@ -213,13 +241,27 @@ impl ::std::os::windows::io::AsRawSocket for HttpStream {
213241
}
214242
}
215243

244+
#[cfg(windows)]
245+
impl ::std::os::windows::io::FromRawSocket for HttpStream {
246+
unsafe fn from_raw_socket(sock: ::std::os::windows::io::RawSocket) -> HttpStream {
247+
HttpStream(TcpStream::from_raw_socket(sock))
248+
}
249+
}
250+
216251
#[cfg(unix)]
217252
impl ::std::os::unix::io::AsRawFd for HttpStream {
218-
fn as_raw_fd(&self) -> i32 {
253+
fn as_raw_fd(&self) -> ::std::os::unix::io::RawFd {
219254
self.0.as_raw_fd()
220255
}
221256
}
222257

258+
#[cfg(unix)]
259+
impl ::std::os::unix::io::FromRawFd for HttpStream {
260+
unsafe fn from_raw_fd(fd: ::std::os::unix::io::RawFd) -> HttpStream {
261+
HttpStream(TcpStream::from_raw_fd(fd))
262+
}
263+
}
264+
223265
impl NetworkStream for HttpStream {
224266
#[inline]
225267
fn peer_addr(&mut self) -> io::Result<SocketAddr> {

0 commit comments

Comments
 (0)