Struct assets_manager::OnceInitCell

source ·
pub struct OnceInitCell<U, T> { /* private fields */ }
Available on crate feature 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>

source

pub const fn new(value: U) -> Self

Creates a new uninitialized cell.

source

pub const fn with_value(value: T) -> Self

Creates a new initialized cell.

source

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.

source

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.

source

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: Send + Sync + 'static> Compound for OnceInitCell<Option<U>, T>

source§

fn load(cache: AnyCache<'_>, id: &SharedString) -> Result<Self, BoxedError>

Loads an asset from the cache. Read more
source§

const HOT_RELOADED: bool = U::HOT_RELOADED

If false, disable hot-reloading for assets of this type (true by default). If so, you may want to implement NotHotReloaded for this type to enable additional functions.
source§

impl<U: Compound, T: Send + Sync + 'static> Compound for OnceInitCell<U, T>

source§

fn load(cache: AnyCache<'_>, id: &SharedString) -> Result<Self, BoxedError>

Loads an asset from the cache. Read more
source§

const HOT_RELOADED: bool = U::HOT_RELOADED

If false, disable hot-reloading for assets of this type (true by default). If so, you may want to implement NotHotReloaded for this type to enable additional functions.
source§

impl<U, T: Debug> Debug for OnceInitCell<U, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<U, T> Default for OnceInitCell<U, T>
where U: Default,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<U, T> Drop for OnceInitCell<U, T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<U, T> RefUnwindSafe for OnceInitCell<U, T>

source§

impl<U, T> Sync for OnceInitCell<U, T>
where T: Send + Sync, U: Send,

source§

impl<U, T> UnwindSafe for OnceInitCell<U, T>
where T: UnwindSafe, U: UnwindSafe,

Auto Trait Implementations§

§

impl<U, T> !Freeze for OnceInitCell<U, T>

§

impl<U, T> Send for OnceInitCell<U, T>
where U: Send, T: Send,

§

impl<U, T> Unpin for OnceInitCell<U, T>
where U: Unpin, T: Unpin,

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<S> FromSample<S> for S

source§

fn from_sample_(s: S) -> S

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> Storable for T
where T: Compound,

source§

fn get_type(_: Private) -> Type

source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

source§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,