Skip to content

Commit 611c3da

Browse files
authored
Add PluralElements [try_]for_each, [try_]for_each_mut (#6479)
1 parent a278f32 commit 611c3da

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

components/plurals/src/lib.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,67 @@ impl<T> PluralElements<T> {
974974
Ok(plural_elements)
975975
}
976976

977+
/// Immutably applies a function `f` to each value.
978+
pub fn for_each<F: FnMut(&T)>(&self, mut f: F) {
979+
#[allow(clippy::unit_arg)] // consistency with map and one-liner
980+
let Ok(()) = self.try_for_each(move |x| Ok::<(), Infallible>(f(x)));
981+
}
982+
983+
/// Immutably applies a function `f` to each value,
984+
/// propagating a possible error.
985+
pub fn try_for_each<E, F: FnMut(&T) -> Result<(), E>>(&self, mut f: F) -> Result<(), E> {
986+
// Use a structure to create compile errors if another field is added
987+
let _ = PluralElements(PluralElementsInner {
988+
other: f(&self.0.other)?,
989+
zero: self.0.zero.as_ref().map(&mut f).transpose()?,
990+
one: self.0.one.as_ref().map(&mut f).transpose()?,
991+
two: self.0.two.as_ref().map(&mut f).transpose()?,
992+
few: self.0.few.as_ref().map(&mut f).transpose()?,
993+
many: self.0.many.as_ref().map(&mut f).transpose()?,
994+
explicit_zero: self.0.explicit_zero.as_ref().map(&mut f).transpose()?,
995+
explicit_one: self.0.explicit_one.as_ref().map(&mut f).transpose()?,
996+
});
997+
Ok(())
998+
}
999+
1000+
/// Mutably applies a function `f` to each value.
1001+
///
1002+
/// # Examples
1003+
///
1004+
/// ```
1005+
/// use icu_plurals::PluralElements;
1006+
///
1007+
/// let mut x = PluralElements::new(11).with_one_value(Some(15));
1008+
/// x.for_each_mut(|i| *i *= 2);
1009+
///
1010+
/// assert_eq!(*x.other(), 22);
1011+
/// assert_eq!(*x.one(), 30);
1012+
/// ```
1013+
pub fn for_each_mut<F: FnMut(&mut T)>(&mut self, mut f: F) {
1014+
#[allow(clippy::unit_arg)] // consistency with map and one-liner
1015+
let Ok(()) = self.try_for_each_mut(move |x| Ok::<(), Infallible>(f(x)));
1016+
}
1017+
1018+
/// Mutably applies a function `f` to each value,
1019+
/// propagating a possible error.
1020+
pub fn try_for_each_mut<E, F: FnMut(&mut T) -> Result<(), E>>(
1021+
&mut self,
1022+
mut f: F,
1023+
) -> Result<(), E> {
1024+
// Use a structure to create compile errors if another field is added
1025+
let _ = PluralElements(PluralElementsInner {
1026+
other: f(&mut self.0.other)?,
1027+
zero: self.0.zero.as_mut().map(&mut f).transpose()?,
1028+
one: self.0.one.as_mut().map(&mut f).transpose()?,
1029+
two: self.0.two.as_mut().map(&mut f).transpose()?,
1030+
few: self.0.few.as_mut().map(&mut f).transpose()?,
1031+
many: self.0.many.as_mut().map(&mut f).transpose()?,
1032+
explicit_zero: self.0.explicit_zero.as_mut().map(&mut f).transpose()?,
1033+
explicit_one: self.0.explicit_one.as_mut().map(&mut f).transpose()?,
1034+
});
1035+
Ok(())
1036+
}
1037+
9771038
/// Converts from `&PluralElements<T>` to `PluralElements<&T>`.
9781039
pub fn as_ref(&self) -> PluralElements<&T> {
9791040
PluralElements(PluralElementsInner {

0 commit comments

Comments
 (0)