[−][src]Struct sloth::Lazy
Represents a value of some type T
, lazily evaluated using a parameterless
function or a closure (FnOnce() -> T
) passed to Lazy::new()
.
The evaluated value may be referenced using value_ref()
or value_mut()
methods.
For types implementing Copy
, a copy of the contained value may be obtained
using value()
.
The evaluator function will not be called more than once.
If none of value()
,
value_ref()
and value_mut()
methods are used,
the evaluator function will never be called at all.
Examples
Lazily converting a string to upper case:
use sloth::Lazy; let some_str = "the quick brown fox jumps over the lazy dog"; let lazy_upper_str = Lazy::new(|| some_str.to_uppercase()); assert_eq!( *lazy_upper_str.value_ref(), "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" );
Regardless of how many times the value is accessed, the evaluator function is only called once:
use sloth::Lazy; let mut evaluator_called_times = 0; let lazy_value = Lazy::new(|| { evaluator_called_times += 1; 25 }); assert_eq!(lazy_value.value(), 25); let another_value = lazy_value.value() + lazy_value.value(); assert_eq!(evaluator_called_times, 1);
Methods
impl<T, Eval> Lazy<T, Eval> where
Eval: FnOnce() -> T,
[src]
Eval: FnOnce() -> T,
pub fn new(evaluator: Eval) -> Self
[src]
Constructs a lazy T
instance, whose value, if needed, will later be
obtained from evaluator
and cached.
evaluator
will be invoked only the first time any one of
value()
,
value_ref()
or value_mut()
methods is called.
pub fn value_ref(&self) -> Ref<T>
[src]
Immutably borrows the evaluation result.
This will invoke evaluator function if none of the value
* methods
were called earlier.
pub fn value_mut(&mut self) -> RefMut<T>
[src]
Mutably borrows the evaluation result.
This will invoke evaluator function if none of the value
* methods
were called earlier.
impl<T, Eval> Lazy<T, Eval> where
T: Copy,
Eval: FnOnce() -> T,
[src]
T: Copy,
Eval: FnOnce() -> T,
pub fn value(&self) -> T
[src]
Returns a copy of the evaluation result.
This will invoke evaluator function if none of the value
* methods
were called earlier.
Auto Trait Implementations
impl<T, Eval> Unpin for Lazy<T, Eval> where
Eval: Unpin,
T: Unpin,
Eval: Unpin,
T: Unpin,
impl<T, Eval> !Sync for Lazy<T, Eval>
impl<T, Eval> Send for Lazy<T, Eval> where
Eval: Send,
T: Send,
Eval: Send,
T: Send,
impl<T, Eval> UnwindSafe for Lazy<T, Eval> where
Eval: UnwindSafe,
T: UnwindSafe,
Eval: UnwindSafe,
T: UnwindSafe,
impl<T, Eval> !RefUnwindSafe for Lazy<T, Eval>
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,