Skip to main content

Memo

Struct Memo 

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

Source

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§

Source§

impl<T: Debug> Debug for Memo<T>

Source§

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

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

impl<T> Default for Memo<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for Memo<T>

§

impl<T> RefUnwindSafe for Memo<T>

§

impl<T> Send for Memo<T>
where T: Sync + Send,

§

impl<T> Sync for Memo<T>
where T: Sync + Send,

§

impl<T> Unpin for Memo<T>

§

impl<T> UnsafeUnpin for Memo<T>

§

impl<T> UnwindSafe for Memo<T>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more