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>
impl<T: AssetTree> AssetTreeProcessor<T>
Sourcepub fn new(
deserializer: impl FnMut(Vec<u8>) -> Result<T, Box<dyn Error>> + Send + Sync + 'static,
) -> Self
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 typeT.
§Returns
A new instance of AssetTreeProcessor.
Examples found in repository?
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}Sourcepub fn with_serializer(
self,
serializer: impl FnMut(&T) -> Result<Vec<u8>, Box<dyn Error>> + Send + Sync + 'static,
) -> Self
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 typeTinto aVec<u8>.
§Returns
A mutable reference to Self, allowing for method chaining.
Trait Implementations§
Source§impl<T: AssetTree> BundleWithDependenciesProcessor for AssetTreeProcessor<T>
impl<T: AssetTree> BundleWithDependenciesProcessor for AssetTreeProcessor<T>
Source§fn process_bytes(
&mut self,
bytes: Vec<u8>,
) -> Result<BundleWithDependencies<Self::Bundle>, Box<dyn Error>>
fn process_bytes( &mut self, bytes: Vec<u8>, ) -> Result<BundleWithDependencies<Self::Bundle>, Box<dyn Error>>
BundleWithDependencies.Source§fn produce_bytes(
&mut self,
inspector: AssetInspector<'_>,
) -> Result<StoreWithDependencies, Box<dyn Error>>
fn produce_bytes( &mut self, inspector: AssetInspector<'_>, ) -> Result<StoreWithDependencies, Box<dyn Error>>
AssetInspector.