rafx_assets/assets/
asset_manager_render_resource.rs

1use super::AssetManager;
2use rafx_base::memory::force_to_static_lifetime;
3use std::ops::Deref;
4use std::sync::Arc;
5
6/// static reference is dangerous, must only be used when extracting. This is an option and is unset
7/// while not extracting.
8#[derive(Default)]
9pub struct AssetManagerRenderResource(Option<Arc<&'static AssetManager>>);
10
11impl AssetManagerRenderResource {
12    /// Sets the AssetManager ref
13    pub unsafe fn begin_extract(
14        &mut self,
15        asset_manager: &AssetManager,
16    ) {
17        assert!(self.0.is_none());
18        self.0 = Some(Arc::new(force_to_static_lifetime(asset_manager)));
19    }
20
21    /// Clears the AssetManager ref and panics if any extract ref is remaining
22    pub fn end_extract(&mut self) {
23        self.0
24            .take()
25            .to_owned()
26            .expect("Reference to AssetManager is still in use");
27    }
28
29    /// Returns an extract ref. Panics if called while not extracting. The ref must be dropped
30    /// before end_extract() is called.
31    pub fn extract_ref(&self) -> AssetManagerExtractRef {
32        AssetManagerExtractRef(self.0.as_ref().unwrap().clone())
33    }
34}
35
36/// "Borrowed" from AssetManagerRenderResource, must be dropped before extract ends
37#[derive(Clone)]
38pub struct AssetManagerExtractRef(Arc<&'static AssetManager>);
39
40impl Deref for AssetManagerExtractRef {
41    type Target = AssetManager;
42
43    fn deref(&self) -> &Self::Target {
44        &*self.0
45    }
46}