Skip to content

Commit

Permalink
Added ForEach::for_each_subsequent
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ecton committed Jun 7, 2024
1 parent 7bd13e2 commit a6d5b07
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 23 additions & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2981,10 +2981,19 @@ pub trait ForEach<T> {
/// 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<F>(&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<F>(&self, for_each: F) -> CallbackHandle
where
F: for<'a> FnMut(Self::Ref<'a>) + Send + 'static;
}

macro_rules! impl_tuple_for_each {
Expand All @@ -3000,6 +3009,18 @@ macro_rules! impl_tuple_for_each {

#[allow(unused_mut)]
fn for_each<F>(&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<F>(&self, mut for_each: F) -> CallbackHandle
where
F: for<'a> FnMut(Self::Ref<'a>) + Send + 'static,
{
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit a6d5b07

Please sign in to comment.