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:
- If cache hit, returns the cached value’s ref.
- If cache miss, loads the value from the source to cache (default as fallback), then returns the ref.
get_mut
- If cache hit, returns the cached value’s mut ref, dereferencing it will mark the value as dirty.
- If cache miss, loads the value from the source to cache (default as fallback), then returns the mut ref.
- 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>
impl<const N: usize> Config<N>
Sourcepub fn get<T>(&self) -> <T as Cacheable<()>>::Ref<'_>
pub fn get<T>(&self) -> <T as Cacheable<()>>::Ref<'_>
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.
Sourcepub fn get_mut<T>(&self) -> <T as Cacheable<()>>::Mut<'_>
pub fn get_mut<T>(&self) -> <T as Cacheable<()>>::Mut<'_>
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.