From a6d5b078f5c1ccdd11f59fff83764e94bf04f394 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Fri, 7 Jun 2024 10:27:07 -0700 Subject: [PATCH] Added ForEach::for_each_subsequent Refs #153 This turned out to be "needed" by the debug window due to how dynamic locking was nested when a for_each call was being invoked. To keep the code simple, for_each_subsequent was added. --- src/debug.rs | 2 +- src/value.rs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/debug.rs b/src/debug.rs index d7083e1..5e6184c 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -211,7 +211,7 @@ impl DebugSection { if let Some(parent) = parent.clone() { let label = label.clone(); (&children, &values) - .for_each({ + .for_each_subsequent({ move |(children, values)| { if children.is_empty() && values.is_empty() { Self::remove_child_section(&parent, &label); diff --git a/src/value.rs b/src/value.rs index 9a7d2b1..f54a927 100644 --- a/src/value.rs +++ b/src/value.rs @@ -2981,10 +2981,19 @@ pub trait ForEach { /// The borrowed representation of T to pass into the `for_each` function. type Ref<'a>; - /// Apply `for_each` to each value contained within `self`. + /// Invokes `for_each` with the current contents and each time this source's + /// contents are updated. fn for_each(&self, for_each: F) -> CallbackHandle where F: for<'a> FnMut(Self::Ref<'a>) + Send + 'static; + + /// Attaches `for_each` to this value so that it is invoked each time the + /// source's contents are updated. + /// + /// `for_each` will not be invoked with the currently stored value. + fn for_each_subsequent(&self, for_each: F) -> CallbackHandle + where + F: for<'a> FnMut(Self::Ref<'a>) + Send + 'static; } macro_rules! impl_tuple_for_each { @@ -3000,6 +3009,18 @@ macro_rules! impl_tuple_for_each { #[allow(unused_mut)] fn for_each(&self, mut for_each: F) -> CallbackHandle + where + F: for<'a> FnMut(Self::Ref<'a>) + Send + 'static, + { + { + $(let $var = self.$field.read();)+ + for_each(($(&$var,)+)); + }; + self.for_each_subsequent(for_each) + } + + #[allow(unused_mut)] + fn for_each_subsequent(&self, mut for_each: F) -> CallbackHandle where F: for<'a> FnMut(Self::Ref<'a>) + Send + 'static, { @@ -3070,7 +3091,7 @@ macro_rules! impl_tuple_for_each { // The list of tuple fields excluding the one being invoked. [$($rtype:ident $rfield:tt $rvar:ident),+] ) => { - $handles += $var.for_each((&$for_each, $(&$rvar,)+).with_clone(|(for_each, $($rvar,)+)| { + $handles += $var.for_each_subsequent((&$for_each, $(&$rvar,)+).with_clone(|(for_each, $($rvar,)+)| { move |$var: &$type| { $(let $rvar = $rvar.read();)+ let mut for_each =