Skip to main content

Shared

Struct Shared 

Source
pub struct Shared<Value> { /* private fields */ }
Expand description

Gives multiple owners read and write access to the same value.

A clone shares this handle’s binding: rebind switches both handles to the new value. A reader or projection keeps the original value alive. It sees later changes to that value, not the replacement.

Access never waits for another reader or writer. Overlapping access returns a busy error, even inside a callback on the same thread. update changes the value in place, unlocks it, then calls the supplied publication callback. The handle itself does not notify a Store or save the value.

§Examples

use rca::Shared; // With rca-sharing alone, import rca_sharing::Shared.

let value = Shared::new(Some(1));
let alias = value.clone();
let reader = value.reader().unwrap();
let child = value.project(Option::as_ref, Option::as_mut).unwrap();

value.rebind(&Shared::new(Some(10))).unwrap();
child.update(|value| *value += 1, || {}).unwrap();
assert_eq!(reader.read(|value| *value).unwrap(), Some(2));
assert_eq!(alias.read(|value| *value).unwrap(), Some(10));

Implementations§

Source§

impl<Value> Shared<Value>

Source

pub fn new(value: Value) -> Self

Stores the value in a new handle with its own binding.

Source

pub fn reader(&self) -> Result<SharedReader<Value>, SharedAccessError>

Captures the current reference as a reader, independent of later rebinding.

§Errors

Returns Busy or Poisoned if the binding is unavailable.

Source

pub fn read<ResultValue>( &self, read: impl FnOnce(&Value) -> ResultValue, ) -> Result<ResultValue, SharedAccessError>

Reads the currently captured value with a synchronous callback.

§Errors

Returns Busy or Poisoned. This method checks the binding before the value.

§Panics

If the read callback panics, the panic propagates and poisons the value. If the callback rebinds this handle, the method releases its old reference before it returns. A value or resource destructor can panic at that point.

Source

pub fn update<ResultValue>( &self, update: impl FnOnce(&mut Value) -> ResultValue, publish: impl FnOnce(), ) -> Result<ResultValue, SharedAccessError>

Updates the value, releases its lock, then calls publish.

If update returns normally, publish runs once, even for an unchanged value. Another writer can change the value before publish reads it. To keep the exact result of this update, return an owned value from update.

§Examples
use rca::Shared; // With rca-sharing alone, import rca_sharing::Shared.
let value = Shared::new(1);
let previous = value.update(|value| std::mem::replace(value, 2), || {}).unwrap();
assert_eq!(previous, 1);
§Errors

Returns Busy or Poisoned before invoking either callback.

§Panics

A mutation panic poisons the reference and skips publication. A publication panic propagates after commit, without poisoning that reference. Releasing a reference displaced by publication may also panic before returning.

Source

pub fn try_update<ResultValue, Failure>( &self, update: impl FnOnce(&mut Value) -> Result<ResultValue, Failure>, publish: impl FnOnce(), ) -> Result<ResultValue, SharedMutationError<Failure>>

Updates the value and calls publish even if update returns an error.

If update returns Err, its writes remain. As with Self::update, this method releases the lock before it calls publish. The returned result and typed cause are owned and cannot borrow the value.

§Errors

Returns an uncommitted access failure or the committed original user cause.

§Panics

A mutation panic poisons the reference and skips publication. A publication panic propagates after commit, without poisoning that reference. Releasing a reference displaced by publication may also panic before returning.

Source

pub fn rebind(&self, target: &Self) -> Result<(), SharedAccessError>

Switches this handle and its clones to the target’s current value.

Existing readers and projections keep the original value. If the target has an independent binding, later changes to its binding do not affect this handle. The method releases the old reference outside the lock, so its destructor can access this handle.

§Errors

Checks the target binding first, then this binding. A Busy or Poisoned error leaves this binding unchanged.

§Panics

The old value’s destructor can panic after the binding changes.

Source

pub fn project<Child, Get, GetMut>( &self, get: Get, get_mut: GetMut, ) -> Result<SharedProjection<Value, Child, Get, GetMut>, SharedAccessError>
where Get: for<'a> Fn(&'a Value) -> Option<&'a Child>, GetMut: for<'a> Fn(&'a mut Value) -> Option<&'a mut Child>,

Captures a writable partial projection without caching missing values.

The getters must select the same logical field. Presence is evaluated on each access, not when constructing the projection. Total fields use Some.

§Errors

Returns Busy or Poisoned if the binding is unavailable.

Trait Implementations§

Source§

impl<Value> Clone for Shared<Value>

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

impl<Value> Debug for Shared<Value>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Value> Forget for Shared<Value>

§

impl<Value> Freeze for Shared<Value>
where *const ArcInner<Mutex<SharedReader<Value>>> is !null: Freeze, PhantomData<ArcInner<Mutex<SharedReader<Value>>>>: Freeze,

§

impl<Value> Move for Shared<Value>

§

impl<Value> RefUnwindSafe for Shared<Value>

§

impl<Value> Send for Shared<Value>
where Arc<Mutex<SharedReader<Value>>>: Send,

§

impl<Value> Sync for Shared<Value>
where Arc<Mutex<SharedReader<Value>>>: Sync,

§

impl<Value> Unpin for Shared<Value>
where Arc<Mutex<SharedReader<Value>>>: Unpin,

§

impl<Value> UnsafeUnpin for Shared<Value>
where *const ArcInner<Mutex<SharedReader<Value>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<SharedReader<Value>>>>: UnsafeUnpin,

§

impl<Value> UnwindSafe for Shared<Value>
where Arc<Mutex<SharedReader<Value>>>: UnwindSafe,

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, 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> 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.