Skip to main content

Signal

Struct Signal 

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

Latest-value state cell with a lock-free synchronous snapshot and a coalesced change feed.

Signal has a two-plane implementation. The data plane (set, set_eventually, update, and update_eventually) runs on the caller thread: it publishes the ArcSwap mirror, stores the published sequence, wakes currently parked subscribers, and then returns. The Ractor actor owns only the control plane: subscribe, unsubscribe, close, and terminal delivery.

changes() is coalesced. Each materialized subscriber keeps only a consumed sequence cursor and reads the latest global snapshot when pulled; slow subscribers skip intermediate values and writers are never backpressured. Subscribe has no get-then-subscribe gap: the actor publishes the new slot-table snapshot before the materialized stream can pull, and the first pull reads the latest global mirror + published_sequence snapshot.

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.

Values are stored as immutable Arc<T> snapshots. Datum cannot prevent user-chosen interior mutability inside T; callers should treat values placed in a Signal as logically immutable.

Implementations§

Source§

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

Source

pub fn new(initial: T) -> StreamResult<Self>

Create a new signal initialized to initial.

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 Signal::get when the caller wants an owned snapshot shared by Arc.

Source

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

Set the state and return after the value is published to current subscribers.

Source

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

Set the state on the caller thread.

In the two-plane implementation this publishes before returning; it remains named set_eventually for API compatibility with the actor-backed first implementation.

Source

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

Update the state atomically and return after publication.

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 signal, re-emitting the current final snapshot to current subscribers before completing them. Post-close subscribers receive the final snapshot and then completion.

Source

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

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

Source§

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

Source

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

A coalesced source of the current value followed by later snapshots.

Each materialization registers a fresh slot with the control actor. Slow subscribers may skip intermediate values, but always observe the newest pending snapshot and the final snapshot before completion.

Trait Implementations§

Source§

impl<T: Send + Sync + 'static> Clone for Signal<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 Signal<T>

§

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

§

impl<T> Send for Signal<T>

§

impl<T> Sync for Signal<T>

§

impl<T> Unpin for Signal<T>

§

impl<T> UnsafeUnpin for Signal<T>

§

impl<T> UnwindSafe for Signal<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