Enum Memoized

Source
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>

Source

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

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

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

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

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§

Source§

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

Source§

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

Immutably borrows from an owned value. Read more
Source§

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

Source§

type Target = O

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

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

Source§

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

Mutably dereferences the value.

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>
where O: Send, I: Sync, Func: Send,

§

impl<I, O, Func> Sync for Memoized<I, O, Func>
where O: Sync, I: Sync, Func: Sync,

§

impl<I, O, Func> Unpin for Memoized<I, O, Func>
where O: Unpin,

§

impl<I, O, Func> UnwindSafe for Memoized<I, O, Func>
where O: UnwindSafe, I: RefUnwindSafe, Func: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.