Skip to content

Commit 3b2174e

Browse files
authored
chore: cleanup Socket.shutdown (tokio-rs#195)
Use the convenient socket2::SockRef::from to get a socket reference which can then be shutdown. Add doc to the Socket.set_nodelay as it is pub. Maybe the Socket is itself made pub someday. This just keeps the shutdown and set_nodelay code in sync.
1 parent f6cb0ed commit 3b2174e

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/io/socket.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,17 @@ impl Socket {
234234
/// This function will cause all pending and future I/O on the specified portions to return
235235
/// immediately with an appropriate value.
236236
pub fn shutdown(&self, how: std::net::Shutdown) -> io::Result<()> {
237-
use std::os::unix::io::FromRawFd;
238-
239-
let fd = self.as_raw_fd();
240-
// SAFETY: Our fd is the handle the kernel has given us for a socket,
241-
// TCP or Unix, Listener or Stream, so it is a valid file descriptor/socket.
242-
// Create a socket2::Socket long enough to call its shutdown method
243-
// and then forget it so the socket is not otherwise dropped here.
244-
let s = unsafe { socket2::Socket::from_raw_fd(fd) };
245-
let result = s.shutdown(how);
246-
std::mem::forget(s);
247-
result
237+
let socket_ref = socket2::SockRef::from(self);
238+
socket_ref.shutdown(how)
248239
}
249240

241+
/// Set the value of the `TCP_NODELAY` option on this socket.
242+
///
243+
/// If set, this option disables the Nagle algorithm. This means that
244+
/// segments are always sent as soon as possible, even if there is only a
245+
/// small amount of data. When not set, data is buffered until there is a
246+
/// sufficient amount to send out, thereby avoiding the frequent sending of
247+
/// small packets.
250248
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
251249
let socket_ref = socket2::SockRef::from(self);
252250
socket_ref.set_nodelay(nodelay)

0 commit comments

Comments
 (0)