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>
impl<'a> AssetResolved<'a>
Sourcepub fn new(handle: AssetHandle, database: &'a AssetDatabase) -> Self
pub fn new(handle: AssetHandle, database: &'a AssetDatabase) -> Self
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: TypedLookupFetch<'b, true>>(
&'b self,
) -> Option<Fetch::Value>
pub fn access_checked<'b, Fetch: TypedLookupFetch<'b, true>>( &'b self, ) -> Option<Fetch::Value>
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 }Sourcepub fn access<'b, Fetch: TypedLookupFetch<'b, true>>(&'b self) -> Fetch::Value
pub fn access<'b, Fetch: TypedLookupFetch<'b, true>>(&'b self) -> Fetch::Value
Examples found in repository?
More 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}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.
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> 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
Mutably borrows from an owned value. Read more