1
- #![ expect( unsafe_code, reason = "Futures require unsafe code." ) ]
2
-
3
1
//! Utilities for working with [`Future`]s.
4
2
use core:: {
5
3
future:: Future ,
6
- pin:: Pin ,
7
- task:: { Context , Poll , RawWaker , RawWakerVTable , Waker } ,
4
+ pin:: pin ,
5
+ task:: { Context , Poll , Waker } ,
8
6
} ;
9
7
10
8
/// Consumes a future, polls it once, and immediately returns the output
11
9
/// or returns `None` if it wasn't ready yet.
12
10
///
13
11
/// This will cancel the future if it's not ready.
14
- pub fn now_or_never < F : Future > ( mut future : F ) -> Option < F :: Output > {
15
- let noop_waker = noop_waker ( ) ;
16
- let mut cx = Context :: from_waker ( & noop_waker) ;
17
-
18
- // SAFETY: `future` is not moved and the original value is shadowed
19
- let future = unsafe { Pin :: new_unchecked ( & mut future) } ;
20
-
21
- match future. poll ( & mut cx) {
12
+ pub fn now_or_never < F : Future > ( future : F ) -> Option < F :: Output > {
13
+ let mut cx = Context :: from_waker ( Waker :: noop ( ) ) ;
14
+ match pin ! ( future) . poll ( & mut cx) {
22
15
Poll :: Ready ( x) => Some ( x) ,
23
16
_ => None ,
24
17
}
@@ -27,30 +20,5 @@ pub fn now_or_never<F: Future>(mut future: F) -> Option<F::Output> {
27
20
/// Polls a future once, and returns the output if ready
28
21
/// or returns `None` if it wasn't ready yet.
29
22
pub fn check_ready < F : Future + Unpin > ( future : & mut F ) -> Option < F :: Output > {
30
- let noop_waker = noop_waker ( ) ;
31
- let mut cx = Context :: from_waker ( & noop_waker) ;
32
-
33
- let future = Pin :: new ( future) ;
34
-
35
- match future. poll ( & mut cx) {
36
- Poll :: Ready ( x) => Some ( x) ,
37
- _ => None ,
38
- }
39
- }
40
-
41
- fn noop_clone ( _data : * const ( ) ) -> RawWaker {
42
- noop_raw_waker ( )
43
- }
44
- fn noop ( _data : * const ( ) ) { }
45
-
46
- const NOOP_WAKER_VTABLE : RawWakerVTable = RawWakerVTable :: new ( noop_clone, noop, noop, noop) ;
47
-
48
- fn noop_raw_waker ( ) -> RawWaker {
49
- RawWaker :: new ( core:: ptr:: null ( ) , & NOOP_WAKER_VTABLE )
50
- }
51
-
52
- pub ( crate ) fn noop_waker ( ) -> Waker {
53
- // SAFETY: the `RawWakerVTable` is just a big noop and doesn't violate any of the rules in `RawWakerVTable`s documentation
54
- // (which talks about retaining and releasing any "resources", of which there are none in this case)
55
- unsafe { Waker :: from_raw ( noop_raw_waker ( ) ) }
23
+ now_or_never ( future)
56
24
}
0 commit comments