Skip to content

Commit a64446b

Browse files
bushrat011899TimJentzschchescockmockersf
authored
Create bevy_platform_support Crate (#17250)
# Objective - Contributes to #16877 ## Solution - Initial creation of `bevy_platform_support` crate. - Moved `bevy_utils::Instant` into new `bevy_platform_support` crate. - Moved `portable-atomic`, `portable-atomic-util`, and `critical-section` into new `bevy_platform_support` crate. ## Testing - CI --- ## Showcase Instead of needing code like this to import an `Arc`: ```rust #[cfg(feature = "portable-atomic")] use portable_atomic_util::Arc; #[cfg(not(feature = "portable-atomic"))] use alloc::sync::Arc; ``` We can now use: ```rust use bevy_platform_support::sync::Arc; ``` This applies to many other types, but the goal is overall the same: allowing crates to use `std`-like types without the boilerplate of conditional compilation and platform-dependencies. ## Migration Guide - Replace imports of `bevy_utils::Instant` with `bevy_platform_support::time::Instant` - Replace imports of `bevy::utils::Instant` with `bevy::platform_support::time::Instant` ## Notes - `bevy_platform_support` hasn't been reserved on `crates.io` - ~~`bevy_platform_support` is not re-exported from `bevy` at this time. It may be worthwhile exporting this crate, but I am unsure of a reasonable name to export it under (`platform_support` may be a bit wordy for user-facing).~~ - I've included an implementation of `Instant` which is suitable for `no_std` platforms that are not Wasm for the sake of eliminating feature gates around its use. It may be a controversial inclusion, so I'm happy to remove it if required. - There are many other items (`spin`, `bevy_utils::Sync(Unsafe)Cell`, etc.) which should be added to this crate. I have kept the initial scope small to demonstrate utility without making this too unwieldy. --------- Co-authored-by: TimJentzsch <[email protected]> Co-authored-by: Chris Russell <[email protected]> Co-authored-by: François Mockers <[email protected]>
1 parent edb34cd commit a64446b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+586
-197
lines changed

crates/bevy_app/Cargo.toml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,25 @@ std = [
4848
"downcast-rs/std",
4949
"bevy_utils/std",
5050
"bevy_tasks?/std",
51+
"bevy_platform_support/std",
5152
]
5253

5354
## `critical-section` provides the building blocks for synchronization primitives
5455
## on all platforms, including `no_std`.
5556
critical-section = [
56-
"portable-atomic?/critical-section",
5757
"bevy_tasks?/critical-section",
5858
"bevy_ecs/critical-section",
59+
"bevy_platform_support/critical-section",
60+
"bevy_reflect?/critical-section",
5961
]
6062

6163
## `portable-atomic` provides additional platform support for atomic types and
6264
## operations, even on targets without native support.
6365
portable-atomic = [
64-
"dep:portable-atomic",
65-
"dep:portable-atomic-util",
6666
"bevy_tasks?/portable-atomic",
6767
"bevy_ecs/portable-atomic",
68+
"bevy_platform_support/portable-atomic",
69+
"bevy_reflect?/portable-atomic",
6870
]
6971

7072
[dependencies]
@@ -76,19 +78,14 @@ bevy_utils = { path = "../bevy_utils", version = "0.16.0-dev", default-features
7678
"alloc",
7779
] }
7880
bevy_tasks = { path = "../bevy_tasks", version = "0.16.0-dev", default-features = false, optional = true }
81+
bevy_platform_support = { path = "../bevy_platform_support", version = "0.16.0-dev", default-features = false }
7982

8083
# other
8184
downcast-rs = { version = "2", default-features = false }
8285
thiserror = { version = "2", default-features = false }
8386
variadics_please = "1.1"
8487
tracing = { version = "0.1", default-features = false, optional = true }
8588
log = { version = "0.4", default-features = false }
86-
portable-atomic = { version = "1", default-features = false, features = [
87-
"fallback",
88-
], optional = true }
89-
portable-atomic-util = { version = "0.2.4", features = [
90-
"alloc",
91-
], optional = true }
9289

