pub struct Memo<T> { /* private fields */ }Expand description
A value derived from one generation of the repository, kept until that generation ends.
The OpenAPI document, the MCP tool and resource listings and the resolved web surfaces
are all expensive functions of the index and the registry, and all of them used to be
OnceLocks — right while the index was immutable for the life of a process, and a
second frozen picture the moment it was not. A memo answers from the generation it was
built for and rebuilds for the next one.
use majordomus_cli::live::Memo;
let memo: Memo<String> = Memo::default();
let a = memo.get_or_init(0, || "built once".to_string());
let b = memo.get_or_init(0, || panic!("the same generation must not rebuild"));
assert_eq!(*a, *b);
let c = memo.get_or_init(1, || "built again".to_string());
assert_eq!(*c, "built again", "a new generation is a new value");Implementations§
Source§impl<T> Memo<T>
impl<T> Memo<T>
Sourcepub fn get_or_init(&self, generation: u64, build: impl FnOnce() -> T) -> Arc<T> ⓘ
pub fn get_or_init(&self, generation: u64, build: impl FnOnce() -> T) -> Arc<T> ⓘ
The value for generation, building it once when this memo does not hold it.
The lock is held across build, so two threads that arrive together on a new
generation do the work once rather than twice.
use majordomus_cli::live::Memo;
let memo: Memo<Vec<u8>> = Memo::default();
let first = memo.get_or_init(3, || vec![1, 2, 3]);
let again = memo.get_or_init(3, || unreachable!("the same generation is not rebuilt"));
assert!(std::sync::Arc::ptr_eq(&first, &again));
assert_eq!(*memo.get_or_init(4, Vec::new), Vec::<u8>::new(), "a new generation rebuilds");Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for Memo<T>
impl<T> RefUnwindSafe for Memo<T>
impl<T> Send for Memo<T>
impl<T> Sync for Memo<T>
impl<T> Unpin for Memo<T>
impl<T> UnsafeUnpin for Memo<T>
impl<T> UnwindSafe for Memo<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more