Skip to content

Commit 0f2af63

Browse files
committed
Avoid needless set_nonblocking calls
connect() already sets socket to non-blocking mode.
1 parent 62e3454 commit 0f2af63

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
@@ -655,14 +655,16 @@ impl<T: AsFd> Async<T> {
655655
/// # std::io::Result::Ok(()) });
656656
/// ```
657657
pub fn new(io: T) -> io::Result<Async<T>> {
658-
let fd = io.as_fd();
659-
660658
// Put the file descriptor in non-blocking mode.
661-
set_nonblocking(fd)?;
659+
set_nonblocking(io.as_fd())?;
660+
661+
Self::new_nonblocking(io)
662+
}
662663

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

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

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

744748
Ok(Async {
745749
source: Reactor::get().insert_io(registration)?,
@@ -1478,7 +1482,8 @@ impl Async<TcpStream> {
14781482

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

14831488
// The stream becomes writable when connected.
14841489
stream.writable().await?;
@@ -1811,7 +1816,8 @@ impl Async<UnixStream> {
18111816
rn::AddressFamily::UNIX,
18121817
None,
18131818
)?;
1814-
let stream = Async::new(UnixStream::from(socket))?;
1819+
// Use new_nonblocking because connect already sets socket to non-blocking mode.
1820+
let stream = Async::new_nonblocking(UnixStream::from(socket))?;
18151821

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

0 commit comments

Comments
 (0)