Skip to main content

AssetResolved

Struct AssetResolved 

Source
pub struct AssetResolved<'a> { /* private fields */ }
Expand description

A wrapper for a resolved asset with direct access to its associated data.

AssetResolved provides additional utilities for checking asset state and accessing its data.

Implementations§

Source§

impl<'a> AssetResolved<'a>

Source

pub fn new(handle: AssetHandle, database: &'a AssetDatabase) -> Self

Creates a new resolved asset.

§Arguments
  • handle: The resolved asset handle.
  • database: Reference to the AssetDatabase.
§Returns

An instance of AssetResolved.

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: Component>(&self) -> bool

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: TypedLookupFetch<'b, true>>( &'b self, ) -> Option<Fetch::Value>

Accesses the asset’s typed data, if available.

§Returns

The asset’s data or None if access fails.

Examples found in repository?
examples/ingame.rs (line 114)
102    fn on_redraw(&mut self, graphics: &mut Graphics<Vertex>, _: &mut AppControl) {
103        // Process assets periotically.
104        if self.timer.elapsed().as_secs_f32() > DELTA_TIME {
105            self.timer = Instant::now();
106            self.process_assets(graphics);
107        }
108
109        // Do not render unless we have shader loaded.
110        let Ok(image_shader) = self.image_shader.resolve(&self.assets) else {
111            return;
112        };
113        let Some(image_shader) = image_shader
114            .access_checked::<&AsyncHandle<Shader>>()
115            .map(|handle| handle.to_ref())
116        else {
117            return;
118        };
119
120        // Begin drawing objects.
121        self.context.begin_frame(graphics);
122        self.context.push_shader(&image_shader);
123        self.context.push_blending(GlowBlending::Alpha);
124
125        // Draw sprite only if texture asset is loaded.
126        if let Ok(texture) = self.ferris_texture.resolve(&self.assets)
127            && let Some(texture) = texture
128                .access_checked::<&AsyncHandle<Texture>>()
129                .map(|handle| handle.to_ref())
130        {
131            Sprite::single(SpriteTexture::new("u_image".into(), texture))
132                .pivot(0.5.into())
133                .draw(&mut self.context, graphics);
134        }
135
136        // Commit drawn objects.
137        self.context.end_frame();
138    }
Source

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

Accesses the asset’s typed data without additional checks.

§Returns

The asset’s data.

Examples found in repository?
examples/10_references.rs (line 47)
42    fn contents(&self, database: &AssetDatabase) -> String {
43        let mut result = self.content.as_str().to_owned();
44        if let Some(next) = self.next.as_ref() {
45            result.push(' ');
46            if let Ok(resolved) = next.resolve(database) {
47                result.push_str(&resolved.access::<&Self>().contents(database));
48            }
49        }
50        result
51    }
More examples
Hide additional examples
examples/17_smart_references.rs (line 19)
8fn main() -> Result<(), Box<dyn Error>> {
9    /* ANCHOR: main */
10    let mut database = AssetDatabase::default()
11        .with_protocol(TextAssetProtocol)
12        .with_fetch(FileAssetFetch::default().with_root("resources"));
13
14    /* ANCHOR: smart_reference */
15    // Load asset and mark its first reference.
16    let first = SmartAssetRef::new("text://lorem.txt", &mut database)?;
17    println!(
18        "Lorem Ipsum: {}",
19        first.resolve(&database)?.access::<&String>()
20    );
21    database.maintain()?;
22    assert_eq!(database.storage.len(), 1);
23    assert_eq!(
24        first
25            .resolve(&database)?
26            .access::<&AssetReferenceCounter>()
27            .counter(),
28        1
29    );
30
31    // Clone the asset and mark its second reference.
32    let second = first.clone();
33    database.maintain()?;
34    assert_eq!(database.storage.len(), 1);
35    assert_eq!(
36        second
37            .resolve(&database)?
38            .access::<&AssetReferenceCounter>()
39            .counter(),
40        2
41    );
42
43    // Ensure asset again from scratch and mark its third reference.
44    let third = SmartAssetRef::new("text://lorem.txt", &mut database)?;
45    database.maintain()?;
46    assert_eq!(database.storage.len(), 1);
47    assert_eq!(
48        third
49            .resolve(&database)?
50            .access::<&AssetReferenceCounter>()
51            .counter(),
52        3
53    );
54
55    // Drop the first reference and expect 2 references.
56    drop(first);
57    database.maintain()?;
58    assert_eq!(database.storage.len(), 1);
59    assert_eq!(
60        third
61            .resolve(&database)?
62            .access::<&AssetReferenceCounter>()
63            .counter(),
64        2
65    );
66
67    // Drop the second reference and expect 1 reference.
68    drop(second);
69    database.maintain()?;
70    assert_eq!(database.storage.len(), 1);
71    assert_eq!(
72        third
73            .resolve(&database)?
74            .access::<&AssetReferenceCounter>()
75            .counter(),
76        1
77    );
78
79    // Drop the third reference and expect no references and asset no more existing.
80    drop(third);
81    database.maintain()?;
82    assert_eq!(database.storage.len(), 0);
83    /* ANCHOR_END: smart_reference */
84    /* ANCHOR_END: main */
85
86    Ok(())
87}
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.

Auto Trait Implementations§

§

impl<'a> Freeze for AssetResolved<'a>

§

impl<'a> !RefUnwindSafe for AssetResolved<'a>

§

impl<'a> Send for AssetResolved<'a>

§

impl<'a> Sync for AssetResolved<'a>

§

impl<'a> Unpin for AssetResolved<'a>

§

impl<'a> !UnwindSafe for AssetResolved<'a>

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