pub struct AssetNodeResolved<'a, T: Component> { /* private fields */ }Expand description
AssetNodeResolved represents a resolved asset node, providing access to the component associated with the asset. It allows both read and write access to the component, ensuring that the asset is properly resolved before accessing its data.
Implementations§
Source§impl<'a, T: Component> AssetNodeResolved<'a, T>
impl<'a, T: Component> AssetNodeResolved<'a, T>
Sourcepub fn read(&self) -> Option<&T>
pub fn read(&self) -> Option<&T>
Gives read access to component of the asset.
§Returns
An Option containing a reference to the component if it is accessible,
or None if the component is not accessible.
Sourcepub fn write(&self) -> Option<&mut T>
pub fn write(&self) -> Option<&mut T>
Gives write access to component of the asset.
§Returns
An Option containing a mutable reference to the component if it is
accessible, or None if the component is not accessible.
Sourcepub fn read_unchecked(&self) -> &T
pub fn read_unchecked(&self) -> &T
Gives unchecked read access to component of the asset.
§Returns
A reference to the component, allowing read access without checking if the component is accessible. This method can panic if the component is not accessible, so it should be used with caution.
Examples found in repository?
14fn main() -> Result<(), Box<dyn Error>> {
15 // /* ANCHOR: main */
16 let mut database = AssetDatabase::default()
17 .with_protocol(BundleAssetProtocol::new(
18 "custom",
19 // AssetTreeProcessor handles deserialization of the custom asset
20 // as single component. That component will report its dependencies
21 // to load.
22 AssetTreeProcessor::<CustomAsset>::new(|bytes| {
23 Ok(serde_json::from_slice::<CustomAsset>(&bytes)?)
24 }),
25 ))
26 .with_fetch(FileAssetFetch::default().with_root("resources"));
27
28 // Create asset node and ensure it is being loaded.
29 let asset = AssetNode::<CustomAsset>::new("custom://part1.json");
30 asset.ensure(&mut database)?;
31
32 // Wait until the database is not busy, which means all assets are loaded.
33 while database.is_busy() {
34 database.maintain()?;
35 }
36
37 // Resolve the asset and read its contents.
38 // This will also resolve all dependencies of the asset.
39 let contents = asset
40 .resolve(&database)?
41 .read_unchecked()
42 .contents(&database);
43 println!("Custom chain contents: {contents:?}");
44 // /* ANCHOR_END: main */
45 Ok(())
46}
47
48/* ANCHOR: custom_asset */
49// This example demonstrates how to create a custom asset type that can
50// have dependencies on other assets. The `CustomAsset` struct is defined
51// with a `content` field and an optional `next` field that points to another
52// `CustomAsset`.
53#[derive(Debug, Default, Serialize, Deserialize, AssetTree)]
54struct CustomAsset {
55 content: String,
56 // The `next` field is annotated with `#[asset_deps]` to indicate that it is
57 // an asset dependency that has to be loaded along with this asset.
58 #[serde(default)]
59 #[asset_deps]
60 next: Option<AssetNode<CustomAsset>>,
61}
62
63impl CustomAsset {
64 fn contents(&self, database: &AssetDatabase) -> String {
65 let mut result = self.content.as_str().to_owned();
66 if let Some(next) = self.next.as_ref() {
67 result.push(' ');
68 if let Ok(resolved) = next.resolve(database) {
69 result.push_str(&resolved.read_unchecked().contents(database));
70 }
71 }
72 result
73 }Sourcepub fn write_unchecked(&self) -> &mut T
pub fn write_unchecked(&self) -> &mut T
Gives unchecked write access to component of the asset.
§Returns
A mutable reference to the component, allowing write access without checking if the component is accessible. This method can panic if the component is not accessible, so it should be used with caution.
Methods from Deref<Target = AssetResolved<'a>>§
Sourcepub fn does_exists(&self) -> bool
pub fn does_exists(&self) -> bool
Checks if the asset exists in the database.
Sourcepub fn is_ready_to_use(&self) -> bool
pub fn is_ready_to_use(&self) -> bool
Checks if the asset is ready to use.
Sourcepub async fn wait_for_ready_to_use(&self)
pub async fn wait_for_ready_to_use(&self)
Waits asynchronously for the asset to be ready to use.
Sourcepub fn access_checked<'b, Fetch>(
&'b self,
) -> Option<<Fetch as TypedLookupFetch<'b, true>>::Value>where
Fetch: TypedLookupFetch<'b, true>,
pub fn access_checked<'b, Fetch>(
&'b self,
) -> Option<<Fetch as TypedLookupFetch<'b, true>>::Value>where
Fetch: TypedLookupFetch<'b, true>,
Sourcepub fn access<'b, Fetch>(
&'b self,
) -> <Fetch as TypedLookupFetch<'b, true>>::Valuewhere
Fetch: TypedLookupFetch<'b, true>,
pub fn access<'b, Fetch>(
&'b self,
) -> <Fetch as TypedLookupFetch<'b, true>>::Valuewhere
Fetch: TypedLookupFetch<'b, true>,
Sourcepub fn dependencies(&self) -> impl Iterator<Item = AssetRef>
pub fn dependencies(&self) -> impl Iterator<Item = AssetRef>
Iterates over the asset’s dependencies as AssetRef objects.
Sourcepub fn dependent(&self) -> impl Iterator<Item = AssetRef>
pub fn dependent(&self) -> impl Iterator<Item = AssetRef>
Iterates over the assets dependent on this one as AssetRef objects.
Sourcepub fn traverse_dependencies(&self) -> impl Iterator<Item = AssetRef>
pub fn traverse_dependencies(&self) -> impl Iterator<Item = AssetRef>
Iterates recursively over all dependencies as AssetRef objects.