Struct FnMap

Source
pub struct FnMap(/* private fields */);
Expand description

Single thread only FnMap implementation.

This implementation is zero cost.

Implementations§

Source§

impl FnMap

Source

pub fn new() -> Self

Examples found in repository?
examples/example.rs (line 4)
3fn main() {
4    let map = FnMap::new();
5
6    fn one() -> i32 {
7        println!("one computed");
8        1
9    }
10    
11    let a = *map.get(|| map.get(one) + 1);
12    dbg!(a);
13    assert_eq!(a, 2);
14
15    let b = *map.get(|| map.get(one) + 1);
16    dbg!(b);
17    assert_eq!(b, 2);
18
19    let c = *map.get(one);
20    dbg!(c);
21    assert_eq!(c, 1);
22}
Source

pub fn get_ptr<T: 'static + Send>( &self, key_fn: impl FnOnce() -> T, ) -> NonNull<T>

Source

pub fn get<T: 'static + Send>(&self, key: impl FnOnce() -> T) -> &T

Get or compute value using key

Examples found in repository?
examples/example.rs (line 11)
3fn main() {
4    let map = FnMap::new();
5
6    fn one() -> i32 {
7        println!("one computed");
8        1
9    }
10    
11    let a = *map.get(|| map.get(one) + 1);
12    dbg!(a);
13    assert_eq!(a, 2);
14
15    let b = *map.get(|| map.get(one) + 1);
16    dbg!(b);
17    assert_eq!(b, 2);
18
19    let c = *map.get(one);
20    dbg!(c);
21    assert_eq!(c, 1);
22}
Source

pub fn get_mut<T: 'static + Send>(&mut self, key: impl FnOnce() -> T) -> &mut T

Get or compute value using key

Source

pub fn reset(&mut self)

Reset stored values

Trait Implementations§

Source§

impl Debug for FnMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FnMap

Source§

fn default() -> FnMap

Returns the “default value” for a type. Read more
Source§

impl Send for FnMap

Auto Trait Implementations§

§

impl !Freeze for FnMap

§

impl !RefUnwindSafe for FnMap

§

impl !Sync for FnMap

§

impl Unpin for FnMap

§

impl !UnwindSafe for FnMap

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