Skip to content

Commit 5157c78

Browse files
authored
Move futures.rs, ConditionalSend and BoxedFuture types to bevy_tasks (#16951)
# Objective - Related to #11478 ## Solution - Moved `futures.rs`, `ConditionalSend` `ConditionalSendFuture` and `BoxedFuture` from `bevy_utils` to `bevy_tasks`. ## Testing - CI checks ## Migration Guide - Several modules were moved from `bevy_utils` into `bevy_tasks`: - Replace `bevy_utils::futures` imports with `bevy_tasks::futures`. - Replace `bevy_utils::ConditionalSend` with `bevy_tasks::ConditionalSend`. - Replace `bevy_utils::ConditionalSendFuture` with `bevy_tasks::ConditionalSendFuture`. - Replace `bevy_utils::BoxedFuture` with `bevy_tasks::BoxedFuture`.
1 parent 847c3a1 commit 5157c78

File tree

11 files changed

+50
-48
lines changed

11 files changed

+50
-48
lines changed

crates/bevy_asset/src/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use futures_lite::AsyncWriteExt;
2222
pub use source::*;
2323

2424
use alloc::sync::Arc;
25-
use bevy_utils::{BoxedFuture, ConditionalSendFuture};
25+
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
2626
use core::future::Future;
2727
use core::{
2828
mem::size_of,

crates/bevy_asset/src/loader.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::{
88
};
99
use atomicow::CowArc;
1010
use bevy_ecs::world::World;
11-
use bevy_utils::{BoxedFuture, ConditionalSendFuture, HashMap, HashSet};
11+
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
12+
use bevy_utils::{HashMap, HashSet};
1213
use core::any::{Any, TypeId};
1314
use downcast_rs::{impl_downcast, Downcast};
1415
use ron::error::SpannedError;

crates/bevy_asset/src/processor/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,15 @@ use crate::{
5858
};
5959
use alloc::{collections::VecDeque, sync::Arc};
6060
use bevy_ecs::prelude::*;
61+
#[cfg(feature = "trace")]
62+
use bevy_tasks::ConditionalSendFuture;
6163
use bevy_tasks::IoTaskPool;
64+
#[cfg(feature = "trace")]
65+
use bevy_utils::tracing::{info_span, instrument::Instrument};
6266
use bevy_utils::{
6367
tracing::{debug, error, trace, warn},
6468
HashMap, HashSet,
6569
};
66-
#[cfg(feature = "trace")]
67-
use bevy_utils::{
68-
tracing::{info_span, instrument::Instrument},
69-
ConditionalSendFuture,
70-
};
7170
use futures_io::ErrorKind;
7271
use futures_lite::{AsyncReadExt, AsyncWriteExt, StreamExt};
7372
use parking_lot::RwLock;

crates/bevy_asset/src/processor/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
AssetLoadError, AssetLoader, AssetPath, DeserializeMetaError, ErasedLoadedAsset,
1111
MissingAssetLoaderForExtensionError, MissingAssetLoaderForTypeNameError,
1212
};
13-
use bevy_utils::{BoxedFuture, ConditionalSendFuture};
13+
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
1414
use core::marker::PhantomData;
1515
use serde::{Deserialize, Serialize};
1616
use thiserror::Error;

crates/bevy_asset/src/saver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use crate::{
33
ErasedLoadedAsset, Handle, LabeledAsset, UntypedHandle,
44
};
55
use atomicow::CowArc;
6-
use bevy_utils::{BoxedFuture, ConditionalSendFuture, HashMap};
6+
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
7+
use bevy_utils::HashMap;
78
use core::{borrow::Borrow, hash::Hash, ops::Deref};
89
use serde::{Deserialize, Serialize};
910

crates/bevy_asset/src/server/loaders.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ use crate::{
44
};
55
use alloc::sync::Arc;
66
use async_broadcast::RecvError;
7+
#[cfg(feature = "trace")]
8+
use bevy_tasks::ConditionalSendFuture;
79
use bevy_tasks::IoTaskPool;
8-
use bevy_utils::{tracing::warn, HashMap, TypeIdMap};
910
#[cfg(feature = "trace")]
10-
use bevy_utils::{
11-
tracing::{info_span, instrument::Instrument},
12-
ConditionalSendFuture,
13-
};
11+
use bevy_utils::tracing::{info_span, instrument::Instrument};
12+
use bevy_utils::{tracing::warn, HashMap, TypeIdMap};
1413
use core::any::TypeId;
1514
use thiserror::Error;
1615

crates/bevy_asset/src/transformer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{meta::Settings, Asset, ErasedLoadedAsset, Handle, LabeledAsset, UntypedHandle};
22
use atomicow::CowArc;
3-
use bevy_utils::{ConditionalSendFuture, HashMap};
3+
use bevy_tasks::ConditionalSendFuture;
4+
use bevy_utils::HashMap;
45
use core::{
56
borrow::Borrow,
67
convert::Infallible,

crates/bevy_render/src/render_resource/pipeline_cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl ShaderCache {
329329
// So to keep the complexity of the ShaderCache low, we will only catch this error early on native platforms,
330330
// and on wasm the error will be handled by wgpu and crash the application.
331331
if let Some(Some(wgpu::Error::Validation { description, .. })) =
332-
bevy_utils::futures::now_or_never(error)
332+
bevy_tasks::futures::now_or_never(error)
333333
{
334334
return Err(PipelineCacheError::CreateShaderModule(description));
335335
}
@@ -874,7 +874,7 @@ impl PipelineCache {
874874
}
875875

876876
CachedPipelineState::Creating(ref mut task) => {
877-
match bevy_utils::futures::check_ready(task) {
877+
match bevy_tasks::futures::check_ready(task) {
878878
Some(Ok(pipeline)) => {
879879
cached_pipeline.state = CachedPipelineState::Ok(pipeline);
880880
return;

crates/bevy_utils/src/futures.rs renamed to crates/bevy_tasks/src/futures.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(unsafe_code, reason = "Futures require unsafe code.")]
2+
13
//! Utilities for working with [`Future`]s.
24
use core::{
35
future::Future,

crates/bevy_tasks/src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@
88

99
extern crate alloc;
1010

11+
#[cfg(not(target_arch = "wasm32"))]
12+
mod conditional_send {
13+
/// Use [`ConditionalSend`] to mark an optional Send trait bound. Useful as on certain platforms (eg. Wasm),
14+
/// futures aren't Send.
15+
pub trait ConditionalSend: Send {}
16+
impl<T: Send> ConditionalSend for T {}
17+
}
18+
19+
#[cfg(target_arch = "wasm32")]
20+
#[expect(missing_docs, reason = "Not all docs are written yet (#3492).")]
21+
mod conditional_send {
22+
pub trait ConditionalSend {}
23+
impl<T> ConditionalSend for T {}
24+
}
25+
26+
pub use conditional_send::*;
27+
28+
/// Use [`ConditionalSendFuture`] for a future with an optional Send trait bound, as on certain platforms (eg. Wasm),
29+
/// futures aren't Send.
30+
pub trait ConditionalSendFuture: core::future::Future + ConditionalSend {}
31+
impl<T: core::future::Future + ConditionalSend> ConditionalSendFuture for T {}
32+
33+
use alloc::boxed::Box;
34+
35+
/// An owned and dynamically typed Future used when you can't statically type your result or need to add some indirection.
36+
pub type BoxedFuture<'a, T> = core::pin::Pin<Box<dyn ConditionalSendFuture<Output = T> + 'a>>;
37+
38+
pub mod futures;
39+
1140
mod executor;
1241

1342
mod slice;

crates/bevy_utils/src/lib.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
22
#![expect(
33
unsafe_code,
4-
reason = "Some utilities, such as futures and cells, require unsafe code."
4+
reason = "Some utilities, such as cells, require unsafe code."
55
)]
66
#![doc(
77
html_logo_url = "https://bevyengine.org/assets/icon.png",
@@ -23,7 +23,6 @@ pub mod prelude {
2323
pub use crate::default;
2424
}
2525

26-
pub mod futures;
2726
pub mod synccell;
2827
pub mod syncunsafecell;
2928

@@ -64,9 +63,6 @@ pub use time::*;
6463
#[cfg(feature = "tracing")]
6564
pub use tracing;
6665

67-
#[cfg(feature = "alloc")]
68-
use alloc::boxed::Box;
69-
7066
#[cfg(feature = "alloc")]
7167
use core::any::TypeId;
7268
use core::{
@@ -77,32 +73,6 @@ use core::{
7773
ops::Deref,
7874
};
7975

80-
#[cfg(not(target_arch = "wasm32"))]
81-
mod conditional_send {
82-
/// Use [`ConditionalSend`] to mark an optional Send trait bound. Useful as on certain platforms (eg. Wasm),
83-
/// futures aren't Send.
84-
pub trait ConditionalSend: Send {}
85-
impl<T: Send> ConditionalSend for T {}
86-
}
87-
88-
#[cfg(target_arch = "wasm32")]
89-
#[expect(missing_docs, reason = "Not all docs are written yet (#3492).")]
90-
mod conditional_send {
91-
pub trait ConditionalSend {}
92-
impl<T> ConditionalSend for T {}
93-
}
94-
95-
pub use conditional_send::*;
96-
97-
/// Use [`ConditionalSendFuture`] for a future with an optional Send trait bound, as on certain platforms (eg. Wasm),
98-
/// futures aren't Send.
99-
pub trait ConditionalSendFuture: core::future::Future + ConditionalSend {}
100-
impl<T: core::future::Future + ConditionalSend> ConditionalSendFuture for T {}
101-
102-
/// An owned and dynamically typed Future used when you can't statically type your result or need to add some indirection.
103-
#[cfg(feature = "alloc")]
104-
pub type BoxedFuture<'a, T> = core::pin::Pin<Box<dyn ConditionalSendFuture<Output = T> + 'a>>;
105-
10676
/// A shortcut alias for [`hashbrown::hash_map::Entry`].
10777
#[cfg(feature = "alloc")]
10878
pub type Entry<'a, K, V, S = FixedHasher> = hashbrown::hash_map::Entry<'a, K, V, S>;

0 commit comments

Comments
 (0)