pub struct CurrentState<T> { /* private fields */ }Expand description
A retained current-state subscription that returns owned snapshots.
The subscription initially contains the latest state published before it
was created. Self::snapshot and Self::changed clone that state while
holding the internal Tokio watch borrow only for the duration of the call,
so the returned value can safely live across awaits and database writes.
Cloning a subscription creates an independent change cursor over the same retained state.
let mut state = db.self_wot_subscribe();
let snapshot = state.snapshot();
// The snapshot is owned and may remain live across database operations.
let _heads = db.get_heads_self().await;
assert_eq!(
snapshot.len(),
snapshot.followees.len() + snapshot.extended.len()
);
let _newer_snapshot = state.changed().await;The raw Tokio receiver and its borrow guard are intentionally not exposed:
let state = db.self_wot_subscribe();
let _borrow = state.borrow();Implementations§
Source§impl<T> CurrentState<T>where
T: Clone,
impl<T> CurrentState<T>where
T: Clone,
Sourcepub fn snapshot(&self) -> T
pub fn snapshot(&self) -> T
Return an owned snapshot of the latest retained state.
This does not advance the subscription’s change cursor. A subsequent
Self::changed call immediately returns if it has not yet observed
the publication that supplied this snapshot.
Sourcepub async fn changed(&mut self) -> Result<T, CurrentStateClosed>
pub async fn changed(&mut self) -> Result<T, CurrentStateClosed>
Wait for a publication newer than the cursor and return the latest owned snapshot.
If multiple publications arrive before this call observes them, it coalesces them and returns the latest value. The returned value may equal a previous snapshot because snapshots do not advance the cursor and publishers may publish equal values.
After the publisher closes, this method delivers any last unseen
publication before returning CurrentStateClosed. Self::snapshot
remains available after closure.
This method is cancellation safe: using it in tokio::select! does
not consume an update when another branch wins.
Trait Implementations§
Source§impl<T: Clone> Clone for CurrentState<T>
impl<T: Clone> Clone for CurrentState<T>
Auto Trait Implementations§
impl<T> Forget for CurrentState<T>
impl<T> Freeze for CurrentState<T>
impl<T> Move for CurrentState<T>
impl<T> RefUnwindSafe for CurrentState<T>where
Receiver<T>: RefUnwindSafe,
impl<T> Send for CurrentState<T>
impl<T> Sync for CurrentState<T>
impl<T> Unpin for CurrentState<T>
impl<T> UnsafeUnpin for CurrentState<T>where
Receiver<T>: UnsafeUnpin,
impl<T> UnwindSafe for CurrentState<T>where
Receiver<T>: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<F, T> ExpectFrom<F> for T
impl<F, T> ExpectFrom<F> for T
fn expect_from(other: F) -> T
Source§impl<F, I> ExpectInto<I> for Fwhere
I: ExpectFrom<F>,
impl<F, I> ExpectInto<I> for Fwhere
I: ExpectFrom<F>,
fn expect_into(self) -> I
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more