@@ -656,14 +656,16 @@ impl<T: AsFd> Async<T> {
656
656
/// # std::io::Result::Ok(()) });
657
657
/// ```
658
658
pub fn new ( io : T ) -> io:: Result < Async < T > > {
659
- let fd = io. as_fd ( ) ;
660
-
661
659
// 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
+ }
663
664
665
+ fn new_nonblocking ( io : T ) -> io:: Result < Async < T > > {
664
666
// SAFETY: It is impossible to drop the I/O source while it is registered through
665
667
// this type.
666
- let registration = unsafe { Registration :: new ( fd ) } ;
668
+ let registration = unsafe { Registration :: new ( io . as_fd ( ) ) } ;
667
669
668
670
Ok ( Async {
669
671
source : Reactor :: get ( ) . insert_io ( registration) ?,
@@ -731,16 +733,18 @@ impl<T: AsSocket> Async<T> {
731
733
/// # std::io::Result::Ok(()) });
732
734
/// ```
733
735
pub fn new ( io : T ) -> io:: Result < Async < T > > {
734
- let borrowed = io. as_socket ( ) ;
735
-
736
736
// Put the socket in non-blocking mode.
737
- set_nonblocking ( borrowed) ?;
737
+ set_nonblocking ( io. as_socket ( ) ) ?;
738
+
739
+ Self :: new_nonblocking ( io)
740
+ }
738
741
742
+ fn new_nonblocking ( io : T ) -> io:: Result < Async < T > > {
739
743
// Create the registration.
740
744
//
741
745
// SAFETY: It is impossible to drop the I/O source while it is registered through
742
746
// this type.
743
- let registration = unsafe { Registration :: new ( borrowed ) } ;
747
+ let registration = unsafe { Registration :: new ( io . as_socket ( ) ) } ;
744
748
745
749
Ok ( Async {
746
750
source : Reactor :: get ( ) . insert_io ( registration) ?,
@@ -1479,7 +1483,8 @@ impl Async<TcpStream> {
1479
1483
1480
1484
// Begin async connect.
1481
1485
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) ) ?;
1483
1488
1484
1489
// The stream becomes writable when connected.
1485
1490
stream. writable ( ) . await ?;
@@ -1812,7 +1817,8 @@ impl Async<UnixStream> {
1812
1817
rn:: AddressFamily :: UNIX ,
1813
1818
None ,
1814
1819
) ?;
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) ) ?;
1816
1822
1817
1823
// The stream becomes writable when connected.
1818
1824
stream. writable ( ) . await ?;
0 commit comments