Skip to content

Commit a4486cc

Browse files
committed
Rename Erased to DynamicType, some internal assetloader changes
1 parent 18c0d60 commit a4486cc

File tree

7 files changed

+99
-88
lines changed

7 files changed

+99
-88
lines changed

crates/bevy_asset/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async-lock = "3.0"
3636
crossbeam-channel = "0.5"
3737
downcast-rs = "1.2"
3838
disqualified = "1.0"
39+
either = "1.13"
3940
futures-io = "0.3"
4041
futures-lite = "2.0.1"
4142
blake3 = "1.5"

crates/bevy_asset/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ pub use futures_lite::{AsyncReadExt, AsyncWriteExt};
187187
pub use handle::*;
188188
pub use id::*;
189189
pub use loader::*;
190-
pub use loader_builders::{Deferred, Erased, Immediate, NestedLoader, Typed, UnknownType};
190+
pub use loader_builders::{
191+
Deferred, DynamicTyped, Immediate, NestedLoader, StaticTyped, UnknownTyped,
192+
};
191193
pub use path::*;
192194
pub use reflect::*;
193195
pub use server::*;

crates/bevy_asset/src/loader.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
io::{AssetReaderError, MissingAssetSourceError, MissingProcessedAssetReaderError, Reader},
3-
loader_builders::{Deferred, NestedLoader, Typed},
3+
loader_builders::{Deferred, NestedLoader, StaticTyped},
44
meta::{AssetHash, AssetMeta, AssetMetaDyn, ProcessedInfoMinimal, Settings},
55
path::AssetPath,
66
Asset, AssetLoadError, AssetServer, AssetServerMode, Assets, Handle, UntypedAssetId,
@@ -290,7 +290,11 @@ impl<A: Asset> AssetContainer for A {
290290
}
291291
}
292292

