Skip to main content

Subscription

Struct Subscription 

Source
pub struct Subscription<T: Send + Sync + 'static> { /* private fields */ }
Expand description

Latest-value state cell with a bounded every-change feed.

Subscription uses a two-plane implementation. The Ractor actor owns only subscribe, unsubscribe, close, terminal delivery, and the registry that is periodically published as an ArcSwap slot-table snapshot. State transitions run on the caller thread.

The lossless data plane is a sequence-claimed ring: each writer claims a global sequence, waits for its publish turn, writes the ring slot, stores the ArcSwap mirror, publishes an internal (sequence, value) snapshot, wakes current subscribers, and then returns. Under SubscriptionOverflow::Backpressure, producers wait outside the actor while any active subscriber cursor would lag past the logical capacity. Subscribers consume by cursor in total sequence order, so they see no gaps or duplicates.

Subscribe has no actor-serialized-set gap. The control actor first publishes the new slot-table snapshot, then seeds the slot from the current published (sequence, value) and sets the cursor to sequence + 1; any writer that races after registration is either represented by the seed or consumed from the ring.

update uses ArcSwap::compare_and_swap under the write publication turn. The update function may be re-invoked if a concurrent writer wins the CAS race, matching the usual Ref/atomic update contract.

Implementations§

Source§

impl<T: Send + Sync + 'static> Subscription<T>

Source

pub fn new( initial: T, capacity: usize, overflow: SubscriptionOverflow, ) -> StreamResult<Self>

Create a subscription initialized to initial.

Panics if capacity == 0.

Source

pub fn get(&self) -> Arc<T>

Return the current immutable snapshot without sending an actor message.

Source

pub fn get_cloned(&self) -> T
where T: Clone,

Return a cloned value using ArcSwap::load()’s guarded read path.

This avoids cloning the Arc itself on the hot read path. For scalar Copy/cheap-Clone values, this is the fair equivalent of JVM refs returning the value directly; use Subscription::get when the caller wants an owned snapshot shared by Arc.

Source

pub fn set(&self, value: T) -> StreamResult<()>

Set the state and wait for the transition to be accepted according to the overflow policy.

Source

pub fn set_eventually(&self, value: T) -> StreamResult<()>

Set the state on the caller thread.

With SubscriptionOverflow::Backpressure, this may still park the caller until subscriber cursors make ring capacity available. It does not send an actor message.

Source

pub fn update<F>(&self, update: F) -> StreamResult<()>
where F: FnMut(&T) -> T + Send + 'static,

Update the state atomically and wait for the transition to be accepted.

The update function may be called more than once if a concurrent writer wins the CAS race.

Source

pub fn update_eventually<F>(&self, update: F) -> StreamResult<()>
where F: FnMut(&T) -> T + Send + 'static,

Update the state atomically on the caller thread.

The update function may be called more than once if a concurrent writer wins the CAS race.

Source

pub fn close(&self) -> StreamResult<()>

Close the subscription, re-emitting the current final snapshot to current subscribers.

Source

pub fn close_with(&self, final_value: T) -> StreamResult<()>

Set a final value, then close the subscription in one control-plane turn.

Source§

impl<T: Clone + Send + Sync + 'static> Subscription<T>

Source

pub fn changes(&self) -> Source<T>

A bounded source of the current value followed by every accepted change.

Under SubscriptionOverflow::Backpressure, every subscriber observes every change. Under DropNew, slow subscribers may miss changes. Under Fail, a full subscriber fails with StreamError::Failed.

Trait Implementations§

Source§

impl<T: Send + Sync + 'static> Clone for Subscription<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Subscription<T>

§

impl<T> RefUnwindSafe for Subscription<T>
where T: RefUnwindSafe,

§

impl<T> Send for Subscription<T>

§

impl<T> Sync for Subscription<T>

§

impl<T> Unpin for Subscription<T>

§

impl<T> UnsafeUnpin for Subscription<T>

§

impl<T> UnwindSafe for Subscription<T>
where T: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

impl<T> Message for T
where T: Any + Send + 'static,

Source§

fn from_boxed(m: BoxedMessage) -> Result<Self, BoxedDowncastErr>

Convert a BoxedMessage to this concrete type
Source§

fn box_message(self, pid: &ActorId) -> Result<BoxedMessage, BoxedDowncastErr>

Convert this message to a BoxedMessage
Source§

impl<T> OutputMessage for T
where T: Message + Clone,

Source§

impl<T> State for T
where T: Any + Send + 'static,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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