pub enum Memoized<I: 'static, O: Clone, Func: Fn(I) -> O> {
UnInitialized(PhantomData<&'static I>, Box<Func>),
Processed(O),
}Expand description
The enumerator that holds data. In its pre-processed state it holds a pointer to the lambda that assigns its future constant value.
use memoization::Memoized;
struct Example<I:'static, O: Clone, F: Fn(I) -> O> {
data: Memoized<I,O,F>
}
fn eq_str(a: &str, b: &str) -> bool {
a == b
}
let lambda = |x: i32 | -> 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(PhantomData<&'static I>, Box<Func>)
Processed(O)
Implementations§
Source§impl<I: 'static, O: Clone, Func: Fn(I) -> O> Memoized<I, O, Func>
impl<I: 'static, O: Clone, Func: Fn(I) -> O> Memoized<I, O, Func>
Sourcepub fn new(lambda: Func) -> Memoized<I, O, Func>
pub fn new(lambda: Func) -> Memoized<I, O, Func>
Build a new memoized field. The user will pass a lambda function that will provide the constructor with typing data.
use memoization::Memoized;
let lambda = | a: (i32,i64,&str) | -> String {
format!("Line {:?} DateCode {:?} Log \"{}\"",a.0,a.1,a.2)
};
let memoized = Memoized::new(lambda);Sourcepub fn process(&mut self, data: I)
pub fn process(&mut self, data: I)
This will convert an UnInitialized value to a Processed value. When called on a processed value it will panic.
use memoization::Memoized;
let tup: (i32,i64,&'static str) = (20,-15,"Hello World!");
let lambda = | a: (i32,i64,&str) | -> String {
format!("Line {:?} DateCode {:?} Log \"{}\"",a.0,a.1,a.2)
};
let mut memoized = Memoized::new(lambda);
//process the data
memoized.process(tup);
//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 fetch(&mut self, data: I) -> O
pub fn fetch(&mut self, data: I) -> O
When passed input data the enum will execute, and return a CLONE of its result. The data will be created at this stage. If the data already exists it will return a clone of the data.
use memoization::Memoized;
let tup: (i32,i64,&'static str) = (20,-15,"Hello World!");
let lambda = | a: (i32,i64,&str) | -> String {
format!("Line {:?} DateCode {:?} Log \"{}\"",a.0,a.1,a.2)
};
let mut memoized = Memoized::new(lambda);
//process + fetch a copy
let output: String = memoized.fetch(tup);
assert_eq!( output, "Line 20 DateCode -15 Log \"Hello World!\"".to_string());Sourcepub fn get(&self) -> O
pub fn get(&self) -> O
returns a clone of a processed field. If the field is not processed the function will panic.
use memoization::Memoized;
let tup: (i32,i64,&'static str) = (20,-15,"Hello World!");
let lambda = | a: (i32,i64,&str) | -> String {
format!("Line {:?} DateCode {:?} Log \"{}\"",a.0,a.1,a.2)
};
let mut memoized = Memoized::new(lambda);
//process the data
memoized.process(tup);
//get copy of the data
let x: String = memoized.get();
assert_eq!( x, "Line 20 DateCode -15 Log \"Hello World!\"".to_string());Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Informs user if filed has been processed/initalized.
use memoization::Memoized;
let tup: (i32,i64,&'static str) = (20,-15,"Hello World!");
let lambda = | a: (i32,i64,&str) | -> String {
format!("Line {:?} DateCode {:?} Log \"{}\"",a.0,a.1,a.2)
};
let mut memoized = Memoized::new(lambda);
//data is not initalized/processed
assert!( ! memoized.is_initialized() );
//process the data
memoized.process(tup);
//data is now initalized
assert!( memoized.is_initialized() );Trait Implementations§
Auto Trait Implementations§
impl<I, O, Func> Freeze for Memoized<I, O, Func>where
O: Freeze,
impl<I, O, Func> RefUnwindSafe for Memoized<I, O, Func>
impl<I, O, Func> Send for Memoized<I, O, Func>
impl<I, O, Func> Sync for Memoized<I, O, Func>
impl<I, O, Func> Unpin for Memoized<I, O, Func>where
O: Unpin,
impl<I, O, Func> UnwindSafe for Memoized<I, O, Func>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more