293-
/// An error that occurs when attempting to call [`DirectNestedLoader::load`](crate::DirectNestedLoader::load)
293+
/// An error that occurs when attempting to call [`NestedLoader::load`] which
294+
/// is configured to work [immediately].
295+
///
296+
/// [`NestedLoader::load`]: crate::NestedLoader::load
297+
/// [immediately]: crate::Immediate
294298
#[derive(Error, Debug)]
295299
#[error("Failed to load dependency {dependency:?} {error}")]
296300
pub struct LoadDirectError {
@@ -550,7 +554,7 @@ impl<'a> LoadContext<'a> {
550554

551555
/// Create a builder for loading nested assets in this context.
552556
#[must_use]
553-
pub fn loader(&mut self) -> NestedLoader<'a, '_, Typed, Deferred> {
557+
pub fn loader(&mut self) -> NestedLoader<'a, '_, StaticTyped, Deferred> {
554558
NestedLoader::new(self)
555559
}
556560

crates/bevy_asset/src/loader_builders.rs

Lines changed: 61 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,34 @@ impl ReaderRef<'_> {
3232
/// The type parameters `T` and `M` determine how this loader will load assets:
3333
/// - `T`: the typing of this loader. How do we know what type of asset to load?
3434
///
35-
/// See [`Typed`] (the default), [`Erased`], and [`UnknownType`].
35+
/// See [`StaticTyped`] (the default), [`DynamicTyped`], and [`UnknownTyped`].
3636
///
3737
/// - `M`: the load mode. Do we want to load this asset right now (in which case
3838
/// you will have to `await` the operation), or do we just want a [`Handle`],
3939
/// and leave the actual asset loading to later?
4040
///
41-
/// See [`Indirect`] (the default) and [`Direct`].
41+
/// See [`Deferred`] (the default) and [`Immediate`].
4242
///
4343
/// When configuring this builder, you can freely switch between these modes
44-
/// via functions like [`direct`]/[`indirect`].
44+
/// via functions like [`deferred`] and [`immediate`].
4545
///
4646
/// ## Typing
4747
///
4848
/// To inform the loader of what type of asset to load:
49-
/// - in [`Typed`]: statically providing a type parameter `A: Asset` to
49+
/// - in [`StaticTyped`]: statically providing a type parameter `A: Asset` to
5050
/// [`load`].
5151
///
5252
/// This is the simplest way to get a [`Handle<A>`] to the loaded asset, as
5353
/// long as you know the type of `A` at compile time.
5454
///
55-
/// - in [`Erased`]: providing the [`TypeId`] and [type name] of the asset at
56-
/// runtime.
55+
/// - in [`DynamicTyped`]: providing the [`TypeId`] of the asset at runtime.
5756
///
58-
/// If you know the type info of the asset at runtime, but not at compile
59-
/// time, use [`erased`] followed by [`load`] to start loading an asset of
60-
/// that type. This lets you get an [`UntypedHandle`] (via [`Indirect`]), or a
61-
/// [`LoadedUntypedAsset`] (via [`Direct`]).
57+
/// If you know the type ID of the asset at runtime, but not at compile time,
58+
/// use [`with_dynamic_type`] followed by [`load`] to start loading an asset
59+
/// of that type. This lets you get an [`UntypedHandle`] (via [`Deferred`]),
60+
/// or a [`LoadedUntypedAsset`] (via [`Immediate`]).
6261
///
63-
/// - in [`UnknownType`]: loading a [`LoadedUntypedAsset`], which is a
62+
/// - in [`UnknownTyped`]: loading a [`LoadedUntypedAsset`], which is a
6463
/// version of the loaded asset.
6564
///
6665
/// If you have no idea what type of asset you will be loading (not even at
@@ -106,11 +105,10 @@ impl ReaderRef<'_> {
106105
/// - `ctx`: the lifetime of the associated [`AssetServer`](crate::AssetServer) reference
107106
/// - `builder`: the lifetime of the temporary builder structs
108107
///
109-
/// [`direct`]: Self::direct
110-
/// [`indirect`]: Self::indirect
111-
/// [type name]: core::any::type_name
108+
/// [`deferred`]: Self::deferred
109+
/// [`immediate`]: Self::immediate
112110
/// [`load`]: Self::load
113-
/// [`erased`]: Self::erased
111+
/// [`with_dynamic_type`]: Self::with_dynamic_type
114112
/// [`AssetServer::load`]: crate::AssetServer::load
115113
/// [`AssetProcessor`]: crate::processor::AssetProcessor
116114
/// [`Process`]: crate::processor::Process
@@ -132,27 +130,26 @@ mod sealed {
132130
/// [`load`].
133131
///
134132
/// [`load`]: NestedLoader::load
135-
pub struct Typed {
133+
pub struct StaticTyped {
136134
_priv: (),
137135
}
138136

139-
impl sealed::Typing for Typed {}
137+
impl sealed::Typing for StaticTyped {}
140138

141139
/// [`NestedLoader`] has been configured with info on what type of asset to load
142140
/// at runtime.
143-
pub struct Erased<'builder> {
141+
pub struct DynamicTyped {
144142
asset_type_id: TypeId,
145-
asset_type_name: &'builder str,
146143
}
147144

148-
impl sealed::Typing for Erased<'_> {}
145+
impl sealed::Typing for DynamicTyped {}
149146

150147
/// [`NestedLoader`] does not know what type of asset it will be loading.
151-
pub struct UnknownType {
148+
pub struct UnknownTyped {
152149
_priv: (),
153150
}
154151

155-
impl sealed::Typing for UnknownType {}
152+
impl sealed::Typing for UnknownTyped {}
156153

157154
/// [`NestedLoader`] will create and return asset handles immediately, but only
158155
/// actually load the asset later.
@@ -171,12 +168,12 @@ impl sealed::Mode for Immediate<'_, '_> {}
171168

172169
// common to all states
173170

174-
impl<'ctx, 'builder> NestedLoader<'ctx, 'builder, Typed, Deferred> {
171+
impl<'ctx, 'builder> NestedLoader<'ctx, 'builder, StaticTyped, Deferred> {
175172
pub(crate) fn new(load_context: &'builder mut LoadContext<'ctx>) -> Self {
176173
NestedLoader {
177174
load_context,
178175
meta_transform: None,
179-
typing: Typed { _priv: () },
176+
typing: StaticTyped { _priv: () },
180177
mode: Deferred { _priv: () },
181178
}
182179
}
@@ -212,42 +209,38 @@ impl<'ctx, 'builder, T: sealed::Typing, M: sealed::Mode> NestedLoader<'ctx, 'bui
212209

213210
// convert between `T`s
214211

215-
/// When [`load`]ing, the loader will attempt to load an asset with the
216-
/// given [`TypeId`] and [type name].
212+
/// When [`load`]ing, you must pass in the asset type as a type parameter
213+
/// statically.
214+
///
215+
/// If you don't know the type statically (at compile time), consider
216+
/// [`with_dynamic_type`] or [`with_unknown_type`].
217217
///
218-
/// [type name]: core::any::type_name
218+
/// [`load`]: Self::load
219+
/// [`with_dynamic_type`]: Self::with_dynamic_type
220+
/// [`with_unknown_type`]: Self::with_unknown_type
219221
#[must_use]
220-
pub fn erased(
221-
self,
222-
asset_type_id: TypeId,
223-
asset_type_name: &'builder str,
224-
) -> NestedLoader<'ctx, 'builder, Erased, M> {
222+
pub fn with_static_type(self) -> NestedLoader<'ctx, 'builder, StaticTyped, M> {
225223
NestedLoader {
226224
load_context: self.load_context,
227225
meta_transform: self.meta_transform,
228-
typing: Erased {
229-
asset_type_id,
230-
asset_type_name,
231-
},
226+
typing: StaticTyped { _priv: () },
232227
mode: self.mode,
233228
}
234229
}
235230

236-
/// When [`load`]ing, you must pass in the asset type as a type parameter
237-
/// statically.
238-
///
239-
/// If you don't know the type statically (at compile time), consider
240-
/// [`erased`] or [`unknown_type`].
231+
/// When [`load`]ing, the loader will attempt to load an asset with the
232+
/// given [`TypeId`].
241233
///
242234
/// [`load`]: Self::load
243-
/// [`erased`]: Self::erased
244-
/// [`unknown_type`]: Self::unknown_type
245235
#[must_use]
246-
pub fn typed(self) -> NestedLoader<'ctx, 'builder, Typed, M> {
236+
pub fn with_dynamic_type(
237+
self,
238+
asset_type_id: TypeId,
239+
) -> NestedLoader<'ctx, 'builder, DynamicTyped, M> {
247240
NestedLoader {
248241
load_context: self.load_context,
249242
meta_transform: self.meta_transform,
250-
typing: Typed { _priv: () },
243+
typing: DynamicTyped { asset_type_id },
251244
mode: self.mode,
252245
}
253246
}
@@ -257,11 +250,11 @@ impl<'ctx, 'builder, T: sealed::Typing, M: sealed::Mode> NestedLoader<'ctx, 'bui
257250
///
258251
/// [`load`]: Self::load
259252
#[must_use]
260-
pub fn unknown_type(self) -> NestedLoader<'ctx, 'builder, UnknownType, M> {
253+
pub fn with_unknown_type(self) -> NestedLoader<'ctx, 'builder, UnknownTyped, M> {
261254
NestedLoader {
262255
load_context: self.load_context,
263256
meta_transform: self.meta_transform,
264-
typing: UnknownType { _priv: () },
257+
typing: UnknownTyped { _priv: () },
265258
mode: self.mode,
266259
}
267260
}
@@ -280,6 +273,7 @@ impl<'ctx, 'builder, T: sealed::Typing, M: sealed::Mode> NestedLoader<'ctx, 'bui
280273
mode: Deferred { _priv: () },
281274
}
282275
}
276+
283277
/// The [`load`] call itself will load an asset, rather than scheduling the
284278
/// loading to happen later.
285279
///
@@ -300,18 +294,18 @@ impl<'ctx, 'builder, T: sealed::Typing, M: sealed::Mode> NestedLoader<'ctx, 'bui
300294

301295
// deferred loading logic
302296

303-
impl NestedLoader<'_, '_, Typed, Deferred> {
297+
impl NestedLoader<'_, '_, StaticTyped, Deferred> {
304298
/// Retrieves a handle for the asset at the given path and adds that path as
305299
/// a dependency of this asset.
306300
///
307301
/// This requires you to know the type of asset statically.
308302
/// - If you have runtime info for what type of asset you're loading (e.g. a
309-
/// [`TypeId`]), use [`erased`].
303+
/// [`TypeId`]), use [`with_dynamic_type`].
310304
/// - If you do not know at all what type of asset you're loading, use
311-
/// [`unknown_type`].
305+
/// [`with_unknown_type`].
312306
///
313-
/// [`erased`]: Self::erased
314-
/// [`unknown_type`]: Self::unknown_type
307+
/// [`with_dynamic_type`]: Self::with_dynamic_type
308+
/// [`with_unknown_type`]: Self::with_unknown_type
315309
pub fn load<'c, A: Asset>(self, path: impl Into<AssetPath<'c>>) -> Handle<A> {
316310
let path = path.into().to_owned();
317311
let handle = if self.load_context.should_load_dependencies {
@@ -328,13 +322,14 @@ impl NestedLoader<'_, '_, Typed, Deferred> {
328322
}
329323
}
330324

331-
impl NestedLoader<'_, '_, Erased<'_>, Deferred> {
325+
impl NestedLoader<'_, '_, DynamicTyped, Deferred> {
332326
/// Retrieves a handle for the asset at the given path and adds that path as
333327
/// a dependency of this asset.
334328
///
335-
/// This requires you to pass in the asset type ID and name into [`erased`].
329+
/// This requires you to pass in the asset type ID into
330+
/// [`with_dynamic_type`].
336331
///
337-
/// [`erased`]: Self::erased
332+
/// [`with_dynamic_type`]: Self::with_dynamic_type
338333
pub fn load<'p>(self, path: impl Into<AssetPath<'p>>) -> UntypedHandle {
339334
let path = path.into().to_owned();
340335
let handle = if self.load_context.should_load_dependencies {
@@ -343,7 +338,6 @@ impl NestedLoader<'_, '_, Erased<'_>, Deferred> {
343338
.load_erased_with_meta_transform(
344339
path,
345340
self.typing.asset_type_id,
346-
self.typing.asset_type_name,
347341
self.meta_transform,
348342
(),
349343
)
@@ -353,7 +347,6 @@ impl NestedLoader<'_, '_, Erased<'_>, Deferred> {
353347
.get_or_create_path_handle_erased(
354348
path,
355349
self.typing.asset_type_id,
356-
self.typing.asset_type_name,
357350
self.meta_transform,
358351
)
359352
};
@@ -362,7 +355,7 @@ impl NestedLoader<'_, '_, Erased<'_>, Deferred> {
362355
}
363356
}
364357

365-
impl NestedLoader<'_, '_, UnknownType, Deferred> {
358+
impl NestedLoader<'_, '_, UnknownTyped, Deferred> {
366359
/// Retrieves a handle for the asset at the given path and adds that path as
367360
/// a dependency of this asset.
368361
///
@@ -445,17 +438,17 @@ impl<'builder, 'reader, T> NestedLoader<'_, '_, T, Immediate<'builder, 'reader>>
445438
}
446439
}
447440

448-
impl NestedLoader<'_, '_, Typed, Immediate<'_, '_>> {
441+
impl NestedLoader<'_, '_, StaticTyped, Immediate<'_, '_>> {
449442
/// Attempts to load the asset at the given `path` immediately.
450443
///
451444
/// This requires you to know the type of asset statically.
452445
/// - If you have runtime info for what type of asset you're loading (e.g. a
453-
/// [`TypeId`]), use [`erased`].
446+
/// [`TypeId`]), use [`with_dynamic_type`].
454447
/// - If you do not know at all what type of asset you're loading, use
455-
/// [`unknown_type`].
448+
/// [`with_unknown_type`].
456449
///
457-
/// [`erased`]: Self::erased
458-
/// [`unknown_type`]: Self::unknown_type
450+
/// [`with_dynamic_type`]: Self::with_dynamic_type
451+
/// [`with_unknown_type`]: Self::with_unknown_type
459452
pub async fn load<'p, A: Asset>(
460453
self,
461454
path: impl Into<AssetPath<'p>>,
@@ -477,12 +470,13 @@ impl NestedLoader<'_, '_, Typed, Immediate<'_, '_>> {
477470
}
478471
}
479472

480-
impl NestedLoader<'_, '_, Erased<'_>, Immediate<'_, '_>> {
473+
impl NestedLoader<'_, '_, DynamicTyped, Immediate<'_, '_>> {
481474
/// Attempts to load the asset at the given `path` immediately.
482475
///
483-
/// This requires you to pass in the asset type ID and name into [`erased`].
476+
/// This requires you to pass in the asset type ID into
477+
/// [`with_dynamic_type`].
484478
///
485-
/// [`erased`]: Self::erased
479+
/// [`with_dynamic_type`]: Self::with_dynamic_type
486480
pub async fn load<'p>(
487481
self,
488482
path: impl Into<AssetPath<'p>>,
@@ -495,7 +489,7 @@ impl NestedLoader<'_, '_, Erased<'_>, Immediate<'_, '_>> {
495489
}
496490
}
497491

498-
impl NestedLoader<'_, '_, UnknownType, Immediate<'_, '_>> {
492+
impl NestedLoader<'_, '_, UnknownTyped, Immediate<'_, '_>> {
499493
/// Attempts to load the asset at the given `path` immediately.
500494
///
501495
/// This will infer the asset type from metadata.

0 commit comments

Comments
 (0)