Skip to content

Commit b7617e6

Browse files
committed
Avoid needless set_nonblocking calls
connect() already sets socket to non-blocking mode.
1 parent 59ee2ea commit b7617e6

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/lib.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -656,14 +656,16 @@ impl<T: AsFd> Async<T> {
656656
/// # std::io::Result::Ok(()) });
657657
/// ```
658658
pub fn new(io: T) -> io::Result<Async<T>> {
659-
let fd = io.as_fd();
660-
661659
// Put the file descriptor in non-blocking mode.
662-
set_nonblocking(fd)?;
660+
set_nonblocking(io.as_fd())?;
661+
662+
Self::new_nonblocking(io)
663+
}
663664

665+
fn new_nonblocking(io: T) -> io::Result<Async<T>> {
664666
// SAFETY: It is impossible to drop the I/O source while it is registered through
665667
// this type.
666-
let registration = unsafe { Registration::new(fd) };
668+
let registration = unsafe { Registration::new(io.as_fd()) };
667669

668670
Ok(Async {
669671
source: Reactor::get().insert_io(registration)?,
@@ -731,16 +733,18 @@ impl<T: AsSocket> Async<T> {
731733
/// # std::io::Result::Ok(()) });
732734
/// ```
733735
pub fn new(io: T) -> io::Result<Async<T>> {
734-
let borrowed = io.as_socket();
735-
736736
// Put the socket in non-blocking mode.
737-
set_nonblocking(borrowed)?;
737+
set_nonblocking(io.as_socket())?;
738+
739+
Self::new_nonblocking(io)
740+
}
738741

742+
fn new_nonblocking(io: T) -> io::Result<Async<T>> {
739743
// Create the registration.
740744
//
741745
// SAFETY: It is impossible to drop the I/O source while it is registered through
742746
// this type.
743-
let registration = unsafe { Registration::new(borrowed) };
747+
let registration = unsafe { Registration::new(io.as_socket()) };
744748

745749
Ok(Async {
746750
source: Reactor::get().insert_io(registration)?,
@@ -1479,7 +1483,8 @@ impl Async<TcpStream> {
14791483

14801484
// Begin async connect.
14811485
let socket = connect(sock_addr, domain, Some(rn::ipproto::TCP))?;
1482-
let stream = Async::new(TcpStream::from(socket))?;
1486+
// Use new_nonblocking because connect already sets socket to non-blocking mode.
1487+
let stream = Async::new_nonblocking(TcpStream::from(socket))?;
14831488

14841489
// The stream becomes writable when connected.
14851490
stream.writable().await?;
@@ -1812,7 +1817,8 @@ impl Async<UnixStream> {
18121817
rn::AddressFamily::UNIX,
18131818
None,
18141819
)?;
1815-
let stream = Async::new(UnixStream::from(socket))?;
1820+
// Use new_nonblocking because connect already sets socket to non-blocking mode.
1821+
let stream = Async::new_nonblocking(UnixStream::from(socket))?;
18161822

18171823
// The stream becomes writable when connected.
18181824
stream.writable().await?;

0 commit comments

Comments
 (0)