Expand description
§auto_invalidate
Automatic cache invalidation on mutable access using a guard pattern.
This crate provides a #[cached] macro that wraps your structs with automatic
cache invalidation. When you access the struct mutably through as_mut(),
all cache fields are automatically invalidated when the guard is dropped.
§Example
use auto_invalidate::cached;
use std::cell::OnceCell;
#[cached]
struct Squarer {
value: i32,
#[invalidate]
cache: OnceCell<i32>,
}
impl Squarer {
fn new(value: i32) -> Self {
Self::build(value)
}
fn square(&self) -> i32 {
*self.cache.get_or_init(|| self.value * self.value)
}
fn set(&mut self, value: i32) {
self.as_mut().value = value;
}
}
let mut s = Squarer::new(4);
assert_eq!(s.square(), 16);
s.set(5); // cache is automatically invalidated
assert_eq!(s.square(), 25);Structs§
Traits§
Attribute Macros§
- cached
- Attribute macro that creates a cached wrapper type with automatic cache invalidation.