From 844f8217cae333f8f521f6e294b172d367db3499 Mon Sep 17 00:00:00 2001 From: adamritter <58403584+adamritter@users.noreply.github.com> Date: Sun, 30 Oct 2022 13:15:01 +0000 Subject: [PATCH] Add replace_object_with that can pass an object without using mutable reference. This makes it possible to change an object in place and reuse it. --- src/signal/mutable.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/signal/mutable.rs b/src/signal/mutable.rs index 37b3c08..44a4fce 100644 --- a/src/signal/mutable.rs +++ b/src/signal/mutable.rs @@ -239,6 +239,17 @@ impl fmt::Debug for ReadOnlyMutable where A: fmt::Debug { #[repr(transparent)] pub struct Mutable(ReadOnlyMutable); +impl Mutable { + pub fn replace_object_with(&self, f: F) where F: FnOnce(A) -> A { + let mut state = self.state().lock.write().unwrap(); + let old_value=std::mem::take(&mut state.value); + let new_value = f(old_value); + state.value=new_value; + + state.notify(true); + } +} + impl Mutable { // TODO should this inline ? pub fn new(value: A) -> Self {