##// END OF EJS Templates
rust: use `peek_mut` from the standard lib now that it's stable...
Raphaël Gomès -
r50849:a1123772 default
parent child Browse files
Show More
@@ -510,30 +510,6 b' impl Config {'
510 510 &'a self,
511 511 section: &'a [u8],
512 512 ) -> impl Iterator<Item = (&[u8], &[u8])> + 'a {
513 // TODO: Use `Iterator`’s `.peekable()` when its `peek_mut` is
514 // available:
515 // https://doc.rust-lang.org/nightly/std/iter/struct.Peekable.html#method.peek_mut
516 struct Peekable<I: Iterator> {
517 iter: I,
518 /// Remember a peeked value, even if it was None.
519 peeked: Option<Option<I::Item>>,
520 }
521
522 impl<I: Iterator> Peekable<I> {
523 fn new(iter: I) -> Self {
524 Self { iter, peeked: None }
525 }
526
527 fn next(&mut self) {
528 self.peeked = None
529 }
530
531 fn peek_mut(&mut self) -> Option<&mut I::Item> {
532 let iter = &mut self.iter;
533 self.peeked.get_or_insert_with(|| iter.next()).as_mut()
534 }
535 }
536
537 513 // Deduplicate keys redefined in multiple layers
538 514 let mut keys_already_seen = HashSet::new();
539 515 let mut key_is_new =
@@ -543,12 +519,12 b' impl Config {'
543 519 // This is similar to `flat_map` + `filter_map`, except with a single
544 520 // closure that owns `key_is_new` (and therefore the
545 521 // `keys_already_seen` set):
546 let mut layer_iters = Peekable::new(
547 self.layers
548 .iter()
549 .rev()
550 .map(move |layer| layer.iter_section(section)),
551 );
522 let mut layer_iters = self
523 .layers
524 .iter()
525 .rev()
526 .map(move |layer| layer.iter_section(section))
527 .peekable();
552 528 std::iter::from_fn(move || loop {
553 529 if let Some(pair) = layer_iters.peek_mut()?.find(&mut key_is_new) {
554 530 return Some(pair);
General Comments 0
You need to be logged in to leave comments. Login now