Skip to content

Commit 94612af

Browse files
authored
Cleanup bevy_tasks::future module (#20744)
# Objective `Waker::noop` was stabilized in Rust 1.85, and we have a vendored version in tree. ## Solution Remove it and clean up related code. ## Testing CI
1 parent 4f49680 commit 94612af

File tree

2 files changed

+7
-40
lines changed

2 files changed

+7
-40
lines changed

crates/bevy_tasks/src/futures.rs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
#![expect(unsafe_code, reason = "Futures require unsafe code.")]
2-
31
//! Utilities for working with [`Future`]s.
42
use 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.
2922
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)
5624
}

crates/bevy_tasks/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ cfg::switch! {
132132
let mut future = core::pin::pin!(future);
133133

134134
// We don't care about the waker as we're just going to poll as fast as possible.
135-
let waker = futures::noop_waker();
136-
let cx = &mut Context::from_waker(&waker);
135+
let cx = &mut Context::from_waker(core::task::Waker::noop());
137136

138137
// Keep polling until the future is ready.
139138
loop {

0 commit comments

Comments
 (0)