Enum memoization::Memoized
source · [−]Expand description
Magical Enum
The generic enum that allows for memoization to happeni.
use memoization::Memoized;
struct Example<O: Clone, F: FnOnce() -> O> {
data: Memoized<O,F>
}
fn eq_str(a: &str, b: &str) -> bool {
a == b
}
let x = 9001;
let lambda = move || -> String {
x.to_string()
};
let mut dut = Example {
data: Memoized::new(lambda)
};
//field is memoized but it can still be written too.
*dut.data = "9001".to_string();
//field can be borrowed as its output data type.
assert!( eq_str( &dut.data, "9001") );
Variants
UnInitialized(Box<Func>)
Processed(Option<O>)
Implementations
sourceimpl<O, Func: FnOnce() -> O> Memoized<O, Func>
impl<O, Func: FnOnce() -> O> Memoized<O, Func>
sourcepub fn new(lambda: Func) -> Memoized<O, Func>
pub fn new(lambda: Func) -> Memoized<O, Func>
Build a new memoized field. The user will pass a lambda function that will initialize the field.
sourcepub fn process(&mut self)
pub fn process(&mut self)
This will convert an UnInitialized value to a Processed value. When called on a Processed value this function will PANIC.
use memoization::Memoized;
let a: (i32,i64,&'static str) = (20,-15,"Hello World!");
let lambda = move || -> String {
format!("Line {:?} DateCode {:?} Log \"{}\"",a.0,a.1,a.2)
};
let mut memoized = Memoized::new(lambda);
//process the data
memoized.process();
//borrowing the processed, as it's processed data type
let x: &str = &memoized;
assert_eq!( x, "Line 20 DateCode -15 Log \"Hello World!\"");
sourcepub fn processed(&self) -> bool
pub fn processed(&self) -> bool
Informs user if a field has been Processed.
use memoization::Memoized;
let a: (i32,i64,&'static str) = (20,-15,"Hello World!");
let lambda = move || -> String {
format!("Line {:?} DateCode {:?} Log \"{}\"",a.0,a.1,a.2)
};
let mut memoized = Memoized::new(lambda);
//data is not initalized/processed
assert!( ! memoized.processed() );
//process the data
memoized.process();
//data is now initalized
assert!( memoized.processed() );
Trait Implementations
Auto Trait Implementations
impl<O, Func> RefUnwindSafe for Memoized<O, Func> where
Func: RefUnwindSafe,
O: RefUnwindSafe,
impl<O, Func> Send for Memoized<O, Func> where
Func: Send,
O: Send,
impl<O, Func> Sync for Memoized<O, Func> where
Func: Sync,
O: Sync,
impl<O, Func> Unpin for Memoized<O, Func> where
O: Unpin,
impl<O, Func> UnwindSafe for Memoized<O, Func> where
Func: UnwindSafe,
O: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more