Skip to content

Commit

Permalink
refactor(asset): migrate bevy_asset
Browse files Browse the repository at this point in the history
  • Loading branch information
BD103 committed Sep 13, 2024
1 parent d9a78b4 commit 3fecaca
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
25 changes: 22 additions & 3 deletions crates/bevy_asset/src/io/embedded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ impl EmbeddedAssetRegistry {
/// 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>`].
#[allow(unused)]
#[cfg_attr(
not(feature = "embedded_watcher"),
expect(
unused_variables,
reason = "The `full_path` argument is not used when `embedded_watcher` is disabled."
)
)]
pub fn insert_asset(&self, full_path: PathBuf, asset_path: &Path, value: impl Into<Value>) {
#[cfg(feature = "embedded_watcher")]
self.root_paths
Expand All @@ -43,7 +49,13 @@ impl EmbeddedAssetRegistry {
/// 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>`].
#[allow(unused)]
#[cfg_attr(
not(feature = "embedded_watcher"),
expect(
unused_variables,
reason = "The `full_path` argument is not used when `embedded_watcher` is disabled."
)
)]
pub fn insert_meta(&self, full_path: &Path, asset_path: &Path, value: impl Into<Value>) {
#[cfg(feature = "embedded_watcher")]
self.root_paths
Expand All @@ -61,10 +73,17 @@ impl EmbeddedAssetRegistry {

/// Registers a `embedded` [`AssetSource`] that uses this [`EmbeddedAssetRegistry`].
// NOTE: unused_mut because embedded_watcher feature is the only mutable consumer of `let mut source`
#[allow(unused_mut)]
pub fn register_source(&self, sources: &mut AssetSourceBuilders) {
let dir = self.dir.clone();
let processed_dir = self.dir.clone();

#[cfg_attr(
not(feature = "embedded_watcher"),
expect(
unused_mut,
reason = "Variable is only mutated when `embedded_watcher` feature is enabled."
)
)]
let mut source = AssetSource::build()
.with_reader(move || Box::new(MemoryAssetReader { root: dir.clone() }))
.with_processed_reader(move || {
Expand Down
15 changes: 12 additions & 3 deletions crates/bevy_asset/src/io/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use thiserror::Error;

use super::{ErasedAssetReader, ErasedAssetWriter};

// Needed for doc strings.
#[allow(unused_imports)]
#[allow(unused_imports, reason = "Needed for documentation links.")]
use crate::io::{AssetReader, AssetWriter};

/// A reference to an "asset source", which maps to an [`AssetReader`] and/or [`AssetWriter`].
Expand Down Expand Up @@ -508,7 +507,17 @@ impl AssetSource {
/// `file_debounce_time` is the amount of time to wait (and debounce duplicate events) before returning an event.
/// Higher durations reduce duplicates but increase the amount of time before a change event is processed. If the
/// duration is set too low, some systems might surface events _before_ their filesystem has the changes.
#[allow(unused)]
#[cfg_attr(
any(
not(feature = "file_watcher"),
target_arch = "wasm32",
target_os = "android"
),
expect(
unused_variables,
reason = "The `path` and `file_debounce_wait_time` arguments are unused when on WASM, Android, or if the `file_watcher` feature is disabled."
)
)]
pub fn get_default_watcher(
path: String,
file_debounce_wait_time: Duration,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl ErasedLoadedAsset {

/// Cast this loaded asset as the given type. If the type does not match,
/// the original type-erased asset is returned.
#[allow(clippy::result_large_err)]
#[expect(clippy::result_large_err, reason = "Function returns `Self` on error.")]
pub fn downcast<A: Asset>(mut self) -> Result<LoadedAsset<A>, ErasedLoadedAsset> {
match self.value.downcast::<A>() {
Ok(value) => Ok(LoadedAsset {
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_asset/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use std::{
use thiserror::Error;

// Needed for doc strings
#[allow(unused_imports)]
#[allow(unused_imports, reason = "Needed for documentation links.")]
use crate::io::{AssetReader, AssetWriter};

/// A "background" asset processor that reads asset values from a source [`AssetSource`] (which corresponds to an [`AssetReader`] / [`AssetWriter`] pair),
Expand Down Expand Up @@ -516,7 +516,13 @@ impl AssetProcessor {
/// This info will later be used to determine whether or not to re-process an asset
///
/// This will validate transactions and recover failed transactions when necessary.
#[allow(unused)]
#[cfg_attr(
any(target_arch = "wasm32", not(feature = "multi_threaded")),
expect(
dead_code,
reason = "This function is only used when the `multi_threade` feature is enabled, and when not on WASM."
)
)]
async fn initialize(&self) -> Result<(), InitializeError> {
self.validate_transaction_log_and_recover().await;
let mut asset_infos = self.data.asset_infos.write().await;
Expand Down
17 changes: 12 additions & 5 deletions crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ impl ReflectAsset {
}

/// Equivalent of [`Assets::get_mut`]
#[allow(unsafe_code)]
pub fn get_mut<'w>(
&self,
world: &'w mut World,
handle: UntypedHandle,
) -> Option<&'w mut dyn Reflect> {
// SAFETY: unique world access
unsafe { (self.get_unchecked_mut)(world.as_unsafe_world_cell(), handle) }
#[expect(
unsafe_code,
reason = "Use of unsafe `Self::get_unchecked_mut()` function."
)]
unsafe {
(self.get_unchecked_mut)(world.as_unsafe_world_cell(), handle)
}
}

/// Equivalent of [`Assets::get_mut`], but works with an [`UnsafeWorldCell`].
Expand Down Expand Up @@ -83,7 +88,10 @@ impl ReflectAsset {
/// violating Rust's aliasing rules. To avoid this:
/// * Only call this method if you know that the [`UnsafeWorldCell`] may be used to access the corresponding `Assets<T>`
/// * Don't call this method more than once in the same scope.
#[allow(unsafe_code)]
#[expect(
unsafe_code,
reason = "This function calls unsafe code and has safety requirements."
)]
pub unsafe fn get_unchecked_mut<'w>(
&self,
world: UnsafeWorldCell<'w>,
Expand All @@ -108,7 +116,6 @@ impl ReflectAsset {
}

/// Equivalent of [`Assets::len`]
#[allow(clippy::len_without_is_empty)] // clippy expects the `is_empty` method to have the signature `(&self) -> bool`
pub fn len(&self, world: &World) -> usize {
(self.len)(world)
}
Expand Down Expand Up @@ -137,7 +144,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
get_unchecked_mut: |world, handle| {
// SAFETY: `get_unchecked_mut` must be called with `UnsafeWorldCell` having access to `Assets<A>`,
// and must ensure to only have at most one reference to it live at all times.
#[allow(unsafe_code)]
#[expect(unsafe_code, reason = "Uses `UnsafeWorldCell::get_resource_mut()`.")]
let assets = unsafe { world.get_resource_mut::<Assets<A>>().unwrap().into_inner() };
let asset = assets.get_mut(&handle.typed_debug_checked());
asset.map(|asset| asset as &mut dyn Reflect)
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_asset/src/server/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ impl AssetInfos {
.unwrap()
}

#[allow(clippy::too_many_arguments)]
#[expect(
clippy::too_many_arguments,
reason = "Arguments needed so that both `create_loading_handle_untyped()` and `get_or_create_path_handle_internal()` may share code."
)]
fn create_handle_internal(
infos: &mut HashMap<UntypedAssetId, AssetInfo>,
handle_providers: &TypeIdMap<AssetHandleProvider>,
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_asset/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ use std::{any::TypeId, path::Path, sync::Arc};
use std::{future::Future, panic::AssertUnwindSafe};
use thiserror::Error;

// Needed for doc string
#[allow(unused_imports)]
#[allow(unused_imports, reason = "Needed for documentation links.")]
use crate::io::{AssetReader, AssetWriter};

/// Loads and tracks the state of [`Asset`] values from a configured [`AssetReader`]. This can be used to kick off new asset loads and
Expand Down Expand Up @@ -1335,7 +1334,6 @@ pub fn handle_internal_asset_events(world: &mut World) {
}

/// Internal events for asset load results
#[allow(clippy::large_enum_variant)]
pub(crate) enum InternalAssetEvent {
Loaded {
id: UntypedAssetId,
Expand Down

0 comments on commit 3fecaca

Please sign in to comment.