Skip to main content

Lazy

Struct Lazy 

Source
pub(super) struct Lazy<T, F> {
    data: AtomicPtr<T>,
    create: F,
    owned: PhantomData<Box<T>>,
}
Expand description

A non-std lazy initialized value.

This might run the initialization function more than once, but will never block.

I wish I could get these semantics into the non-alloc non-std Lazy type below, but I’m not sure how to do it. If you can do an alloc, then the implementation becomes very simple if you don’t care about redundant work precisely because a pointer can be atomically swapped.

Perhaps making this approach work in the non-alloc non-std case requires asking the caller for a pointer? It would make the API less convenient I think.

Fields§

§data: AtomicPtr<T>§create: F§owned: PhantomData<Box<T>>

Implementations§

Source§

impl<T, F> Lazy<T, F>

Source

pub(super) const fn new(create: F) -> Lazy<T, F>

Create a new alloc but non-std lazy value that is racily initialized. That is, the ‘create’ function may be called more than once.

Source§

impl<T, F: Fn() -> T> Lazy<T, F>

Source

pub(super) fn get(&self) -> &T

Get the underlying lazy value. If it hasn’t been initialized yet, then always attempt to initialize it (even if some other thread is initializing it) and atomically attach it to this lazy value before returning it.

Source

fn poll(&self) -> Option<&T>

If this lazy value has been initialized successfully, then return that value. Otherwise return None immediately. This never attempts to run initialization itself.

Trait Implementations§

Source§

impl<T: Debug, F: Fn() -> T> Debug for Lazy<T, F>

Source§

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

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

impl<T, F> Drop for Lazy<T, F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send + Sync, F: Send + Sync> Sync for Lazy<T, F>

Auto Trait Implementations§

§

impl<T, F> !Freeze for Lazy<T, F>

§

impl<T, F> RefUnwindSafe for Lazy<T, F>

§

impl<T, F> Send for Lazy<T, F>
where F: Send, T: Send,

§

impl<T, F> Unpin for Lazy<T, F>
where F: Unpin,

§

impl<T, F> UnsafeUnpin for Lazy<T, F>
where F: UnsafeUnpin,

§

impl<T, F> UnwindSafe for Lazy<T, F>

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.