1- #![ expect( unsafe_code, reason = "Futures require unsafe code." ) ]
2-
31//! Utilities for working with [`Future`]s.
42use core:: {
53 future:: Future ,
6- pin:: Pin ,
7- task:: { Context , Poll , RawWaker , RawWakerVTable , Waker } ,
4+ pin:: pin ,
5+ task:: { Context , Poll , Waker } ,
86} ;
97
108/// Consumes a future, polls it once, and immediately returns the output
119/// or returns `None` if it wasn't ready yet.
1210///
1311/// 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) {
2215 Poll :: Ready ( x) => Some ( x) ,
2316 _ => None ,
2417 }
@@ -27,30 +20,5 @@ pub fn now_or_never<F: Future>(mut future: F) -> Option<F::Output> {
2720/// Polls a future once, and returns the output if ready
2821/// or returns `None` if it wasn't ready yet.
2922pub 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)
5624}
0 commit comments