Auto merge of #140023 - cjgillot:arena-try-alloc, r=BoxyUwU

Introduce Arena::try_alloc_from_iter.

`alloc_from_iter` already collects the iterator for reentrancy. So adding an early exit for a fallible iterator integrates naturally into the code. This avoids the other solution to allocate and dump the allocation.
This commit is contained in:
bors
2025-04-29 21:06:15 +00:00
2 changed files with 47 additions and 33 deletions

View File

@@ -177,16 +177,8 @@ impl<'a> ConditionSet<'a> {
arena: &'a DroplessArena,
f: impl Fn(Condition) -> Option<Condition>,
) -> Option<ConditionSet<'a>> {
let mut all_ok = true;
let set = arena.alloc_from_iter(self.iter().map_while(|c| {
if let Some(c) = f(c) {
Some(c)
} else {
all_ok = false;
None
}
}));
all_ok.then_some(ConditionSet(set))
let set = arena.try_alloc_from_iter(self.iter().map(|c| f(c).ok_or(()))).ok()?;
Some(ConditionSet(set))
}
}