Enum memoization::Memoized [] [src]

pub enum Memoized<I: 'static, O: Clone, Func: Fn(I) -> O> {
    UnInitialized(PhantomData<&'static I>, Box<Func>),
    Processed(O),
}

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)

Methods

impl<I: 'static, O: Clone, Func: Fn(I) -> O> Memoized<I, O, Func>
[src]

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);

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!\"");

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());

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());

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

impl<I: 'static, O: Clone, Func: Fn(I) -> O> Deref for Memoized<I, O, Func>
[src]

type Target = O

The resulting type after dereferencing

fn deref<'b>(&'b self) -> &'b Self::Target

The method called to dereference a value

impl<I: 'static, O: Clone, Func: Fn(I) -> O> DerefMut for Memoized<I, O, Func>
[src]

fn deref_mut<'b>(&'b mut self) -> &'b mut Self::Target

The method called to mutably dereference a value

impl<I: 'static, O: Clone, Func: Fn(I) -> O> Borrow<O> for Memoized<I, O, Func>
[src]

fn borrow<'b>(&'b self) -> &'b O

Immutably borrows from an owned value. Read more