Struct assets_manager::OnceInitCell
source · pub struct OnceInitCell<U, T> { /* private fields */ }utils only.Expand description
A thread-safe cell which can be written to only once.
This is just like a OnceCell, but it also has data when uninitialized.
This is useful if an asset needs a context to be fully initialized. The
“raw” version of the asset can be stored as the Uninitialized part of
the cell, and further loading can be done later when additional context
is available.
The type also provides easy integration with hot-reloading: when the “uninitialized” value is reloaded, so is the cell, and the initialization is re-run.
§Example
use assets_manager::{asset::Png, AnyCache, BoxedError, OnceInitCell};
struct GpuCtx(/* ... */);
struct Texture(/* ... */);
impl GpuCtx {
/// Loads a texture to GPU from an image
fn load_texture(&self, img: &image::DynamicImage) -> Texture {
/* ... */
}
/// Does something with a GPU texture
fn use_texture(&self, texture: &Texture) {
/* ... */
}
/// Loads a texture from an image the cache and uses it.
fn load_and_use_texture(&self, cache: AnyCache, id: &str) -> Result<(), BoxedError> {
// Load the cached texture or the source PNG image.
let img = cache.load::<OnceInitCell<Png, Texture>>(id)?.read();
// If the image has not been uploaded to GPU yet or if it has been
// reloaded, upload it.
let texture = img.get_or_init(|img| self.load_texture(&img.0));
self.use_texture(texture);
Ok(())
}
}Implementations§
source§impl<U, T> OnceInitCell<U, T>
impl<U, T> OnceInitCell<U, T>
sourcepub const fn with_value(value: T) -> Self
pub const fn with_value(value: T) -> Self
Creates a new initialized cell.
sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets the reference to the underlying value.
Returns None if the cell is empty, or being initialized. This
method never blocks.
sourcepub fn get_or_init(&self, f: impl FnOnce(&mut U) -> T) -> &T
pub fn get_or_init(&self, f: impl FnOnce(&mut U) -> T) -> &T
Gets the contents of the cell, initializing it with f if the cell
was uninitialized.
See get_or_try_init for more details.
sourcepub fn get_or_try_init<E>(
&self,
f: impl FnOnce(&mut U) -> Result<T, E>,
) -> Result<&T, E>
pub fn get_or_try_init<E>( &self, f: impl FnOnce(&mut U) -> Result<T, E>, ) -> Result<&T, E>
Gets the contents of the cell, initializing it with f if the cell
was uninitialized. If the cell was uninitialized and f failed, an
error is returned.
§Panics
If f panics, the panic is propagated to the caller, and the cell
remains uninitialized.
It is an error to reentrantly initialize the cell from f. The
exact outcome is unspecified.
Trait Implementations§
source§impl<U: Compound, T: Storable> Compound for OnceInitCell<Option<U>, T>
impl<U: Compound, T: Storable> Compound for OnceInitCell<Option<U>, T>
source§fn load(cache: AnyCache<'_>, id: &SharedString) -> Result<Self, BoxedError>
fn load(cache: AnyCache<'_>, id: &SharedString) -> Result<Self, BoxedError>
source§const HOT_RELOADED: bool = U::HOT_RELOADED
const HOT_RELOADED: bool = U::HOT_RELOADED
false, disable hot-reloading for assets of this type (true by
default). This avoids having to lock the asset to read it (ie it makes
Handle::read a noop)source§impl<U: Compound, T: Storable> Compound for OnceInitCell<U, T>
impl<U: Compound, T: Storable> Compound for OnceInitCell<U, T>
source§fn load(cache: AnyCache<'_>, id: &SharedString) -> Result<Self, BoxedError>
fn load(cache: AnyCache<'_>, id: &SharedString) -> Result<Self, BoxedError>
source§const HOT_RELOADED: bool = U::HOT_RELOADED
const HOT_RELOADED: bool = U::HOT_RELOADED
false, disable hot-reloading for assets of this type (true by
default). This avoids having to lock the asset to read it (ie it makes
Handle::read a noop)source§impl<U, T: Debug> Debug for OnceInitCell<U, T>
impl<U, T: Debug> Debug for OnceInitCell<U, T>
source§impl<U, T> Default for OnceInitCell<U, T>where
U: Default,
impl<U, T> Default for OnceInitCell<U, T>where
U: Default,
source§impl<U: DirLoadable, T: Storable> DirLoadable for OnceInitCell<Option<U>, T>
impl<U: DirLoadable, T: Storable> DirLoadable for OnceInitCell<Option<U>, T>
source§fn select_ids(
cache: AnyCache<'_>,
id: &SharedString,
) -> Result<Vec<SharedString>>
fn select_ids( cache: AnyCache<'_>, id: &SharedString, ) -> Result<Vec<SharedString>>
id. Read moresource§fn sub_directories(
cache: AnyCache<'_>,
id: &SharedString,
f: impl FnMut(&str),
) -> Result<()>
fn sub_directories( cache: AnyCache<'_>, id: &SharedString, f: impl FnMut(&str), ) -> Result<()>
source§impl<U: DirLoadable, T: Storable> DirLoadable for OnceInitCell<U, T>
impl<U: DirLoadable, T: Storable> DirLoadable for OnceInitCell<U, T>
source§fn select_ids(
cache: AnyCache<'_>,
id: &SharedString,
) -> Result<Vec<SharedString>>
fn select_ids( cache: AnyCache<'_>, id: &SharedString, ) -> Result<Vec<SharedString>>
id. Read more