Add 'with' and 'with_mut' to 'use_shared_state' (#1353)

This commit is contained in:
Daniel Albl authored and GitHub committed 2023-08-15 13:27:19 -05:00
1 parent 1ab5a03aef
commit 555f4d5834
1 file changed
+14
+14
View File
@@ -282,6 +282,20 @@ impl<T> UseSharedState<T> {
),
}
}
/// Take a reference to the inner value temporarily and produce a new value
#[cfg_attr(debug_assertions, track_caller)]
#[cfg_attr(debug_assertions, inline(never))]
pub fn with<O>(&self, immutable_callback: impl FnOnce(&T) -> O) -> O {
immutable_callback(&*self.read())
}
/// Take a mutable reference to the inner value temporarily and produce a new value
#[cfg_attr(debug_assertions, track_caller)]
#[cfg_attr(debug_assertions, inline(never))]
pub fn with_mut<O>(&self, mutable_callback: impl FnOnce(&mut T) -> O) -> O {
mutable_callback(&mut *self.write())
}
}
impl<T> Clone for UseSharedState<T> {