9390
[target.'cfg(any(unix, windows))'.dependencies]
9491
ctrlc = { version = "3.4.4", optional = true }

crates/bevy_app/src/schedule_runner.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ use crate::{
33
plugin::Plugin,
44
PluginsState,
55
};
6+
use bevy_platform_support::time::Instant;
67
use core::time::Duration;
78

8-
#[cfg(any(target_arch = "wasm32", feature = "std"))]
9-
use bevy_utils::Instant;
10-
119
#[cfg(target_arch = "wasm32")]
1210
use {
1311
alloc::{boxed::Box, rc::Rc},
@@ -100,7 +98,6 @@ impl Plugin for ScheduleRunnerPlugin {
10098
let tick = move |app: &mut App,
10199
_wait: Option<Duration>|
102100
-> Result<Option<Duration>, AppExit> {
103-
#[cfg(any(target_arch = "wasm32", feature = "std"))]
104101
let start_time = Instant::now();
105102

106103
app.update();
@@ -109,10 +106,8 @@ impl Plugin for ScheduleRunnerPlugin {
109106
return Err(exit);
110107
};
111108

112-
#[cfg(any(target_arch = "wasm32", feature = "std"))]
113109
let end_time = Instant::now();
114110

115-
#[cfg(any(target_arch = "wasm32", feature = "std"))]
116111
if let Some(wait) = _wait {
117112
let exe_time = end_time - start_time;
118113
if exe_time < wait {

crates/bevy_app/src/task_pool_plugin.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22
feature = "portable-atomic",
33
expect(
44
clippy::redundant_closure,
5-
reason = "portable_atomic_util::Arc has subtly different implicit behavior"
5+
reason = "bevy_platform_support::sync::Arc has subtly different implicit behavior"
66
)
77
)]
88

99
use crate::{App, Plugin};
1010

1111
use alloc::string::ToString;
12+
use bevy_platform_support::sync::Arc;
1213
use bevy_tasks::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool, TaskPoolBuilder};
1314
use core::{fmt::Debug, marker::PhantomData};
1415
use log::trace;
1516

1617
#[cfg(not(target_arch = "wasm32"))]
1718
use {crate::Last, bevy_ecs::prelude::NonSend};
1819

19-
#[cfg(feature = "portable-atomic")]
20-
use portable_atomic_util::Arc;
21-
22-
#[cfg(not(feature = "portable-atomic"))]
23-
use alloc::sync::Arc;
24-
2520
#[cfg(not(target_arch = "wasm32"))]
2621
use bevy_tasks::tick_global_task_pools_on_main_thread;
2722

crates/bevy_core_pipeline/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ bevy_transform = { path = "../bevy_transform", version = "0.16.0-dev" }
3535
bevy_math = { path = "../bevy_math", version = "0.16.0-dev" }
3636
bevy_utils = { path = "../bevy_utils", version = "0.16.0-dev" }
3737
bevy_window = { path = "../bevy_window", version = "0.16.0-dev" }
38+
bevy_platform_support = { path = "../bevy_platform_support", version = "0.16.0-dev", default-features = false, features = [
39+
"std",
40+
] }
3841

3942
serde = { version = "1", features = ["derive"] }
4043
bitflags = "2.3"

crates/bevy_core_pipeline/src/oit/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bevy_app::prelude::*;
44
use bevy_asset::{load_internal_asset, Handle};
55
use bevy_ecs::{component::*, prelude::*};
66
use bevy_math::UVec2;
7+
use bevy_platform_support::time::Instant;
78
use bevy_reflect::Reflect;
89
use bevy_render::{
910
camera::{Camera, ExtractedCamera},
@@ -16,7 +17,7 @@ use bevy_render::{
1617
view::Msaa,
1718
Render, RenderApp, RenderSet,
1819
};
19-
use bevy_utils::{HashSet, Instant};
20+
use bevy_utils::HashSet;
2021
use bevy_window::PrimaryWindow;
2122
use resolve::{
2223
node::{OitResolveNode, OitResolvePass},

crates/bevy_diagnostic/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.16.0-dev" }
2121
bevy_time = { path = "../bevy_time", version = "0.16.0-dev" }
2222
bevy_utils = { path = "../bevy_utils", version = "0.16.0-dev" }
2323
bevy_tasks = { path = "../bevy_tasks", version = "0.16.0-dev" }
24+
bevy_platform_support = { path = "../bevy_platform_support", version = "0.16.0-dev", default-features = false, features = [
25+
"std",
26+
] }
2427

2528
# other
2629
const-fnv1a-hash = "1.1.0"

crates/bevy_diagnostic/src/diagnostic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use core::{
66

77
use bevy_app::{App, SubApp};
88
use bevy_ecs::system::{Deferred, Res, Resource, SystemBuffer, SystemParam};
9-
use bevy_utils::{HashMap, Instant, PassHash};
9+
use bevy_platform_support::time::Instant;
10+
use bevy_utils::{HashMap, PassHash};
1011
use const_fnv1a_hash::fnv1a_hash_str_64;
1112

1213
use crate::DEFAULT_MAX_HISTORY_LENGTH;

crates/bevy_ecs/Cargo.toml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,25 @@ std = [
7676
"nonmax/std",
7777
"arrayvec?/std",
7878
"log/std",
79+
"bevy_platform_support/std",
7980
]
8081

8182
## `critical-section` provides the building blocks for synchronization primitives
8283
## on all platforms, including `no_std`.
8384
critical-section = [
84-
"dep:critical-section",
8585
"bevy_tasks?/critical-section",
86-
"portable-atomic?/critical-section",
86+
"bevy_platform_support/critical-section",
87+
"bevy_reflect?/critical-section",
8788
]
8889

8990
## `portable-atomic` provides additional platform support for atomic types and
9091
## operations, even on targets without native support.
9192
portable-atomic = [
92-
"dep:portable-atomic",
93-
"dep:portable-atomic-util",
9493
"bevy_tasks?/portable-atomic",
94+
"bevy_platform_support/portable-atomic",
9595
"concurrent-queue/portable-atomic",
9696
"spin/portable_atomic",
97+
"bevy_reflect?/portable-atomic",
9798
]
9899

99100
[dependencies]
@@ -104,6 +105,9 @@ bevy_utils = { path = "../bevy_utils", version = "0.16.0-dev", default-features
104105
"alloc",
105106
] }
106107
bevy_ecs_macros = { path = "macros", version = "0.16.0-dev" }
108+
bevy_platform_support = { path = "../bevy_platform_support", version = "0.16.0-dev", default-features = false, features = [
109+
"alloc",
110+
] }
107111

108112
bitflags = { version = "2.3", default-features = false }
109113
concurrent-queue = { version = "2.5.0", default-features = false }
@@ -124,13 +128,6 @@ arrayvec = { version = "0.7.4", default-features = false, optional = true }
124128
smallvec = { version = "1", features = ["union"] }
125129
indexmap = { version = "2.5.0", default-features = false }
126130
variadics_please = { version = "1.1", default-features = false }
127-
critical-section = { version = "1.2.0", optional = true }
128-
portable-atomic = { version = "1", default-features = false, features = [
129-
"fallback",
130-
], optional = true }
131-
portable-atomic-util = { version = "0.2.4", features = [
132-
"alloc",
133-
], optional = true }
134131
spin = { version = "0.9.8", default-features = false, features = [
135132
"spin_mutex",
136133
"rwlock",

crates/bevy_ecs/src/component.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
use alloc::boxed::Box;
1616
use alloc::{borrow::Cow, format, vec::Vec};
1717
pub use bevy_ecs_macros::Component;
18+
use bevy_platform_support::sync::Arc;
1819
use bevy_ptr::{OwningPtr, UnsafeCellDeref};
1920
#[cfg(feature = "bevy_reflect")]
2021
use bevy_reflect::Reflect;
@@ -32,12 +33,6 @@ use core::{
3233
use disqualified::ShortName;
3334
use thiserror::Error;
3435

35-
#[cfg(feature = "portable-atomic")]
36-
use portable_atomic_util::Arc;
37-
38-
#[cfg(not(feature = "portable-atomic"))]
39-
use alloc::sync::Arc;
40-
4136
pub use bevy_ecs_macros::require;
4237

4338
/// A data type that can be used to store data for an [entity].

crates/bevy_ecs/src/entity/clone_entities.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
use alloc::{borrow::ToOwned, vec::Vec};
2+
use bevy_platform_support::sync::Arc;
23
use bevy_ptr::{Ptr, PtrMut};
4+
use bevy_utils::{HashMap, HashSet};
35
use bumpalo::Bump;
46
use core::{any::TypeId, ptr::NonNull};
57

6-
use bevy_utils::{HashMap, HashSet};
7-
88
#[cfg(feature = "bevy_reflect")]
99
use alloc::boxed::Box;
1010

11-
#[cfg(feature = "portable-atomic")]
12-
use portable_atomic_util::Arc;
13-
14-
#[cfg(not(feature = "portable-atomic"))]
15-
use alloc::sync::Arc;
16-
1711
use crate::{
1812
bundle::Bundle,
1913
component::{Component, ComponentCloneHandler, ComponentId, ComponentInfo, Components},

crates/bevy_ecs/src/entity/entity_set.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ use core::{
1313

1414
use super::Entity;
1515

16-
#[cfg(feature = "portable-atomic")]
17-
use portable_atomic_util::Arc;
18-
19-
#[cfg(not(feature = "portable-atomic"))]
20-
use alloc::sync::Arc;
16+
use bevy_platform_support::sync::Arc;
2117

2218
/// A trait for entity borrows.
2319
///

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use crate::{
7070
storage::{SparseSetIndex, TableId, TableRow},
7171
};
7272
use alloc::vec::Vec;
73+
use bevy_platform_support::sync::atomic::Ordering;
7374
use core::{fmt, hash::Hash, mem, num::NonZero};
7475
use log::warn;
7576

@@ -79,26 +80,16 @@ use core::panic::Location;
7980
#[cfg(feature = "serialize")]
8081
use serde::{Deserialize, Serialize};
8182

82-
#[cfg(not(feature = "portable-atomic"))]
83-
use core::sync::atomic::Ordering;
84-
85-
#[cfg(feature = "portable-atomic")]
86-
use portable_atomic::Ordering;
87-
88-
#[cfg(all(target_has_atomic = "64", not(feature = "portable-atomic")))]
89-
use core::sync::atomic::AtomicI64 as AtomicIdCursor;
90-
#[cfg(all(target_has_atomic = "64", feature = "portable-atomic"))]
91-
use portable_atomic::AtomicI64 as AtomicIdCursor;
83+
#[cfg(target_has_atomic = "64")]
84+
use bevy_platform_support::sync::atomic::AtomicI64 as AtomicIdCursor;
9285
#[cfg(target_has_atomic = "64")]
9386
type IdCursor = i64;
9487

9588
/// Most modern platforms support 64-bit atomics, but some less-common platforms
9689
/// do not. This fallback allows compilation using a 32-bit cursor instead, with
9790
/// the caveat that some conversions may fail (and panic) at runtime.
98-
#[cfg(all(not(target_has_atomic = "64"), not(feature = "portable-atomic")))]
99-
use core::sync::atomic::AtomicIsize as AtomicIdCursor;
100-
#[cfg(all(not(target_has_atomic = "64"), feature = "portable-atomic"))]
101-
use portable_atomic::AtomicIsize as AtomicIdCursor;
91+
#[cfg(not(target_has_atomic = "64"))]
92+
use bevy_platform_support::sync::atomic::AtomicIsize as AtomicIdCursor;
10293
#[cfg(not(target_has_atomic = "64"))]
10394
type IdCursor = isize;
10495

crates/bevy_ecs/src/schedule/executor/multi_threaded.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloc::{boxed::Box, vec::Vec};
2+
use bevy_platform_support::sync::Arc;
23
use bevy_tasks::{ComputeTaskPool, Scope, TaskPool, ThreadExecutor};
34
use bevy_utils::{default, syncunsafecell::SyncUnsafeCell};
45
use concurrent_queue::ConcurrentQueue;
@@ -12,12 +13,6 @@ use std::{
1213
#[cfg(feature = "trace")]
1314
use tracing::{info_span, Span};
1415

15-
#[cfg(feature = "portable-atomic")]
16-
use portable_atomic_util::Arc;
17-
18-
#[cfg(not(feature = "portable-atomic"))]
19-
use alloc::sync::Arc;
20-
2116
use crate::{
2217
archetype::ArchetypeComponentId,
2318
prelude::Resource,

crates/bevy_ecs/src/world/identifier.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ use crate::{
44
system::{ExclusiveSystemParam, ReadOnlySystemParam, SystemMeta, SystemParam},
55
world::{FromWorld, World},
66
};
7-
8-
#[cfg(not(feature = "portable-atomic"))]
9-
use core::sync::atomic::{AtomicUsize, Ordering};
10-
11-
#[cfg(feature = "portable-atomic")]
12-
use portable_atomic::{AtomicUsize, Ordering};
7+
use bevy_platform_support::sync::atomic::{AtomicUsize, Ordering};
138

149
use super::unsafe_world_cell::UnsafeWorldCell;
1510

crates/bevy_ecs/src/world/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,15 @@ use crate::{
5353
},
5454
};
5555
use alloc::{boxed::Box, vec::Vec};
56+
use bevy_platform_support::sync::atomic::{AtomicU32, Ordering};
5657
use bevy_ptr::{OwningPtr, Ptr};
57-
use core::{any::TypeId, fmt};
58+
use core::{any::TypeId, fmt, panic::Location};
5859
use log::warn;
59-
60-
#[cfg(not(feature = "portable-atomic"))]
61-
use core::sync::atomic::{AtomicU32, Ordering};
62-
63-
#[cfg(feature = "portable-atomic")]
64-
use portable_atomic::{AtomicU32, Ordering};
60+
use unsafe_world_cell::{UnsafeEntityCell, UnsafeWorldCell};
6561

6662
#[cfg(feature = "track_location")]
6763
use bevy_ptr::UnsafeCellDeref;
6864

69-
use core::panic::Location;
70-
71-
use unsafe_world_cell::{UnsafeEntityCell, UnsafeWorldCell};
72-
7365
/// Stores and exposes operations on [entities](Entity), [components](Component), resources,
7466
/// and their associated metadata.
7567
///

crates/bevy_ecs/src/world/unsafe_world_cell.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,13 @@ use crate::{
1515
system::Resource,
1616
world::RawCommandQueue,
1717
};
18+
use bevy_platform_support::sync::atomic::Ordering;
1819
use bevy_ptr::Ptr;
19-
#[cfg(feature = "track_location")]
20-
use bevy_ptr::UnsafeCellDeref;
21-
#[cfg(feature = "track_location")]
22-
use core::panic::Location;
2320
use core::{any::TypeId, cell::UnsafeCell, fmt::Debug, marker::PhantomData, ptr};
2421
use thiserror::Error;
2522

26-
#[cfg(not(feature = "portable-atomic"))]
27-
use core::sync::atomic::Ordering;
28-
29-
#[cfg(feature = "portable-atomic")]
30-
use portable_atomic::Ordering;
23+
#[cfg(feature = "track_location")]
24+
use {bevy_ptr::UnsafeCellDeref, core::panic::Location};
3125

3226
/// Variant of the [`World`] where resource and component accesses take `&self`, and the responsibility to avoid
3327
/// aliasing violations are given to the caller instead of being checked at compile-time by rust's unique XOR shared rule.

0 commit comments

Comments
 (0)