Skip to main content

AssetTreeProcessor

Struct AssetTreeProcessor 

Source
pub struct AssetTreeProcessor<T: AssetTree> { /* private fields */ }
Expand description

AssetTreeProcessor is a processor for components that implement the AssetTree trait. It allows deserializing and serializing assets, as well as processing them with dependencies based on what their asset_dependencies method reports.

Implementations§

Source§

impl<T: AssetTree> AssetTreeProcessor<T>

Source

pub fn new( deserializer: impl FnMut(Vec<u8>) -> Result<T, Box<dyn Error>> + Send + Sync + 'static, ) -> Self

Creates a new AssetTreeProcessor with the given deserializer.

The deserializer is a function that takes a Vec<u8> and returns a Result<T, Box<dyn Error>>, where T is the type of the asset tree component.

§Arguments
  • deserializer: A function that deserializes bytes into an asset tree component of type T.
§Returns

A new instance of AssetTreeProcessor.

Examples found in repository?
examples/hello_graph.rs (lines 22-24)
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}
Source

pub fn with_serializer( self, serializer: impl FnMut(&T) -> Result<Vec<u8>, Box<dyn Error>> + Send + Sync + 'static, ) -> Self

Sets a serializer for the asset tree component.

This method allows you to provide a function that serializes an asset tree component of type T into a Vec<u8>. The serializer must be a function that takes a reference to T and returns a Result<Vec<u8>, Box<dyn Error>>.

§Arguments
  • serializer: A function that serializes an asset tree component of type T into a Vec<u8>.
§Returns

A mutable reference to Self, allowing for method chaining.

Trait Implementations§

Source§

impl<T: AssetTree> BundleWithDependenciesProcessor for AssetTreeProcessor<T>

Source§

type Bundle = (T,)

Returned bundle of asset components.
Source§

fn process_bytes( &mut self, bytes: Vec<u8>, ) -> Result<BundleWithDependencies<Self::Bundle>, Box<dyn Error>>

Processes a vector of bytes and returns a BundleWithDependencies.
Source§

fn produce_bytes( &mut self, inspector: AssetInspector<'_>, ) -> Result<StoreWithDependencies, Box<dyn Error>>

Produces bytes using given AssetInspector.
Source§

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

Extracts a DynamicBundle from the given AssetPath. Read more
Source§

fn rewrite_path( &self, path: AssetPath<'static>, ) -> Result<AssetPath<'static>, Box<dyn Error>>

Rewrites the asset path before spawning asset in storage. Read more
Source§

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

Maintains internal state of processor.

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,