Lazy

Struct Lazy 

Source
pub struct Lazy<T, F = fn() -> T>(/* private fields */);
Available on crate feature regex-automata only.
Expand description

A lazily initialized value that implements Deref for T.

A Lazy takes an initialization function and permits callers from any thread to access the result of that initialization function in a safe manner. In effect, this permits one-time initialization of global resources in a (possibly) multi-threaded program.

This type and its functionality are available even when neither the alloc nor the std features are enabled. In exchange, a Lazy does not guarantee that the given create function is called at most once. It might be called multiple times. Moreover, a call to Lazy::get (either explicitly or implicitly via Lazy’s Deref impl) may block until a T is available.

This is very similar to lazy_static or once_cell, except it doesn’t guarantee that the initialization function will be run once and it works in no-alloc no-std environments. With that said, if you need stronger guarantees or a more flexible API, then it is recommended to use either lazy_static or once_cell.

§Warning: may use a spin lock

When this crate is compiled without the alloc feature, then this type may used a spin lock internally. This can have subtle effects that may be undesirable. See Spinlocks Considered Harmful for a more thorough treatment of this topic.

§Example

This type is useful for creating regexes once, and then using them from multiple threads simultaneously without worrying about synchronization.

use regex_automata::{dfa::regex::Regex, util::lazy::Lazy, Match};

static RE: Lazy<Regex> = Lazy::new(|| Regex::new("foo[0-9]+bar").unwrap());

let expected = Some(Match::must(0, 3..14));
assert_eq!(expected, RE.find(b"zzzfoo12345barzzz"));

Implementations§

Source§

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

Source

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

Create a new Lazy value that is initialized via the given function.

The T type is automatically inferred from the return type of the create function given.

Source§

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

Source

pub fn get(this: &Lazy<T, F>) -> &T

Return a reference to the lazily initialized value.

This routine may block if another thread is initializing a T.

Note that given a x which has type Lazy, this must be called via Lazy::get(x) and not x.get(). This routine is defined this way because Lazy impls Deref with a target of T.

§Panics

This panics if the create function inside this lazy value panics. If the panic occurred in another thread, then this routine may also panic (but is not guaranteed to do so).

Trait Implementations§

Source§

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

Source§

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

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

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

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.

Auto Trait Implementations§

§

impl<T, F = fn() -> T> !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> Sync for Lazy<T, F>
where T: Send + Sync, F: Send + Sync,

§

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

§

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Source for T
where T: Deref, <T as Deref>::Target: Source,

Source§

type Slice<'a> = <<T as Deref>::Target as Source>::Slice<'a> where T: 'a

A type this Source can be sliced into.
Source§

fn len(&self) -> usize

Length of the source
Source§

fn read<'a, Chunk>(&'a self, offset: usize) -> Option<Chunk>
where Chunk: Chunk<'a>,

Read a chunk of bytes into an array. Returns None when reading out of bounds would occur. Read more
Source§

unsafe fn read_byte_unchecked(&self, offset: usize) -> u8

Read a byte without doing bounds checks. Read more
Source§

fn slice(&self, range: Range<usize>) -> Option<<T as Source>::Slice<'_>>

Get a slice of the source at given range. This is analogous to slice::get(range). Read more
Source§

unsafe fn slice_unchecked( &self, range: Range<usize>, ) -> <T as Source>::Slice<'_>

Get a slice of the source at given range. This is analogous to slice::get_unchecked(range). Read more
Source§

fn is_boundary(&self, index: usize) -> bool

Check if index is valid for this Source, that is: Read more
Source§

fn find_boundary(&self, index: usize) -> usize

For &str sources attempts to find the closest char boundary at which source can be sliced, starting from index. Read more
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.