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§
Sourcepub fn reader(&self) -> Result<SharedReader<Value>, SharedAccessError>
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.
Sourcepub fn read<ResultValue>(
&self,
read: impl FnOnce(&Value) -> ResultValue,
) -> Result<ResultValue, SharedAccessError>
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.
Sourcepub fn update<ResultValue>(
&self,
update: impl FnOnce(&mut Value) -> ResultValue,
publish: impl FnOnce(),
) -> Result<ResultValue, SharedAccessError>
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.
Sourcepub fn try_update<ResultValue, Failure>(
&self,
update: impl FnOnce(&mut Value) -> Result<ResultValue, Failure>,
publish: impl FnOnce(),
) -> Result<ResultValue, SharedMutationError<Failure>>
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.
Sourcepub fn rebind(&self, target: &Self) -> Result<(), SharedAccessError>
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.
Sourcepub 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>,
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.