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