-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Simplify bevy_tasks
Features
#19091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Simplify bevy_tasks
Features
#19091
Conversation
Self(receiver.into_future()) | ||
Self(receiver) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This did nothing, just an identity function. I'm not sure why it was being called other than just nobody noticed.
/// Re-exports of crates that are useful across Bevy. | ||
/// Not intended for external crates to use. | ||
#[doc(hidden)] | ||
pub mod exports { | ||
crate::cfg::web! { | ||
pub use js_sys; | ||
pub use wasm_bindgen; | ||
pub use wasm_bindgen_futures; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This addition is what will allow the web
feature in particular to be brought under control. But there is room for adding other fundamental crates too (e.g., maybe serde
to remove serialize
features, etc.).
A similar technique could be used to remove bevy_reflect
features as well.
@@ -43,7 +47,14 @@ critical-section = ["dep:critical-section", "portable-atomic/critical-section"] | |||
|
|||
## Enables use of browser APIs. | |||
## Note this is currently only applicable on `wasm32` architectures. | |||
web = ["dep:web-time", "dep:getrandom"] | |||
web = [ | |||
"std", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that thanks to web-time
, our web
feature isn't no_std
at this moment. There is no_std
support on their GitHub repo already merged though, so hopefully we can bring that functionality soon.
It's frustrating, because ideally we would just enable web-time
iff std
and web
are enabled, but there is no way to do that with Cargo features at this time.
#[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))] => { | ||
/// Indicates multithreading support. | ||
multi_threaded | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since aliases can be defined for arbitrary cfg
statements, we can build in that multi_threaded
only works on non-WASM, rather than needing to repeat this entire statement. Should make downstream consumption nicer too.
/// Blocks on the supplied `future`. | ||
/// This implementation will busy-wait until it is completed. | ||
/// Consider enabling the `async-io` or `futures-lite` features. | ||
pub fn block_on<T>(future: impl Future<Output = T>) -> T { | ||
use core::task::{Poll, Context}; | ||
|
||
// Pin the future on the stack. | ||
let mut future = core::pin::pin!(future); | ||
|
||
// We don't care about the waker as we're just going to poll as fast as possible. | ||
let waker = futures::noop_waker(); | ||
let cx = &mut Context::from_waker(&waker); | ||
|
||
// Keep polling until the future is ready. | ||
loop { | ||
match future.as_mut().poll(cx) { | ||
Poll::Ready(output) => return output, | ||
Poll::Pending => core::hint::spin_loop(), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is new functionality just to make the feature gates less hideous. Now consumers can always use block_on
, the features just improve performance for particular environments.
Objective
Now that
bevy_platform::cfg
is merged, we can start tidying up features. This PR starts withbevy_tasks
.Solution
critical-section
feature (it was just a re-export ofbevy_platform
anyway)std
andweb
features, relying onbevy_platform::cfg
to check for availability.futures-lite
feature to provide access to theblock_on
implementation fromfutures-lite
.block_on
that just busy-waits.wasm-bindgen
related dependencies out ofbevy_tasks
and moved them intobevy_platform
under a newexports
module.async-io
implicit feature explicit.Testing