pub struct AssetNode<T: AssetTree> { /* private fields */ }Expand description
AssetNode represents a node in the asset graph, which is a reference to an asset that can be resolved to a specific component in asset.
Implementations§
Source§impl<T: AssetTree> AssetNode<T>
impl<T: AssetTree> AssetNode<T>
Sourcepub fn new(path: impl Into<AssetPathStatic>) -> Self
pub fn new(path: impl Into<AssetPathStatic>) -> Self
Creates a new AssetNode from a static asset path.
§Arguments
path: The path to the asset, which can be any type that can be converted intoAssetPathStatic.
§Returns
A new AssetNode instance that references the asset at the given path.
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}Sourcepub fn invalidate(&self) -> Result<(), Box<dyn Error>>
pub fn invalidate(&self) -> Result<(), Box<dyn Error>>
Invalidates the asset node, making internal AssetRef unresolved.
§Returns
A Result indicating success or failure. If successful, the asset node
is marked as invalidated, and any future attempts to resolve it will
require re-fetching or re-resolving the asset.
Sourcepub fn path(&self) -> &AssetPathStatic
pub fn path(&self) -> &AssetPathStatic
Returns the path of the asset node.
§Returns
A reference to the AssetPathStatic associated with this node.
Sourcepub fn handle(&self) -> Result<AssetHandle, Box<dyn Error>>
pub fn handle(&self) -> Result<AssetHandle, Box<dyn Error>>
Returns the handle of the asset node.
§Returns
A Result containing the AssetHandle if successful, or an error if
the handle cannot be retrieved.
Sourcepub fn resolve<'a>(
&'a self,
database: &'a AssetDatabase,
) -> Result<AssetNodeResolved<'a, T>, Box<dyn Error>>
pub fn resolve<'a>( &'a self, database: &'a AssetDatabase, ) -> Result<AssetNodeResolved<'a, T>, Box<dyn Error>>
Resolves the asset node to a specific component in the asset database.
§Arguments
database: A reference to theAssetDatabasewhere the asset is stored.
§Returns
A Result containing an AssetNodeResolved instance if successful, or an
error if the resolution fails. The AssetNodeResolved provides access to
the resolved component, allowing read and write operations.
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 ensure<'a>(
&'a self,
database: &'a mut AssetDatabase,
) -> Result<AssetNodeResolved<'a, T>, Box<dyn Error>>
pub fn ensure<'a>( &'a self, database: &'a mut AssetDatabase, ) -> Result<AssetNodeResolved<'a, T>, Box<dyn Error>>
Ensures that the asset node is resolved and available in the asset database.
§Arguments
database: A mutable reference to theAssetDatabasewhere the asset is stored.
§Returns
A Result containing an AssetNodeResolved instance if successful, or an
error if the resolution fails. The AssetNodeResolved provides access to
the resolved component, allowing read and write operations.
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}Trait Implementations§
Source§impl<T: AssetTree> AssetTree for AssetNode<T>
impl<T: AssetTree> AssetTree for AssetNode<T>
Source§fn asset_dependencies(&self) -> impl IntoIterator<Item = AssetPathStatic>
fn asset_dependencies(&self) -> impl IntoIterator<Item = AssetPathStatic>
Source§impl<'de, T: AssetTree> Deserialize<'de> for AssetNode<T>
impl<'de, T: AssetTree> Deserialize<'de> for AssetNode<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: AssetTree> From<AssetPath<'static>> for AssetNode<T>
impl<T: AssetTree> From<AssetPath<'static>> for AssetNode<T>
Source§fn from(path: AssetPathStatic) -> Self
fn from(path: AssetPathStatic) -> Self
impl<T: Eq + AssetTree> Eq for AssetNode<T>
impl<T: AssetTree> StructuralPartialEq for AssetNode<T>
Auto Trait Implementations§
impl<T> !Freeze for AssetNode<T>
impl<T> RefUnwindSafe for AssetNode<T>
impl<T> Send for AssetNode<T>
impl<T> Sync for AssetNode<T>
impl<T> Unpin for AssetNode<T>
impl<T> UnwindSafe for AssetNode<T>
Blanket Implementations§
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.