Config

Struct Config 

Source
pub struct Config<const N: usize> { /* private fields */ }
Expand description

A struct that can be used to cache configuration values. This behaves like a native cache in CPU:

get:

  1. If cache hit, returns the cached value’s ref.
  2. If cache miss, loads the value from the source to cache (default as fallback), then returns the ref.

get_mut

  1. If cache hit, returns the cached value’s mut ref, dereferencing it will mark the value as dirty.
  2. If cache miss, loads the value from the source to cache (default as fallback), then returns the mut ref.
  3. All cached values marked dirty will be written back when Config dropped or cache line evicted.

At most N different config types are safe to be managed at the same time due to the cache capacity. And each type can be ref up to (usize::MAX >> 2) times or mut ref up to 1 time at the same time. Or invalid borrow may happen (since the counter wraps around on overflow). To avoid entering the password during testing, you can enable mock feature. This can always return the same Encrypter during each test.

Implementations§

Source§

impl<const N: usize> Config<N>

Source

pub fn new() -> Self

Create a new Config cache.

Source

pub fn get<T>(&self) -> <T as Cacheable<()>>::Ref<'_>
where T: Cacheable<()> + Any + Send + Sync,

Get an immutable ref (CfgRef) from the config. If the value was not valid, it would try loading from source, and fell back to the default value.

Caution: You can only get up to (usize::MAX >> 2) immutable refs (CfgRef) of each type at the same time.

If the value was marked as writing, it would panic like RefCell. See CfgRef for more details.

Source

pub fn get_mut<T>(&self) -> <T as Cacheable<()>>::Mut<'_>
where T: Cacheable<()> + Any + Send + Sync,

Get a mutable ref (CfgMut) from the config. If the value was not valid, it would try loading from source, and fell back to the default value.

Caution: You can only get up to 1 mutable ref (CfgMut) of each type at the same time.

If the value was marked as reading or writing, it would panic like RefCell. See CfgMut for more details.

Source

pub fn get_many<T>(&self) -> <T as Cacheable<((),)>>::Ref<'_>
where T: Cacheable<((),)> + Any + Send + Sync,

Get many immutable refs from the config.

T: (T1, T2, T3,)

If the value was not valid, it would try loading from source, and fell back to the default value. See CfgRef for more details.

Source

pub fn get_mut_many<T>(&self) -> <T as Cacheable<((),)>>::Mut<'_>
where T: Cacheable<((),)> + Any + Send + Sync,

Get many mutable refs from the config.

T: (T1, T2, T3,)

If the value was not valid, it would try loading from source, and fell back to the default value. See CfgMut for more details.

Trait Implementations§

Source§

impl<const N: usize> Default for Config<N>

Source§

fn default() -> Self

Create an empty Config cache.

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Config<N>

§

impl<const N: usize> !RefUnwindSafe for Config<N>

§

impl<const N: usize> Send for Config<N>

§

impl<const N: usize> Sync for Config<N>

§

impl<const N: usize> Unpin for Config<N>

§

impl<const N: usize> !UnwindSafe for Config<N>

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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V