pub struct LoadContext<'a> { /* private fields */ }
Expand description
A context that provides access to assets in AssetLoader
s, tracks dependencies, and collects asset load state.
Any asset state accessed by LoadContext
will be tracked and stored for use in dependency events and asset preprocessing.
Implementations§
Source§impl<'a> LoadContext<'a>
impl<'a> LoadContext<'a>
Sourcepub fn begin_labeled_asset(&self) -> LoadContext<'_>
pub fn begin_labeled_asset(&self) -> LoadContext<'_>
Begins a new labeled asset load. Use the returned LoadContext
to load
dependencies for the new asset and call LoadContext::finish
to finalize the asset load.
When finished, make sure you call LoadContext::add_labeled_asset
to add the results back to the parent
context.
Prefer LoadContext::labeled_asset_scope
when possible, which will automatically add
the labeled LoadContext
back to the parent context.
LoadContext::begin_labeled_asset
exists largely to enable parallel asset loading.
See AssetPath
for more on labeled assets.
let mut handles = Vec::new();
for i in 0..2 {
let mut labeled = load_context.begin_labeled_asset();
handles.push(std::thread::spawn(move || {
(i.to_string(), labeled.finish(Image::default()))
}));
}
for handle in handles {
let (label, loaded_asset) = handle.join().unwrap();
load_context.add_loaded_labeled_asset(label, loaded_asset);
}
Sourcepub fn labeled_asset_scope<A>(
&mut self,
label: String,
load: impl FnOnce(&mut LoadContext<'_>) -> A,
) -> Handle<A>where
A: Asset,
pub fn labeled_asset_scope<A>(
&mut self,
label: String,
load: impl FnOnce(&mut LoadContext<'_>) -> A,
) -> Handle<A>where
A: Asset,
Creates a new LoadContext
for the given label
. The load
function is responsible for loading an Asset
of
type A
. load
will be called immediately and the result will be used to finalize the LoadContext
, resulting in a new
LoadedAsset
, which is registered under the label
label.
This exists to remove the need to manually call LoadContext::begin_labeled_asset
and then manually register the
result with LoadContext::add_labeled_asset
.
See AssetPath
for more on labeled assets.
Sourcepub fn add_labeled_asset<A>(&mut self, label: String, asset: A) -> Handle<A>where
A: Asset,
pub fn add_labeled_asset<A>(&mut self, label: String, asset: A) -> Handle<A>where
A: Asset,
This will add the given asset
as a “labeled Asset
” with the label
label.
§Warning
This will not assign dependencies to the given asset
. If adding an asset
with dependencies generated from calls such as LoadContext::load
, use
LoadContext::labeled_asset_scope
or LoadContext::begin_labeled_asset
to generate a
new LoadContext
to track the dependencies for the labeled asset.
See AssetPath
for more on labeled assets.
Sourcepub fn add_loaded_labeled_asset<A>(
&mut self,
label: impl Into<CowArc<'static, str>>,
loaded_asset: LoadedAsset<A>,
) -> Handle<A>where
A: Asset,
pub fn add_loaded_labeled_asset<A>(
&mut self,
label: impl Into<CowArc<'static, str>>,
loaded_asset: LoadedAsset<A>,
) -> Handle<A>where
A: Asset,
Add a LoadedAsset
that is a “labeled sub asset” of the root path of this load context.
This can be used in combination with LoadContext::begin_labeled_asset
to parallelize
sub asset loading.
See AssetPath
for more on labeled assets.
Sourcepub fn has_labeled_asset<'b>(&self, label: impl Into<CowArc<'b, str>>) -> bool
pub fn has_labeled_asset<'b>(&self, label: impl Into<CowArc<'b, str>>) -> bool
Returns true
if an asset with the label label
exists in this context.
See AssetPath
for more on labeled assets.
Sourcepub fn finish<A>(self, value: A) -> LoadedAsset<A>where
A: Asset,
pub fn finish<A>(self, value: A) -> LoadedAsset<A>where
A: Asset,
“Finishes” this context by populating the final Asset
value.
Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Gets the source path for this load context.
Examples found in repository?
43 async fn load(
44 &self,
45 reader: &mut dyn Reader,
46 _settings: &(),
47 load_context: &mut LoadContext<'_>,
48 ) -> Result<Self::Asset, Self::Error> {
49 let compressed_path = load_context.path();
50 let file_name = compressed_path
51 .file_name()
52 .ok_or(GzAssetLoaderError::IndeterminateFilePath)?
53 .to_string_lossy();
54 let uncompressed_file_name = file_name
55 .strip_suffix(".gz")
56 .ok_or(GzAssetLoaderError::IndeterminateFilePath)?;
57 let contained_path = compressed_path.join(uncompressed_file_name);
58
59 let mut bytes_compressed = Vec::new();
60
61 reader.read_to_end(&mut bytes_compressed).await?;
62
63 let mut decoder = GzDecoder::new(bytes_compressed.as_slice());
64
65 let mut bytes_uncompressed = Vec::new();
66
67 decoder.read_to_end(&mut bytes_uncompressed)?;
68
69 // Now that we have decompressed the asset, let's pass it back to the
70 // context to continue loading
71
72 let mut reader = VecReader::new(bytes_uncompressed);
73
74 let uncompressed = load_context
75 .loader()
76 .with_unknown_type()
77 .immediate()
78 .with_reader(&mut reader)
79 .load(contained_path)
80 .await?;
81
82 Ok(GzAsset { uncompressed })
83 }
Sourcepub fn asset_path(&self) -> &AssetPath<'static>
pub fn asset_path(&self) -> &AssetPath<'static>
Gets the source asset path for this load context.
Sourcepub async fn read_asset_bytes<'b, 'c>(
&'b mut self,
path: impl Into<AssetPath<'c>>,
) -> Result<Vec<u8>, ReadAssetBytesError>
pub async fn read_asset_bytes<'b, 'c>( &'b mut self, path: impl Into<AssetPath<'c>>, ) -> Result<Vec<u8>, ReadAssetBytesError>
Reads the asset at the given path and returns its bytes
Sourcepub fn get_label_handle<'b, A>(
&mut self,
label: impl Into<CowArc<'b, str>>,
) -> Handle<A>where
A: Asset,
pub fn get_label_handle<'b, A>(
&mut self,
label: impl Into<CowArc<'b, str>>,
) -> Handle<A>where
A: Asset,
Returns a handle to an asset of type A
with the label label
. This LoadContext
must produce an asset of the
given type and the given label or the dependencies of this asset will never be considered “fully loaded”. However you
can call this method before or after adding the labeled asset.
Sourcepub fn loader(&mut self) -> NestedLoader<'a, '_, StaticTyped, Deferred>
pub fn loader(&mut self) -> NestedLoader<'a, '_, StaticTyped, Deferred>
Create a builder for loading nested assets in this context.
Examples found in repository?
141 async fn load(
142 &self,
143 reader: &mut dyn Reader,
144 _settings: &Self::Settings,
145 load_context: &mut LoadContext<'_>,
146 ) -> Result<CoolText, Self::Error> {
147 let mut bytes = Vec::new();
148 reader.read_to_end(&mut bytes).await?;
149 let ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
150 let mut base_text = ron.text;
151 for embedded in ron.embedded_dependencies {
152 let loaded = load_context
153 .loader()
154 .immediate()
155 .load::<Text>(&embedded)
156 .await?;
157 base_text.push_str(&loaded.get().0);
158 }
159 for (path, settings_override) in ron.dependencies_with_settings {
160 let loaded = load_context
161 .loader()
162 .with_settings(move |settings| {
163 *settings = settings_override.clone();
164 })
165 .immediate()
166 .load::<Text>(&path)
167 .await?;
168 base_text.push_str(&loaded.get().0);
169 }
170 Ok(CoolText {
171 text: base_text,
172 dependencies: ron
173 .dependencies
174 .iter()
175 .map(|p| load_context.load(p))
176 .collect(),
177 })
178 }
More examples
43 async fn load(
44 &self,
45 reader: &mut dyn Reader,
46 _settings: &(),
47 load_context: &mut LoadContext<'_>,
48 ) -> Result<Self::Asset, Self::Error> {
49 let compressed_path = load_context.path();
50 let file_name = compressed_path
51 .file_name()
52 .ok_or(GzAssetLoaderError::IndeterminateFilePath)?
53 .to_string_lossy();
54 let uncompressed_file_name = file_name
55 .strip_suffix(".gz")
56 .ok_or(GzAssetLoaderError::IndeterminateFilePath)?;
57 let contained_path = compressed_path.join(uncompressed_file_name);
58
59 let mut bytes_compressed = Vec::new();
60
61 reader.read_to_end(&mut bytes_compressed).await?;
62
63 let mut decoder = GzDecoder::new(bytes_compressed.as_slice());
64
65 let mut bytes_uncompressed = Vec::new();
66
67 decoder.read_to_end(&mut bytes_uncompressed)?;
68
69 // Now that we have decompressed the asset, let's pass it back to the
70 // context to continue loading
71
72 let mut reader = VecReader::new(bytes_uncompressed);
73
74 let uncompressed = load_context
75 .loader()
76 .with_unknown_type()
77 .immediate()
78 .with_reader(&mut reader)
79 .load(contained_path)
80 .await?;
81
82 Ok(GzAsset { uncompressed })
83 }
Sourcepub fn load<'b, A>(&mut self, path: impl Into<AssetPath<'b>>) -> Handle<A>where
A: Asset,
pub fn load<'b, A>(&mut self, path: impl Into<AssetPath<'b>>) -> Handle<A>where
A: Asset,
Retrieves a handle for the asset at the given path and adds that path as a dependency of the asset.
If the current context is a normal AssetServer::load
, an actual asset load will be kicked off immediately, which ensures the load happens
as soon as possible.
“Normal loads” kicked from within a normal Bevy App will generally configure the context to kick off loads immediately.
If the current context is configured to not load dependencies automatically (ex: AssetProcessor
),
a load will not be kicked off automatically. It is then the calling context’s responsibility to begin a load if necessary.
If you need to override asset settings, asset type, or load directly, please see LoadContext::loader
.
Examples found in repository?
141 async fn load(
142 &self,
143 reader: &mut dyn Reader,
144 _settings: &Self::Settings,
145 load_context: &mut LoadContext<'_>,
146 ) -> Result<CoolText, Self::Error> {
147 let mut bytes = Vec::new();
148 reader.read_to_end(&mut bytes).await?;
149 let ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
150 let mut base_text = ron.text;
151 for embedded in ron.embedded_dependencies {
152 let loaded = load_context
153 .loader()
154 .immediate()
155 .load::<Text>(&embedded)
156 .await?;
157 base_text.push_str(&loaded.get().0);
158 }
159 for (path, settings_override) in ron.dependencies_with_settings {
160 let loaded = load_context
161 .loader()
162 .with_settings(move |settings| {
163 *settings = settings_override.clone();
164 })
165 .immediate()
166 .load::<Text>(&path)
167 .await?;
168 base_text.push_str(&loaded.get().0);
169 }
170 Ok(CoolText {
171 text: base_text,
172 dependencies: ron
173 .dependencies
174 .iter()
175 .map(|p| load_context.load(p))
176 .collect(),
177 })
178 }
Auto Trait Implementations§
impl<'a> Freeze for LoadContext<'a>
impl<'a> !RefUnwindSafe for LoadContext<'a>
impl<'a> Send for LoadContext<'a>
impl<'a> Sync for LoadContext<'a>
impl<'a> Unpin for LoadContext<'a>
impl<'a> !UnwindSafe for LoadContext<'a>
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.