Skip to main content

Loader

Struct Loader 

Source
pub struct Loader { /* private fields */ }
Expand description

Virtual storage for all available assets.

Implementations§

Source§

impl Loader

Source

pub fn builder() -> LoaderBuilder

Returns LoaderBuilder instance

Examples found in repository?
examples/proc.rs (line 157)
123async fn run() -> Result<(), Box<dyn std::error::Error>> {
124    // Fill map.
125    let source = HashMapSource(
126        vec![
127            (
128                AssetId::new(2).unwrap(),
129                b"null".to_vec().into_boxed_slice(),
130            ),
131            (
132                AssetId::new(3).unwrap(),
133                b"{\"field\": {}}".to_vec().into_boxed_slice(),
134            ),
135            (
136                AssetId::new(4).unwrap(),
137                b"{\"a\": 3}".to_vec().into_boxed_slice(),
138            ),
139            (
140                AssetId::new(5).unwrap(),
141                b"{\"c\": {\"a\": 2}, \"a\": 3}".to_vec().into_boxed_slice(),
142            ),
143            (
144                AssetId::new(6).unwrap(),
145                b"{\"wrapped\": 42}".to_vec().into_boxed_slice(),
146            ),
147            (
148                AssetId::new(7).unwrap(),
149                b"{\"a\": 6}".to_vec().into_boxed_slice(),
150            ),
151            (AssetId::new(8).unwrap(), b"{}".to_vec().into_boxed_slice()),
152        ]
153        .into_iter()
154        .collect(),
155    );
156
157    let loader = Loader::builder().with(source).build();
158
159    let _: &UnitAsset = loader.load("2").await.build(&mut ())?;
160    println!("UnitAsset loaded");
161
162    let _: &SimpleAsset = loader.load("3").await.build(&mut ())?;
163    println!("SimpleAsset loaded");
164
165    let _: &TwoLevelAsset = loader.load("4").await.build(&mut ())?;
166    println!("TwoLevelAsset loaded");
167
168    let _: &ComplexAsset = loader.load("5").await.build(&mut ())?;
169    println!("ComplexAsset loaded");
170
171    let _: &WrapperAsset = loader.load("6").await.build(&mut ())?;
172    println!("WrapperAsset loaded");
173
174    let _: &AssetWithWrapper = loader.load("7").await.build(&mut ())?;
175    println!("AssetWithWrapper loaded");
176
177    let _: &AssetWithSerdeAttribute = loader.load("8").await.build(&mut ())?;
178    println!("AssetWithSerdeAttribute loaded");
179
180    let _: &AssetWithOption = loader.load("8").await.build(&mut ())?;
181    println!("AssetWithOption loaded");
182
183    Ok(())
184}
Source

pub fn load_typed<A>(&self, id: TypedAssetId<A>) -> AssetHandle<A>
where A: Asset,

Load asset with specified id and returns handle that can be used to access assets once it is loaded.

If asset was previously requested it will not be re-loaded, but handle to shared state will be returned instead, even if first load was not successful or different format was used.

Source

pub fn load<'a, A, K>(&self, key: K) -> AssetHandle<A>
where A: Asset, K: Into<Key<'a>>,

Load asset with specified key (path or id) and returns handle that can be used to access assets once it is loaded.

If asset was previously requested it will not be re-loaded, but handle to shared state will be returned instead, even if first load was not successful or different format was used.

Examples found in repository?
examples/proc.rs (line 159)
123async fn run() -> Result<(), Box<dyn std::error::Error>> {
124    // Fill map.
125    let source = HashMapSource(
126        vec![
127            (
128                AssetId::new(2).unwrap(),
129                b"null".to_vec().into_boxed_slice(),
130            ),
131            (
132                AssetId::new(3).unwrap(),
133                b"{\"field\": {}}".to_vec().into_boxed_slice(),
134            ),
135            (
136                AssetId::new(4).unwrap(),
137                b"{\"a\": 3}".to_vec().into_boxed_slice(),
138            ),
139            (
140                AssetId::new(5).unwrap(),
141                b"{\"c\": {\"a\": 2}, \"a\": 3}".to_vec().into_boxed_slice(),
142            ),
143            (
144                AssetId::new(6).unwrap(),
145                b"{\"wrapped\": 42}".to_vec().into_boxed_slice(),
146            ),
147            (
148                AssetId::new(7).unwrap(),
149                b"{\"a\": 6}".to_vec().into_boxed_slice(),
150            ),
151            (AssetId::new(8).unwrap(), b"{}".to_vec().into_boxed_slice()),
152        ]
153        .into_iter()
154        .collect(),
155    );
156
157    let loader = Loader::builder().with(source).build();
158
159    let _: &UnitAsset = loader.load("2").await.build(&mut ())?;
160    println!("UnitAsset loaded");
161
162    let _: &SimpleAsset = loader.load("3").await.build(&mut ())?;
163    println!("SimpleAsset loaded");
164
165    let _: &TwoLevelAsset = loader.load("4").await.build(&mut ())?;
166    println!("TwoLevelAsset loaded");
167
168    let _: &ComplexAsset = loader.load("5").await.build(&mut ())?;
169    println!("ComplexAsset loaded");
170
171    let _: &WrapperAsset = loader.load("6").await.build(&mut ())?;
172    println!("WrapperAsset loaded");
173
174    let _: &AssetWithWrapper = loader.load("7").await.build(&mut ())?;
175    println!("AssetWithWrapper loaded");
176
177    let _: &AssetWithSerdeAttribute = loader.load("8").await.build(&mut ())?;
178    println!("AssetWithSerdeAttribute loaded");
179
180    let _: &AssetWithOption = loader.load("8").await.build(&mut ())?;
181    println!("AssetWithOption loaded");
182
183    Ok(())
184}

Trait Implementations§

Source§

impl Clone for Loader

Source§

fn clone(&self) -> Loader

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.