-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Bare Minimum no_std
support for bevy_asset
#19070
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?
Bare Minimum no_std
support for bevy_asset
#19070
Conversation
"Most" functionality excluding: - processor - io - saver - transformer - loader / loader_builders - server
Need to configure a custom backend for `no_std` in CI
run: cd examples/no_std/library && cargo check --no-default-features --features libm,critical-section --target x86_64-unknown-none | ||
run: cd examples/no_std/library && RUSTFLAGS='--cfg getrandom_backend="rdrand"' cargo check --no-default-features --features libm,critical-section --target x86_64-unknown-none |
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.
getrandom
is a bit frustrating from a library perspective. For no_std
support, we need to enable backend via RUSTFLAGS
. This is only needed for users on esoteric platforms, but that does make CI a little uglier.
features = [ | ||
# "bevy_color", | ||
# "bevy_state", | ||
] | ||
features = ["bevy_color", "bevy_state", "bevy_asset"] |
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.
Enabling these features to ensure they're tested for no_std
compatibility in CI.
pub(crate) struct AssetIndexAllocator { | ||
/// A monotonically increasing index. | ||
next_index: AtomicU32, | ||
recycled_queue_sender: Sender<AssetIndex>, | ||
/// This receives every recycled [`AssetIndex`]. It serves as a buffer/queue to store indices ready for reuse. | ||
recycled_queue_receiver: Receiver<AssetIndex>, | ||
recycled_sender: Sender<AssetIndex>, | ||
recycled_receiver: Receiver<AssetIndex>, | ||
recycled_queue: ConcurrentQueue<AssetIndex>, | ||
recycled: ConcurrentQueue<AssetIndex>, | ||
} |
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 crossbeam_channel
isn't no_std
compatible, I switched to concurrent_queue
for tracking recycled asset indexes. Since the sender and receiver were colocated and never shared outside the type, I actually think concurrent_queue
better matches the intended semantics anyway. This does increase the stack size of AssetIndexAllocator
though (~1024 bytes instead of ~64). Moving the queues into an Arc<...>
would solve that issue if it's contentious.
#[deprecated(since = "0.17.0", note = "use `bevy_asset::AssetSourceId` instead")] | ||
pub use crate::AssetSourceId; |
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 moved AssetSourceId
to path.rs
to make the feature gating less ugly. I also think it makes more sense to live there anyway, since it's a fundamental component of what we define as an AssetPath
.
crates/bevy_asset/src/lib.rs
Outdated
#[cfg(feature = "std")] | ||
pub mod io; | ||
pub mod meta; | ||
#[cfg(feature = "std")] | ||
pub mod processor; | ||
#[cfg(feature = "std")] | ||
pub mod saver; | ||
#[cfg(feature = "std")] | ||
pub mod transformer; |
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.
These modules, along with loader
, loader_builders
, and server
will require substantial changes to attain no_std
compatibility. To make those efforts easier to chip at, I'm just gating them entirely right now and getting the surrounding items compatible first. This will allow more targeted PRs in the future to (hopefully) bring us closer to feature parity.
#[cfg(feature = "std")] | ||
#[doc(hidden)] | ||
pub use crate::AssetServer; |
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.
AssetServer
really requires everything else to be no_std
compatible first before it can be.
crates/bevy_asset/src/lib.rs
Outdated
#[cfg(feature = "std")] | ||
mod loader; | ||
#[cfg(feature = "std")] | ||
mod loader_builders; |
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.
Future PR: I feel like these modules should be combined into a bevy_asset::loader
and bevy_asset::loader::builders
(or further split).
#[cfg(feature = "std")] | ||
self.build_asset_plugin_std(app); |
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 moved the std
-requiring parts of the plugin build
function into its own method just to make the feature gating cleaner.
} | ||
} | ||
|
||
impl Plugin for AssetPlugin { |
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.
It's important that AssetPlugin
implement Plugin
for no_std
, as the DefaultPlugins
will include AssetPlugin
if bevy_asset
is enabled on bevy
.
#[cfg(feature = "std")] | ||
type PathInner = Path; | ||
|
||
#[cfg(not(feature = "std"))] | ||
type PathInner = str; |
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.
Future PR: Cart has previously expressed a desire to move away from std::path::Path
, as AssetPath
shouldn't support platform-specific functionality like Windows prefixes or different path separators. AssetPath
should always use str
instead of Path
, but that will requiring providing our own implementations for common Path
operations (components
, file_name
, etc.).
It looks like your PR has been selected for a highlight in the next release blog post, but you didn't provide a release note. Please review the instructions for writing release notes, then expand or revise the content in the release notes directory to showcase your changes. |
Objective
no_std
forbevy_asset
#18978Solution
std
andweb
features tobevy_asset
no_std
dependencies behindstd
featurestd
primarily at thelib.rs
levelbevy_asset
enablestd
and/orweb
where requiredbevy_asset
featurescrossbeam_channel
toconcurrent_queue
where required forno_std
supporttracing
tolog
where required forno_atomic
supportAssetSourceId
tobevy_asset::path
, leaving a re-export so there's no breaking changeAssetPath
to usestr
instead ofPath
whenstd
is disabledbevy_asset
feature tono_std_library
example to ensure it's tested in CITesting
Notes
std
, opting to start with some low-hanging-fruit such as theAsset
trait andAssetPath
. Since theAssets
,Handle<T>
,Asset
, andAssetPath
items are all available, a bare-minimum workflow where assets are added individually without theAssetServer
is possible. It is my intention to chip away at thesestd
feature gates to bring greater parity.AssetLoader
AssetReader
AssetServer
AssetSaver