Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup Socket.shutdown #195

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,17 @@ impl Socket {
/// This function will cause all pending and future I/O on the specified portions to return
/// immediately with an appropriate value.
pub fn shutdown(&self, how: std::net::Shutdown) -> io::Result<()> {
use std::os::unix::io::FromRawFd;

let fd = self.as_raw_fd();
// SAFETY: Our fd is the handle the kernel has given us for a socket,
// TCP or Unix, Listener or Stream, so it is a valid file descriptor/socket.
// Create a socket2::Socket long enough to call its shutdown method
// and then forget it so the socket is not otherwise dropped here.
let s = unsafe { socket2::Socket::from_raw_fd(fd) };
let result = s.shutdown(how);
std::mem::forget(s);
result
let socket_ref = socket2::SockRef::from(self);
socket_ref.shutdown(how)
}

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