rust: use `peek_mut` from the standard lib now that it's stable
Just a little cleanup of a TODO found along the way.
--- a/rust/hg-core/src/config/mod.rs Wed Jan 25 18:45:38 2023 +0100
+++ b/rust/hg-core/src/config/mod.rs Fri Jan 13 00:07:07 2023 +0100
@@ -510,30 +510,6 @@
&'a self,
section: &'a [u8],
) -> impl Iterator<Item = (&[u8], &[u8])> + 'a {
- // TODO: Use `Iterator`’s `.peekable()` when its `peek_mut` is
- // available:
- // https://doc.rust-lang.org/nightly/std/iter/struct.Peekable.html#method.peek_mut
- struct Peekable<I: Iterator> {
- iter: I,
- /// Remember a peeked value, even if it was None.
- peeked: Option<Option<I::Item>>,
- }
-
- impl<I: Iterator> Peekable<I> {
- fn new(iter: I) -> Self {
- Self { iter, peeked: None }
- }
-
- fn next(&mut self) {
- self.peeked = None
- }
-
- fn peek_mut(&mut self) -> Option<&mut I::Item> {
- let iter = &mut self.iter;
- self.peeked.get_or_insert_with(|| iter.next()).as_mut()
- }
- }
-
// Deduplicate keys redefined in multiple layers
let mut keys_already_seen = HashSet::new();
let mut key_is_new =
@@ -543,12 +519,12 @@
// This is similar to `flat_map` + `filter_map`, except with a single
// closure that owns `key_is_new` (and therefore the
// `keys_already_seen` set):
- let mut layer_iters = Peekable::new(
- self.layers
- .iter()
- .rev()
- .map(move |layer| layer.iter_section(section)),
- );
+ let mut layer_iters = self
+ .layers
+ .iter()
+ .rev()
+ .map(move |layer| layer.iter_section(section))
+ .peekable();
std::iter::from_fn(move || loop {
if let Some(pair) = layer_iters.peek_mut()?.find(&mut key_is_new) {
return Some(pair);