Skip to content

Commit e4a11c7

Browse files
author
Stjepan Glavina
committed
Simplify
1 parent 2314c2d commit e4a11c7

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ impl<T: Write> AsyncWrite for Async<T> {
624624
}
625625

626626
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
627-
Poll::Ready(self.source.shutdown_write())
627+
Poll::Ready(sys::shutdown_write(self.source.raw))
628628
}
629629
}
630630

@@ -653,7 +653,7 @@ where
653653
}
654654

655655
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
656-
Poll::Ready(self.source.shutdown_write())
656+
Poll::Ready(sys::shutdown_write(self.source.raw))
657657
}
658658
}
659659

src/parking.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
use std::collections::BTreeMap;
88
use std::fmt;
99
use std::io;
10-
use std::mem::{self, ManuallyDrop};
11-
use std::net::{Shutdown, TcpStream};
10+
use std::mem;
1211
#[cfg(unix)]
13-
use std::os::unix::io::{FromRawFd, RawFd};
12+
use std::os::unix::io::RawFd;
1413
#[cfg(windows)]
15-
use std::os::windows::io::{FromRawSocket, RawSocket};
14+
use std::os::windows::io::RawSocket;
1615
use std::panic;
1716
use std::sync::atomic::{AtomicUsize, Ordering};
1817
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
@@ -740,21 +739,4 @@ impl Source {
740739
})
741740
.await
742741
}
743-
744-
/// Shuts down the write side of the socket.
745-
///
746-
/// If this source is not a socket, the `shutdown` syscall error is ignored.
747-
pub(crate) fn shutdown_write(&self) -> io::Result<()> {
748-
// This may not be a TCP stream, but that's okay - all we do is call `shutdown()` on it.
749-
#[cfg(unix)]
750-
let stream = unsafe { ManuallyDrop::new(TcpStream::from_raw_fd(self.raw)) };
751-
#[cfg(windows)]
752-
let stream = unsafe { ManuallyDrop::new(TcpStream::from_raw_socket(self.raw)) };
753-
754-
// The only actual error may be ENOTCONN.
755-
match stream.shutdown(Shutdown::Write) {
756-
Err(err) if err.kind() == io::ErrorKind::NotConnected => Err(err),
757-
_ => Ok(()),
758-
}
759-
}
760742
}

src/sys/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
use std::io;
2+
use std::mem::ManuallyDrop;
3+
use std::net::{Shutdown, TcpStream};
4+
#[cfg(unix)]
5+
use std::os::unix::io::{FromRawFd, RawFd};
6+
#[cfg(windows)]
7+
use std::os::windows::io::{FromRawSocket, RawSocket};
8+
19
use cfg_if::cfg_if;
210

311
#[cfg(unix)]
@@ -39,3 +47,24 @@ pub struct Event {
3947
pub writable: bool,
4048
pub key: usize,
4149
}
50+
51+
/// Shuts down the write side of a socket.
52+
///
53+
/// If this source is not a socket, the `shutdown()` syscall error is ignored.
54+
pub fn shutdown_write(#[cfg(unix)] raw: RawFd, #[cfg(windows)] raw: RawSocket) -> io::Result<()> {
55+
// This may not be a TCP stream, but that's okay. All we do is call `shutdown()` on the raw
56+
// descriptor and ignore errors if it's not a socket.
57+
let res = unsafe {
58+
#[cfg(unix)]
59+
let stream = ManuallyDrop::new(TcpStream::from_raw_fd(raw));
60+
#[cfg(windows)]
61+
let stream = ManuallyDrop::new(TcpStream::from_raw_socket(raw));
62+
stream.shutdown(Shutdown::Write)
63+
};
64+
65+
// The only actual error may be ENOTCONN, ignore everything else.
66+
match res {
67+
Err(err) if err.kind() == io::ErrorKind::NotConnected => Err(err),
68+
_ => Ok(()),
69+
}
70+
}

0 commit comments

Comments
 (0)