Skip to content

Commit 18c0d60

Browse files
committed
deduplicate some assetloader code
1 parent 60161f6 commit 18c0d60

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

crates/bevy_asset/src/loader_builders.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ mod sealed {
131131
/// [`NestedLoader`] will be provided the type of asset as a type parameter on
132132
/// [`load`].
133133
///
134-
/// [`load`]: Self::load
134+
/// [`load`]: NestedLoader::load
135135
pub struct Typed {
136136
_priv: (),
137137
}
@@ -257,7 +257,7 @@ impl<'ctx, 'builder, T: sealed::Typing, M: sealed::Mode> NestedLoader<'ctx, 'bui
257257
///
258258
/// [`load`]: Self::load
259259
#[must_use]
260-
pub fn of_unknown_type(self) -> NestedLoader<'ctx, 'builder, UnknownType, M> {
260+
pub fn unknown_type(self) -> NestedLoader<'ctx, 'builder, UnknownType, M> {
261261
NestedLoader {
262262
load_context: self.load_context,
263263
meta_transform: self.meta_transform,

crates/bevy_asset/src/server/mod.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
use alloc::sync::Arc;
2121
use atomicow::CowArc;
2222
use bevy_ecs::prelude::*;
23-
use bevy_tasks::IoTaskPool;
23+
use bevy_tasks::{IoTaskPool, Task};
2424
use bevy_utils::{
2525
tracing::{error, info},
2626
HashSet,
@@ -382,14 +382,7 @@ impl AssetServer {
382382
);
383383

384384
if should_load {
385-
let owned_handle = Some(handle.clone().untyped());
386-
let server = self.clone();
387-
let task = IoTaskPool::get().spawn(async move {
388-
if let Err(err) = server.load_internal(owned_handle, path, false, None).await {
389-
error!("{}", err);
390-
}
391-
drop(guard);
392-
});
385+
let task = self.spawn_load_task(handle.clone().untyped(), path, guard);
393386

394387
#[cfg(not(any(target_arch = "wasm32", not(feature = "multi_threaded"))))]
395388
infos.pending_tasks.insert(handle.id().untyped(), task);
@@ -420,14 +413,7 @@ impl AssetServer {
420413
);
421414

422415
if should_load {
423-
let owned_handle = Some(handle.clone());
424-
let server = self.clone();
425-
let task = IoTaskPool::get().spawn(async move {
426-
if let Err(err) = server.load_internal(owned_handle, path, false, None).await {
427-
error!("{}", err);
428-
}
429-
drop(guard);
430-
});
416+
let task = self.spawn_load_task(handle.clone(), path, guard);
431417

432418
#[cfg(not(any(target_arch = "wasm32", not(feature = "multi_threaded"))))]
433419
infos.pending_tasks.insert(handle.id(), task);
@@ -439,6 +425,21 @@ impl AssetServer {
439425
handle
440426
}
441427

428+
pub(crate) fn spawn_load_task<G: Send + Sync + 'static>(
429+
&self,
430+
handle: UntypedHandle,
431+
path: AssetPath<'static>,
432+
guard: G,
433+
) -> Task<()> {
434+
let server = self.clone();
435+
IoTaskPool::get().spawn(async move {
436+
if let Err(err) = server.load_internal(Some(handle), path, false, None).await {
437+
error!("{}", err);
438+
}
439+
drop(guard);
440+
})
441+
}
442+
442443
/// Asynchronously load an asset that you do not know the type of statically. If you _do_ know the type of the asset,
443444
/// you should use [`AssetServer::load`]. If you don't know the type of the asset, but you can't use an async method,
444445
/// consider using [`AssetServer::load_untyped`].

examples/asset/asset_decompression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl AssetLoader for GzAssetLoader {
7272

7373
let uncompressed = load_context
7474
.loader()
75-
.of_unknown_type()
75+
.unknown_type()
7676
.immediate()
7777
.with_reader(&mut reader)
7878
.load(contained_path)

0 commit comments

Comments
 (0)