pub enum Memoized<O, Func: FnOnce() -> O> {
    UnInitialized(Box<Func>),
    Processed(Option<O>),
}
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

Build a new memoized field. The user will pass a lambda function that will initialize the field.

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

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

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Immutably borrows from an owned value. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.