Skip to main content

ExtractAssetFetch

Struct ExtractAssetFetch 

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

A handler for asset extraction that implements AssetFetch.

This struct is used to define the extraction logic for assets awaiting extraction from storage.

Implementations§

Source§

impl ExtractAssetFetch

Source

pub fn new( extract: impl FnMut(Entity, AssetPath<'_>, &World, &mut CommandBuffer) -> Result<(), Box<dyn Error>> + Send + Sync + 'static, ) -> Self

Creates a new ExtractAssetFetch instance.

§Arguments
  • extract: A function that performs asset extraction.
§Returns

A new ExtractAssetFetch instance.

Examples found in repository?
examples/16_extract_from_asset.rs (lines 30-39)
12fn main() -> Result<(), Box<dyn Error>> {
13    /* ANCHOR: main */
14    let mut database = AssetDatabase::default()
15        .with_protocol(TextAssetProtocol)
16        .with_protocol(BytesAssetProtocol)
17        // We start with regular fetch engine.
18        .with_fetch(FileAssetFetch::default().with_root("resources"));
19
20    // Start loading package ZIP bytes.
21    database.ensure("bytes://package.zip")?;
22
23    // Maintain database while busy.
24    while database.is_busy() {
25        database.maintain()?;
26    }
27
28    // Then we push extraction asset fetch to fetch engine stack. From now on
29    // any future asset request will be extracted from loaded ZIP archive.
30    database.push_fetch(ExtractAssetFetch::new(from_asset_extractor(
31        "bytes://package.zip",
32        |bytes: &Vec<u8>, path| {
33            let mut archive = ZipArchive::new(Cursor::new(bytes))?;
34            let mut file = archive.by_name(path.path())?;
35            let mut result = vec![];
36            file.read_to_end(&mut result)?;
37            Ok(result)
38        },
39    )));
40
41    // Extract some assets from ZIP asset.
42    let lorem = database.ensure("text://lorem.txt")?;
43    let trash = database.ensure("bytes://trash.bin")?;
44
45    // Run maintenance to process extracted asset bytes.
46    database.maintain()?;
47
48    println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
49    println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
50    /* ANCHOR_END: main */
51
52    Ok(())
53}

Trait Implementations§

Source§

impl AssetFetch for ExtractAssetFetch

Source§

fn load_bytes(&self, _: AssetPath<'_>) -> Result<DynamicBundle, Box<dyn Error>>

Loads the raw bytes of an asset given its path. Read more
Source§

fn maintain(&mut self, storage: &mut World) -> Result<(), Box<dyn Error>>

Maintains the fetcher’s state. 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> 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,