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 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. Before returning from this function, all observers will be notified that the contents have been updated.
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. Before returning from this function, all observers will be notified that the contents have been updated.
§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.
Before returning from this function, all observers will be notified that the contents have been updated.
§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.
Before returning from this function, all observers will be notified that
the contents have been updated.
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. Before returning from this function,
all observers will be notified that the contents have been updated.
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
.
map_mut()
does not require PartialEq
, and will
invoke change callbacks after accessing exclusively.
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.