Skip to main content

StreamCell

Struct StreamCell 

Source
pub struct StreamCell<T> { /* private fields */ }
Expand description

Versioned, thread-safe cell holding the latest value of a stream.

A StreamCell collapses a stream to its most recent value with optimistic concurrency: each successful write bumps a version counter, and writers must present the version they observed to commit. It is the shared “current value” handoff point between a producing stream and its consumers.

§Examples

use sim_lib_stream_combinators::stream_cell;

let cell = stream_cell("first".to_owned());
let snapshot = cell.get().unwrap();
assert_eq!(snapshot.version, 0);

let next = cell.set("second".to_owned(), snapshot.version).unwrap();
assert_eq!(next, 1);

// A stale version is rejected.
assert!(cell.set("third".to_owned(), snapshot.version).is_err());
assert_eq!(cell.get().unwrap().value, "second");

Implementations§

Source§

impl<T> StreamCell<T>

Source

pub fn new(value: T) -> Self

Creates a cell holding value at version 0.

Source§

impl<T: Clone> StreamCell<T>

Source

pub fn get(&self) -> Result<CellSnapshot<T>>

Reads a versioned snapshot of the current cell value.

Source

pub fn set(&self, value: T, expected_version: u64) -> Result<u64>

Writes value only if expected_version matches the current version.

On success the version is bumped and the new version is returned; a mismatch leaves the cell untouched and returns an error.

Auto Trait Implementations§

§

impl<T> !Freeze for StreamCell<T>

§

impl<T> RefUnwindSafe for StreamCell<T>

§

impl<T> Send for StreamCell<T>
where T: Send,

§

impl<T> Sync for StreamCell<T>
where T: Send,

§

impl<T> Unpin for StreamCell<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for StreamCell<T>
where T: UnsafeUnpin,

§

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