Skip to main content

SharedCell

Struct SharedCell 

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

The storage a Shared<T> value addresses.

Cloning the Arc is what “crosses by sharing” means: every task that received the Shared addresses this one cell, and the Mutex is what makes their accesses take turns.

Implementations§

Source§

impl SharedCell

Source

pub fn new(value: Transfer) -> Arc<SharedCell>

Wraps an already converted value.

Source

pub fn wrap(value: &Value, span: Span) -> Result<Arc<SharedCell>, RuntimeError>

Shared(value): wraps value, or reports why it may not be wrapped.

The payload must be task-safe for the same reason a spawned closure’s captures must be. A Shared<Vector<T>> would let a vector be reached from two tasks, which is exactly what the sentence naming Shared forbids.

Source

pub fn lock<R>( &self, span: Span, body: impl FnOnce(Value) -> Result<(R, Value), RuntimeError>, ) -> Result<R, RuntimeError>

Runs body with the wrapped value, holding the lock for the whole call, and stores back whatever body leaves in it.

body receives the value and returns its own result together with the value to store — which is the same value when the closure took it as a var alias and mutated it in place. A body that raises an error stores nothing, so a half-finished modification is never left behind for another task to find.

Trait Implementations§

Source§

impl Debug for SharedCell

A cell is a handle, and its contents are reachable only under the lock, so it shows as the handle it is rather than as what it currently holds.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.