pub trait Destination<T> {
// Required method
fn try_map_mut<R>(
&self,
map: impl FnOnce(Mutable<'_, T>) -> R,
) -> Result<R, DeadlockError>;
// Provided methods
fn map_mut<R>(&self, map: impl FnOnce(Mutable<'_, T>) -> R) -> R { ... }
fn try_replace(&self, new_value: T) -> Result<T, ReplaceError<T>>
where T: PartialEq { ... }
fn replace(&self, new_value: T) -> Option<T>
where T: PartialEq { ... }
fn set(&self, new_value: T)
where T: PartialEq { ... }
fn force_set(&self, new_value: T) { ... }
fn try_compare_swap(
&self,
expected_current: &T,
new_value: T,
) -> Result<T, TryCompareSwapError<T>>
where T: Clone + PartialEq { ... }
fn compare_swap(&self, expected_current: &T, new_value: T) -> Result<T, T>
where T: Clone + PartialEq { ... }
fn toggle(&self) -> T
where T: Not<Output = T> + Clone { ... }
fn take(&self) -> T
where Self: Source<T>,
T: Default { ... }
fn take_if_not_default(&self) -> Option<T>
where T: Default + PartialEq { ... }
}
Expand description
A destination for values of type T
.
Required Methods§
Sourcefn try_map_mut<R>(
&self,
map: impl FnOnce(Mutable<'_, T>) -> R,
) -> Result<R, DeadlockError>
fn try_map_mut<R>( &self, map: impl FnOnce(Mutable<'_, T>) -> R, ) -> Result<R, DeadlockError>
Maps the contents with exclusive access. All observers will be notified at some point in the future if the value has been changed.
Provided Methods§
Sourcefn map_mut<R>(&self, map: impl FnOnce(Mutable<'_, T>) -> R) -> R
fn map_mut<R>(&self, map: impl FnOnce(Mutable<'_, T>) -> R) -> R
Maps the contents with exclusive access. All observers will be notified at some point in the future if the value has been changed.
§Panics
This function panics if this value is already locked by the current thread.
Sourcefn try_replace(&self, new_value: T) -> Result<T, ReplaceError<T>>where
T: PartialEq,
fn try_replace(&self, new_value: T) -> Result<T, ReplaceError<T>>where
T: PartialEq,
Replaces the contents with new_value
if new_value
is different than
the currently stored value. If the value is updated, the previous
contents are returned.
All observers will be notified at some point in the future if the value has been changed.
§Errors
ReplaceError::NoChange
: Returned whennew_value
is equal to the currently stored value.ReplaceError::Deadlock
: Returned when the current thread already has exclusive access to the contents of this dynamic.
Sourcefn replace(&self, new_value: T) -> Option<T>where
T: PartialEq,
fn replace(&self, new_value: T) -> Option<T>where
T: PartialEq,
Replaces the contents with new_value
, returning the previous contents.
All observers will be notified at some point in the future if the value
has been changed.
If the calling thread has exclusive access to the contents of this
dynamic, this call will return None and the value will not be updated.
If detecting this is important, use Self::try_replace()
.
§Replacing a new value without PartialEq
This function requires that the contained type implements PartialEq
.
One common problem with reactive data graphs is that they can be very
“noisy”. Cushy attempts to minimize noise by only invoking callbacks
when the value has changed, and it detects this by using PartialEq
.
However, not all types implement PartialEq
.
map_mut()
does not require PartialEq
, and can be
used along with std::mem::replace()
to perform the same operation
without checking for equality.
Sourcefn set(&self, new_value: T)where
T: PartialEq,
fn set(&self, new_value: T)where
T: PartialEq,
Stores new_value
in this dynamic. All observers will be notified at
some point in the future if the value has been changed.
If the calling thread has exclusive access to the contents of this
dynamic, this call will return None and the value will not be updated.
If detecting this is important, use Self::try_replace()
.
§Setting a new value without PartialEq
This function requires that the contained type implements PartialEq
.
One common problem with reactive data graphs is that they can be very
“noisy”. Cushy attempts to minimize noise by only invoking callbacks
when the value has changed, and it detects this by using PartialEq
.
However, not all types implement PartialEq
. See
force_set()
.
Sourcefn force_set(&self, new_value: T)
fn force_set(&self, new_value: T)
Stores new_value
in this dynamic without checking for equality.
All observers will be notified at some point that this dynamic’s contents have been changed.
Sourcefn try_compare_swap(
&self,
expected_current: &T,
new_value: T,
) -> Result<T, TryCompareSwapError<T>>
fn try_compare_swap( &self, expected_current: &T, new_value: T, ) -> Result<T, TryCompareSwapError<T>>
Replaces the current value with new_value
if the current value is
equal to expected_current
.
Returns Ok
with the overwritten value upon success.
§Errors
TryCompareSwapError::Deadlock
: This operation would result in a thread deadlock.TryCompareSwapError::CurrentValueMismatch
: The current value did not matchexpected_current
. TheT
returned is a clone of the currently stored value.
Sourcefn compare_swap(&self, expected_current: &T, new_value: T) -> Result<T, T>
fn compare_swap(&self, expected_current: &T, new_value: T) -> Result<T, T>
Replaces the current value with new_value
if the current value is
equal to expected_current
.
Returns Ok
with the overwritten value upon success.
§Errors
Returns Err
with the currently stored value when expected_current
does not match the currently stored value.
Sourcefn toggle(&self) -> T
fn toggle(&self) -> T
Updates the value to the result of invoking Not
on the current
value. This function returns the new value.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.