1
1
use std:: {
2
- future:: { poll_fn, ready , Future } ,
2
+ future:: { poll_fn, Future } ,
3
3
mem:: ManuallyDrop ,
4
4
panic:: RefUnwindSafe ,
5
5
sync:: {
@@ -9,7 +9,7 @@ use std::{
9
9
task:: Poll ,
10
10
} ;
11
11
12
- use futures_util:: { future :: Either , task:: AtomicWaker } ;
12
+ use futures_util:: task:: AtomicWaker ;
13
13
14
14
use crate :: { AsRawFd , OwnedFd , RawFd } ;
15
15
@@ -38,18 +38,18 @@ impl SharedFd {
38
38
}
39
39
40
40
/// Try to take the inner owned fd.
41
- pub fn try_owned ( self ) -> Result < OwnedFd , Self > {
41
+ pub fn try_unwrap ( self ) -> Result < OwnedFd , Self > {
42
42
let this = ManuallyDrop :: new ( self ) ;
43
- if let Some ( fd) = Self :: try_owned_inner ( & this) {
43
+ if let Some ( fd) = unsafe { Self :: try_unwrap_inner ( & this) } {
44
44
Ok ( fd)
45
45
} else {
46
46
Err ( ManuallyDrop :: into_inner ( this) )
47
47
}
48
48
}
49
49
50
- fn try_owned_inner ( this : & ManuallyDrop < Self > ) -> Option < OwnedFd > {
51
- // SAFETY: see ManuallyDrop::take
52
- let ptr = ManuallyDrop :: new ( unsafe { std:: ptr:: read ( & this. 0 ) } ) ;
50
+ // SAFETY: if `Some` is returned, the method should not be called again.
51
+ unsafe fn try_unwrap_inner ( this : & ManuallyDrop < Self > ) -> Option < OwnedFd > {
52
+ let ptr = ManuallyDrop :: new ( std:: ptr:: read ( & this. 0 ) ) ;
53
53
// The ptr is duplicated without increasing the strong count, should forget.
54
54
match Arc :: try_unwrap ( ManuallyDrop :: into_inner ( ptr) ) {
55
55
Ok ( inner) => Some ( inner. fd ) ,
@@ -62,26 +62,26 @@ impl SharedFd {
62
62
63
63
/// Wait and take the inner owned fd.
64
64
pub fn take ( self ) -> impl Future < Output = Option < OwnedFd > > {
65
- if self . 0 . waits . fetch_add ( 1 , Ordering :: AcqRel ) == 0 {
66
- let this = ManuallyDrop :: new ( self ) ;
67
- Either :: Left ( async move {
68
- poll_fn ( |cx| {
69
- if let Some ( fd) = Self :: try_owned_inner ( & this) {
65
+ let this = ManuallyDrop :: new ( self ) ;
66
+ async move {
67
+ if this . 0 . waits . fetch_add ( 1 , Ordering :: AcqRel ) == 0 {
68
+ poll_fn ( move |cx| {
69
+ if let Some ( fd) = unsafe { Self :: try_unwrap_inner ( & this) } {
70
70
return Poll :: Ready ( Some ( fd) ) ;
71
71
}
72
72
73
73
this. 0 . waker . register ( cx. waker ( ) ) ;
74
74
75
- if let Some ( fd) = Self :: try_owned_inner ( & this) {
75
+ if let Some ( fd) = unsafe { Self :: try_unwrap_inner ( & this) } {
76
76
Poll :: Ready ( Some ( fd) )
77
77
} else {
78
78
Poll :: Pending
79
79
}
80
80
} )
81
81
. await
82
- } )
83
- } else {
84
- Either :: Right ( ready ( None ) )
82
+ } else {
83
+ None
84
+ }
85
85
}
86
86
}
87
87
}
0 commit comments