Skip to content

Commit 50548ed

Browse files
committed
Implement IntoRawFd for TcpSocket
1 parent b2b83d4 commit 50548ed

File tree

1 file changed

+64
-44
lines changed

1 file changed

+64
-44
lines changed

src/net/tcp/socket.rs

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use crate::net::{TcpStream, TcpListener};
2-
use crate::sys;
3-
41
use std::io;
52
use std::mem;
63
use std::net::SocketAddr;
7-
use std::time::Duration;
84
#[cfg(unix)]
9-
use std::os::unix::io::{AsRawFd, RawFd, FromRawFd};
5+
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
106
#[cfg(windows)]
117
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
8+
use std::time::Duration;
9+
10+
use crate::net::{TcpListener, TcpStream};
11+
use crate::sys;
1212

1313
/// A non-blocking TCP socket used to configure a stream or listener.
1414
///
@@ -50,18 +50,14 @@ impl TcpSocket {
5050
///
5151
/// This calls `socket(2)`.
5252
pub fn new_v4() -> io::Result<TcpSocket> {
53-
sys::tcp::new_v4_socket().map(|sys| {
54-
TcpSocket { sys }
55-
})
53+
sys::tcp::new_v4_socket().map(|sys| TcpSocket { sys })
5654
}
5755

5856
/// Create a new IPv6 TCP socket.
5957
///
6058
/// This calls `socket(2)`.
6159
pub fn new_v6() -> io::Result<TcpSocket> {
62-
sys::tcp::new_v6_socket().map(|sys| {
63-
TcpSocket { sys }
64-
})
60+
sys::tcp::new_v6_socket().map(|sys| TcpSocket { sys })
6561
}
6662

6763
pub(crate) fn new_for_addr(addr: SocketAddr) -> io::Result<TcpSocket> {
@@ -230,7 +226,7 @@ impl TcpSocket {
230226
/// let socket = TcpSocket::new_v6()?;
231227
/// let keepalive = TcpKeepalive::default()
232228
/// .with_time(Duration::from_secs(4));
233-
/// // Depending on the target operating system, we may also be able to
229+
/// // Depending on the target operating system, we may also be able to
234230
/// // configure the keepalive probe interval and/or the number of retries
235231
/// // here as well.
236232
///
@@ -274,13 +270,16 @@ impl TcpSocket {
274270
///
275271
/// Some platforms specify this value in seconds, so sub-second
276272
/// specifications may be omitted.
277-
#[cfg_attr(docsrs, doc(cfg(any(
278-
target_os = "linux",
279-
target_os = "macos",
280-
target_os = "ios",
281-
target_os = "freebsd",
282-
target_os = "netbsd",
283-
))))]
273+
#[cfg_attr(
274+
docsrs,
275+
doc(cfg(any(
276+
target_os = "linux",
277+
target_os = "macos",
278+
target_os = "ios",
279+
target_os = "freebsd",
280+
target_os = "netbsd",
281+
)))
282+
)]
284283
#[cfg(any(
285284
target_os = "linux",
286285
target_os = "macos",
@@ -300,15 +299,18 @@ impl TcpSocket {
300299
/// This returns the value of `TCP_KEEPCNT` on Unix operating systems that
301300
/// support this option. On Windows, it is not possible to access the value
302301
/// of TCP keepalive parameters after they have been set.
303-
#[cfg_attr(docsrs, doc(cfg(any(
304-
target_os = "linux",
305-
target_os = "macos",
306-
target_os = "ios",
307-
target_os = "freebsd",
308-
target_os = "netbsd",
309-
))))]
302+
#[cfg_attr(
303+
docsrs,
304+
doc(cfg(any(
305+
target_os = "linux",
306+
target_os = "macos",
307+
target_os = "ios",
308+
target_os = "freebsd",
309+
target_os = "netbsd",
310+
)))
311+
)]
310312
#[cfg(any(
311-
target_os = "linux",
313+
target_os = "linux",
312314
target_os = "macos",
313315
target_os = "ios",
314316
target_os = "freebsd",
@@ -332,6 +334,16 @@ impl Drop for TcpSocket {
332334
}
333335
}
334336

337+
#[cfg(unix)]
338+
impl IntoRawFd for TcpSocket {
339+
fn into_raw_fd(self) -> RawFd {
340+
let ret = self.sys;
341+
// Avoid closing the socket
342+
mem::forget(self);
343+
ret
344+
}
345+
}
346+
335347
#[cfg(unix)]
336348
impl AsRawFd for TcpSocket {
337349
fn as_raw_fd(&self) -> RawFd {
@@ -384,7 +396,9 @@ impl FromRawSocket for TcpSocket {
384396
/// The caller is responsible for ensuring that the socket is in
385397
/// non-blocking mode.
386398
unsafe fn from_raw_socket(socket: RawSocket) -> TcpSocket {
387-
TcpSocket { sys: socket as sys::tcp::TcpSocket }
399+
TcpSocket {
400+
sys: socket as sys::tcp::TcpSocket,
401+
}
388402
}
389403
}
390404

@@ -413,14 +427,17 @@ impl TcpKeepalive {
413427
///
414428
/// Some platforms specify this value in seconds, so sub-second
415429
/// specifications may be omitted.
416-
#[cfg_attr(docsrs, doc(cfg(any(
417-
target_os = "linux",
418-
target_os = "macos",
419-
target_os = "ios",
420-
target_os = "freebsd",
421-
target_os = "netbsd",
422-
target_os = "windows"
423-
))))]
430+
#[cfg_attr(
431+
docsrs,
432+
doc(cfg(any(
433+
target_os = "linux",
434+
target_os = "macos",
435+
target_os = "ios",
436+
target_os = "freebsd",
437+
target_os = "netbsd",
438+
target_os = "windows"
439+
)))
440+
)]
424441
#[cfg(any(
425442
target_os = "linux",
426443
target_os = "macos",
@@ -441,13 +458,16 @@ impl TcpKeepalive {
441458
///
442459
/// This will set the value of `TCP_KEEPCNT` on Unix operating systems that
443460
/// support this option.
444-
#[cfg_attr(docsrs, doc(cfg(any(
445-
target_os = "linux",
446-
target_os = "macos",
447-
target_os = "ios",
448-
target_os = "freebsd",
449-
target_os = "netbsd",
450-
))))]
461+
#[cfg_attr(
462+
docsrs,
463+
doc(cfg(any(
464+
target_os = "linux",
465+
target_os = "macos",
466+
target_os = "ios",
467+
target_os = "freebsd",
468+
target_os = "netbsd",
469+
)))
470+
)]
451471
#[cfg(any(
452472
target_os = "linux",
453473
target_os = "macos",
@@ -466,4 +486,4 @@ impl TcpKeepalive {
466486
pub fn new() -> Self {
467487
Self::default()
468488
}
469-
}
489+
}

0 commit comments

Comments
 (0)