rafx_assets/
hydrate_impl.rs

1pub use hydrate_loader::artifact_storage::{DynArtifactLoader, UpdateArtifactResult};
2pub use hydrate_loader::ArtifactManager as AssetResource;
3
4use crate::load_queue_hydrate::RafxGenericLoadEventHandler;
5use crate::resource_loader::RafxLoadEventHandler;
6use crossbeam_channel::Sender;
7use hydrate_base::handle::LoaderInfoProvider;
8use hydrate_base::handle::RefOp;
9use hydrate_base::handle::SerdeContext;
10use hydrate_base::LoadHandle;
11use hydrate_loader::storage::ArtifactLoadOp;
12use std::error::Error;
13use type_uuid::TypeUuid;
14
15//TODO: Seems like we can remove asset_resource and asset_storage so we just
16// have ResourceAssetLoader, rename the file to resource_asset_loader and move up.
17
18pub struct RafxResourceAssetLoader<AssetDataT, AssetT>(
19    pub RafxGenericLoadEventHandler<AssetDataT, AssetT>,
20)
21where
22    AssetDataT: for<'a> serde::Deserialize<'a> + 'static + Send,
23    AssetT: TypeUuid + 'static + Send;
24
25impl<AssetDataT, AssetT> DynArtifactLoader<AssetT> for RafxResourceAssetLoader<AssetDataT, AssetT>
26where
27    AssetDataT: for<'a> serde::Deserialize<'a> + 'static + Send,
28    AssetT: TypeUuid + 'static + Send,
29{
30    fn load_artifact(
31        &mut self,
32        refop_sender: &Sender<RefOp>,
33        loader_info: &dyn LoaderInfoProvider,
34        data: &[u8],
35        load_handle: LoadHandle,
36        load_op: ArtifactLoadOp,
37    ) -> Result<UpdateArtifactResult<AssetT>, Box<dyn Error + Send>> {
38        // To enable automatic serde of Handle, we need to set up a SerdeContext with a RefOp sender
39        let asset = SerdeContext::with(loader_info, refop_sender.clone(), || {
40            bincode::deserialize::<AssetDataT>(data)
41                // Coerce into boxed error
42                .map_err(|x| -> Box<dyn Error + Send + 'static> { Box::new(x) })
43        })?;
44
45        let result = self.0.update_asset(load_handle, load_op, asset);
46        Ok(UpdateArtifactResult::AsyncResult(result.result_rx))
47    }
48
49    fn commit_artifact(
50        &mut self,
51        handle: LoadHandle,
52    ) {
53        self.0.commit_asset_version(handle);
54    }
55
56    fn free_artifact(
57        &mut self,
58        handle: LoadHandle,
59    ) {
60        self.0.free(handle);
61    }
62}