Skip to content

Commit

Permalink
Switch bevy_asset to core::prelude (#17442)
Browse files Browse the repository at this point in the history
Makes use of `std` explicit, simplifying a possible `no_std` port.

# Objective

- Contributes to #15460
- Simplify future `no_std` work on `bevy_asset`

## Solution

- Add `#![no_std]` to switch to `core::prelude` instead of
`std::prelude`

## Testing

- CI

---

## Notes

This is entirely a change around the names of imports and has no impact
on functionality. This just reduces the quantity of changes involved in
the (likely more controversial) `no_std`-ification of `bevy_asset`.
  • Loading branch information
bushrat011899 authored Jan 20, 2025
1 parent 5d0e9cf commit 8f32c79
Show file tree
Hide file tree
Showing 31 changed files with 109 additions and 34 deletions.
2 changes: 2 additions & 0 deletions crates/bevy_asset/src/asset_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ unsafe impl<A: AsAssetId> QueryFilter for AssetChanged<A> {
#[cfg(test)]
mod tests {
use crate::{self as bevy_asset, AssetEvents, AssetPlugin, Handle};
use alloc::{vec, vec::Vec};
use core::num::NonZero;
use std::println;

use crate::{AssetApp, Assets};
use bevy_app::{App, AppExit, Last, Startup, TaskPoolPlugin, Update};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
self as bevy_asset, Asset, AssetEvent, AssetHandleProvider, AssetId, AssetServer, Handle,
UntypedHandle,
};
use alloc::sync::Arc;
use alloc::{sync::Arc, vec::Vec};
use bevy_ecs::{
prelude::EventWriter,
system::{Res, ResMut, Resource, SystemChangeTick},
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_asset/src/folder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use crate as bevy_asset;
use crate::{Asset, UntypedHandle};
use bevy_reflect::TypePath;
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ pub enum UntypedAssetConversionError {

#[cfg(test)]
mod tests {
use alloc::boxed::Box;
use bevy_reflect::PartialReflect;
use bevy_utils::FixedHasher;
use core::hash::BuildHasher;
Expand Down
9 changes: 3 additions & 6 deletions crates/bevy_asset/src/io/android.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::io::{get_meta_path, AssetReader, AssetReaderError, PathStream, Reader, VecReader};
use alloc::{borrow::ToOwned, boxed::Box, ffi::CString, vec::Vec};
use futures_lite::stream;
use std::{ffi::CString, path::Path};
use tracing::error;
use std::path::Path;

/// [`AssetReader`] implementation for Android devices, built on top of Android's [`AssetManager`].
///
Expand Down Expand Up @@ -72,10 +72,7 @@ impl AssetReader for AndroidAssetReader {
Ok(read_dir)
}

async fn is_directory<'a>(
&'a self,
path: &'a Path,
) -> std::result::Result<bool, AssetReaderError> {
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
let asset_manager = bevy_window::ANDROID_APP
.get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/embedded/embedded_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::io::{
memory::Dir,
AssetSourceEvent, AssetWatcher,
};
use alloc::sync::Arc;
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use bevy_utils::HashMap;
use core::time::Duration;
use notify_debouncer_full::{notify::RecommendedWatcher, Debouncer, RecommendedCache};
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_asset/src/io/embedded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ use crate::io::{
memory::{Dir, MemoryAssetReader, Value},
AssetSource, AssetSourceBuilders,
};
use alloc::boxed::Box;
use bevy_ecs::system::Resource;
use std::path::{Path, PathBuf};

#[cfg(feature = "embedded_watcher")]
use alloc::borrow::ToOwned;

pub const EMBEDDED: &str = "embedded";

/// A [`Resource`] that manages "rust source files" in a virtual in memory [`Dir`], which is intended
Expand All @@ -29,7 +33,7 @@ impl EmbeddedAssetRegistry {
/// Inserts a new asset. `full_path` is the full path (as [`file`] would return for that file, if it was capable of
/// running in a non-rust file). `asset_path` is the path that will be used to identify the asset in the `embedded`
/// [`AssetSource`]. `value` is the bytes that will be returned for the asset. This can be _either_ a `&'static [u8]`
/// or a [`Vec<u8>`].
/// or a [`Vec<u8>`](alloc::vec::Vec).
#[cfg_attr(
not(feature = "embedded_watcher"),
expect(
Expand All @@ -48,7 +52,7 @@ impl EmbeddedAssetRegistry {
/// Inserts new asset metadata. `full_path` is the full path (as [`file`] would return for that file, if it was capable of
/// running in a non-rust file). `asset_path` is the path that will be used to identify the asset in the `embedded`
/// [`AssetSource`]. `value` is the bytes that will be returned for the asset. This can be _either_ a `&'static [u8]`
/// or a [`Vec<u8>`].
/// or a [`Vec<u8>`](alloc::vec::Vec).
#[cfg_attr(
not(feature = "embedded_watcher"),
expect(
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/io/file/file_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use async_fs::{read_dir, File};
use futures_io::AsyncSeek;
use futures_lite::StreamExt;

use alloc::{borrow::ToOwned, boxed::Box};
use core::{pin::Pin, task, task::Poll};
use std::path::Path;

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/io/file/file_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
io::{AssetSourceEvent, AssetWatcher},
path::normalize_path,
};
use alloc::borrow::ToOwned;
use core::time::Duration;
use crossbeam_channel::Sender;
use notify_debouncer_full::{
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/io/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod sync_file_asset;
pub use file_watcher::*;
use tracing::{debug, error};

use alloc::borrow::ToOwned;
use std::{
env,
path::{Path, PathBuf},
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/io/file/sync_file_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::io::{
PathStream, Reader, Writer,
};

use alloc::{borrow::ToOwned, boxed::Box, vec::Vec};
use core::{pin::Pin, task::Poll};
use std::{
fs::{read_dir, File},
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/gated.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
use alloc::sync::Arc;
use alloc::{boxed::Box, sync::Arc};
use bevy_utils::HashMap;
use crossbeam_channel::{Receiver, Sender};
use parking_lot::RwLock;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
use alloc::sync::Arc;
use alloc::{borrow::ToOwned, boxed::Box, sync::Arc, vec::Vec};
use bevy_utils::HashMap;
use core::{pin::Pin, task::Poll};
use futures_io::AsyncRead;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod source;
pub use futures_lite::AsyncWriteExt;
pub use source::*;

use alloc::sync::Arc;
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
use core::future::Future;
use core::{
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/processor_gated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
processor::{AssetProcessorData, ProcessStatus},
AssetPath,
};
use alloc::sync::Arc;
use alloc::{borrow::ToOwned, boxed::Box, sync::Arc, vec::Vec};
use async_lock::RwLockReadGuardArc;
use core::{pin::Pin, task::Poll};
use futures_io::AsyncRead;
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_asset/src/io/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use crate::{
io::{processor_gated::ProcessorGatedReader, AssetSourceEvent, AssetWatcher},
processor::AssetProcessorData,
};
use alloc::sync::Arc;
use alloc::{
boxed::Box,
string::{String, ToString},
sync::Arc,
};
use atomicow::CowArc;
use bevy_ecs::system::Resource;
use bevy_utils::HashMap;
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/io/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::io::{
get_meta_path, AssetReader, AssetReaderError, EmptyPathStream, PathStream, Reader, VecReader,
};
use alloc::{borrow::ToOwned, boxed::Box, format};
use js_sys::{Uint8Array, JSON};
use std::path::{Path, PathBuf};
use tracing::error;
Expand Down
18 changes: 15 additions & 3 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@
html_logo_url = "https://bevyengine.org/assets/icon.png",
html_favicon_url = "https://bevyengine.org/assets/icon.png"
)]
#![no_std]

extern crate alloc;
extern crate core;
extern crate std;

pub mod io;
pub mod meta;
Expand Down Expand Up @@ -206,7 +207,11 @@ use crate::{
io::{embedded::EmbeddedAssetRegistry, AssetSourceBuilder, AssetSourceBuilders, AssetSourceId},
processor::{AssetProcessor, Process},
};
use alloc::sync::Arc;
use alloc::{
string::{String, ToString},
sync::Arc,
vec::Vec,
};
use bevy_app::{App, Last, Plugin, PreUpdate};
use bevy_ecs::prelude::Component;
use bevy_ecs::{
Expand Down Expand Up @@ -633,7 +638,14 @@ mod tests {
Asset, AssetApp, AssetEvent, AssetId, AssetLoadError, AssetLoadFailedEvent, AssetPath,
AssetPlugin, AssetServer, Assets,
};
use alloc::sync::Arc;
use alloc::{
boxed::Box,
format,
string::{String, ToString},
sync::Arc,
vec,
vec::Vec,
};
use bevy_app::{App, TaskPoolPlugin, Update};
use bevy_ecs::{
event::EventCursor,
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use crate::{
Asset, AssetLoadError, AssetServer, AssetServerMode, Assets, Handle, UntypedAssetId,
UntypedHandle,
};
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use atomicow::CowArc;
use bevy_ecs::world::World;
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/loader_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
Asset, AssetLoadError, AssetPath, ErasedAssetLoader, ErasedLoadedAsset, Handle, LoadContext,
LoadDirectError, LoadedAsset, LoadedUntypedAsset, UntypedHandle,
};
use alloc::sync::Arc;
use alloc::{borrow::ToOwned, boxed::Box, sync::Arc};
use core::any::TypeId;

// Utility type for handling the sources of reader references
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_asset/src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};

use crate::{
self as bevy_asset, loader::AssetLoader, processor::Process, Asset, AssetPath,
DeserializeMetaError, VisitAssetDependencies,
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_asset/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::io::AssetSourceId;
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};
use atomicow::CowArc;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use core::{
Expand Down Expand Up @@ -316,7 +320,7 @@ impl<'a> AssetPath<'a> {
/// If internally a value is a static reference, the static reference will be used unchanged.
/// If internally a value is an "owned [`Arc`]", it will remain unchanged.
///
/// [`Arc`]: std::sync::Arc
/// [`Arc`]: alloc::sync::Arc
pub fn into_owned(self) -> AssetPath<'static> {
AssetPath {
source: self.source.into_owned(),
Expand All @@ -329,7 +333,7 @@ impl<'a> AssetPath<'a> {
/// If internally a value is a static reference, the static reference will be used unchanged.
/// If internally a value is an "owned [`Arc`]", the [`Arc`] will be cloned.
///
/// [`Arc`]: std::sync::Arc
/// [`Arc`]: alloc::sync::Arc
#[inline]
pub fn clone_owned(&self) -> AssetPath<'static> {
self.clone().into_owned()
Expand Down Expand Up @@ -629,6 +633,7 @@ pub(crate) fn normalize_path(path: &Path) -> PathBuf {
#[cfg(test)]
mod tests {
use crate::AssetPath;
use alloc::string::ToString;
use std::path::Path;

#[test]
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_asset/src/processor/log.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::AssetPath;
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
use async_fs::File;
use bevy_utils::HashSet;
use futures_lite::{AsyncReadExt, AsyncWriteExt};
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_asset/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ use crate::{
AssetLoadError, AssetMetaCheck, AssetPath, AssetServer, AssetServerMode, DeserializeMetaError,
MissingAssetLoaderForExtensionError,
};
use alloc::{collections::VecDeque, sync::Arc};
use alloc::{borrow::ToOwned, boxed::Box, collections::VecDeque, sync::Arc, vec, vec::Vec};
use bevy_ecs::prelude::*;
#[cfg(feature = "trace")]
use bevy_tasks::ConditionalSendFuture;
use bevy_tasks::IoTaskPool;
use bevy_utils::{HashMap, HashSet};
use futures_io::ErrorKind;
Expand All @@ -68,8 +66,13 @@ use parking_lot::RwLock;
use std::path::{Path, PathBuf};
use thiserror::Error;
use tracing::{debug, error, trace, warn};

#[cfg(feature = "trace")]
use tracing::{info_span, instrument::Instrument};
use {
alloc::string::ToString,
bevy_tasks::ConditionalSendFuture,
tracing::{info_span, instrument::Instrument},
};

/// A "background" asset processor that reads asset values from a source [`AssetSource`] (which corresponds to an [`AssetReader`](crate::io::AssetReader) / [`AssetWriter`](crate::io::AssetWriter) pair),
/// processes them in some way, and writes them to a destination [`AssetSource`].
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_asset/src/processor/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ use crate::{
AssetLoadError, AssetLoader, AssetPath, DeserializeMetaError, ErasedLoadedAsset,
MissingAssetLoaderForExtensionError, MissingAssetLoaderForTypeNameError,
};
use alloc::{
borrow::ToOwned,
boxed::Box,
string::{String, ToString},
};
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
use core::marker::PhantomData;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::boxed::Box;
use core::any::{Any, TypeId};

use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, World};
Expand Down Expand Up @@ -243,6 +244,7 @@ impl<A: Asset> FromType<Handle<A>> for ReflectHandle {

#[cfg(test)]
mod tests {
use alloc::{string::String, vec::Vec};
use core::any::TypeId;

use crate as bevy_asset;
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/saver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
io::Writer, meta::Settings, transformer::TransformedAsset, Asset, AssetLoader,
ErasedLoadedAsset, Handle, LabeledAsset, UntypedHandle,
};
use alloc::boxed::Box;
use atomicow::CowArc;
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
use bevy_utils::HashMap;
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_asset/src/server/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use crate::{
Handle, InternalAssetEvent, LoadState, RecursiveDependencyLoadState, StrongHandle,
UntypedAssetId, UntypedHandle,
};
use alloc::sync::{Arc, Weak};
use alloc::{
borrow::ToOwned,
boxed::Box,
sync::{Arc, Weak},
vec::Vec,
};
use bevy_ecs::world::World;
use bevy_tasks::Task;
use bevy_utils::{Entry, HashMap, HashSet, TypeIdMap};
Expand Down
Loading

0 comments on commit 8f32c79

Please sign in to comment.