Skip to main content

AssetNodeResolved

Struct AssetNodeResolved 

Source
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>

Source

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.

Source

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.

Source

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?
examples/hello_graph.rs (line 41)
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    }
Source

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>>§

Source

pub fn entity(&self) -> Entity

Gets the entity associated with this asset.

Source

pub fn does_exists(&self) -> bool

Checks if the asset exists in the database.

Source

pub fn has<T>(&self) -> bool
where T: Component,

Checks if the asset has given component.

Source

pub fn is_ready_to_use(&self) -> bool

Checks if the asset is ready to use.

Source

pub async fn wait_for_ready_to_use(&self)

Waits asynchronously for the asset to be ready to use.

Source

pub fn access_checked<'b, Fetch>( &'b self, ) -> Option<<Fetch as TypedLookupFetch<'b, true>>::Value>
where Fetch: TypedLookupFetch<'b, true>,

Accesses the asset’s typed data, if available.

§Returns

The asset’s data or None if access fails.

Source

pub fn access<'b, Fetch>( &'b self, ) -> <Fetch as TypedLookupFetch<'b, true>>::Value
where Fetch: TypedLookupFetch<'b, true>,

Accesses the asset’s typed data without additional checks.

§Returns

The asset’s data.

Source

pub fn dependencies(&self) -> impl Iterator<Item = AssetRef>

Iterates over the asset’s dependencies as AssetRef objects.

Source

pub fn dependent(&self) -> impl Iterator<Item = AssetRef>

Iterates over the assets dependent on this one as AssetRef objects.

Source

pub fn traverse_dependencies(&self) -> impl Iterator<Item = AssetRef>

Iterates recursively over all dependencies as AssetRef objects.

Trait Implementations§

Source§

impl<'a, T: Component> Deref for AssetNodeResolved<'a, T>

Source§

type Target = AssetResolved<'a>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T: Component> DerefMut for AssetNodeResolved<'a, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<'a, T> Freeze for AssetNodeResolved<'a, T>

§

impl<'a, T> !RefUnwindSafe for AssetNodeResolved<'a, T>

§

impl<'a, T> Send for AssetNodeResolved<'a, T>

§

impl<'a, T> Sync for AssetNodeResolved<'a, T>

§

impl<'a, T> Unpin for AssetNodeResolved<'a, T>

§

impl<'a, T> !UnwindSafe for AssetNodeResolved<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Finalize for T

Source§

unsafe fn finalize_raw(data: *mut ())

Safety Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Component for T
where T: Send + Sync + 'static,