FabricOnce

Struct FabricOnce 

Source
pub struct FabricOnce { /* private fields */ }
Expand description

One-time initialization primitive backed by a fabric memory lease.

Ensures a value is computed and stored exactly once, even if multiple callers race to initialize it. The first writer wins; subsequent callers read the existing value.

§Example

use grafos_sync::FabricOnce;
use grafos_std::mem::MemBuilder;

let lease = MemBuilder::new().acquire().unwrap();
let once = FabricOnce::new(lease, 0).unwrap();

let val = once.call_once(|| b"computed".to_vec()).unwrap();
assert_eq!(&val, b"computed");

// Second call returns the stored value without recomputing
let val2 = once.call_once(|| b"ignored".to_vec()).unwrap();
assert_eq!(&val2, b"computed");

Implementations§

Source§

impl FabricOnce

Source

pub fn new(lease: MemLease, base_offset: u64) -> Result<Self>

Create a new FabricOnce at base_offset in the given lease.

Initializes the initialized flag to 0 (not yet initialized).

Source

pub fn call_once<F>(&self, f: F) -> Result<Vec<u8>>
where F: FnOnce() -> Vec<u8>,

Initialize the value by calling f, or return the existing value if already initialized.

If the initialized flag is already set, f is not called and the stored value is returned. Otherwise, f is called, the result is written to leased memory (data first, then length, then flag), and the value is returned.

Source

pub fn lease_id(&self) -> u128

Returns the lease ID of the underlying memory lease for external renewal management (e.g. via [grafos_leasekit::RenewalManager]).

Source

pub fn expires_at_unix_secs(&self) -> u64

Returns the expiry time (unix seconds) of the underlying memory lease for external renewal management.

Source

pub fn is_initialized(&self) -> Result<bool>

Check if the value has been initialized.

Returns true if call_once has completed at least once (the initialized flag is set in leased memory).

Auto Trait Implementations§

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