Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4bd32c9

Browse files
committedMay 29, 2020
Auto merge of #72747 - Dylan-DPC:rollup-vvydkgl, r=Dylan-DPC
Rollup of 9 pull requests Successful merges: - #72310 (Add Peekable::next_if) - #72383 (Suggest using std::mem::drop function instead of explicit destructor call) - #72398 (SocketAddr and friends now correctly pad its content) - #72465 (Warn about unused captured variables) - #72568 (Implement total_cmp for f32, f64) - #72572 (Add some regression tests) - #72591 (librustc_middle: Rename upvar_list to closure_captures) - #72701 (Fix grammar in liballoc raw_vec) - #72731 (Add missing empty line in E0619 explanation) Failed merges: r? @ghost
2 parents 96dd469 + 180a92c commit 4bd32c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1274
-154
lines changed
 

‎src/liballoc/raw_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
315315
/// `used_capacity + needed_extra_capacity` elements. If it doesn't already,
316316
/// will reallocate the minimum possible amount of memory necessary.
317317
/// Generally this will be exactly the amount of memory necessary,
318-
/// but in principle the allocator is free to give back more than
318+
/// but in principle the allocator is free to give back more than what
319319
/// we asked for.
320320
///
321321
/// If `used_capacity` exceeds `self.capacity()`, this may fail to actually allocate

‎src/libcore/iter/adapters/mod.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,69 @@ impl<I: Iterator> Peekable<I> {
16191619
let iter = &mut self.iter;
16201620
self.peeked.get_or_insert_with(|| iter.next()).as_ref()
16211621
}
1622+
1623+
/// Consume the next value of this iterator if a condition is true.
1624+
///
1625+
/// If `func` returns `true` for the next value of this iterator, consume and return it.
1626+
/// Otherwise, return `None`.
1627+
///
1628+
/// # Examples
1629+
/// Consume a number if it's equal to 0.
1630+
/// ```
1631+
/// #![feature(peekable_next_if)]
1632+
/// let mut iter = (0..5).peekable();
1633+
/// // The first item of the iterator is 0; consume it.
1634+
/// assert_eq!(iter.next_if(|&x| x == 0), Some(0));
1635+
/// // The next item returned is now 1, so `consume` will return `false`.
1636+
/// assert_eq!(iter.next_if(|&x| x == 0), None);
1637+
/// // `next_if` saves the value of the next item if it was not equal to `expected`.
1638+
/// assert_eq!(iter.next(), Some(1));
1639+
/// ```
1640+
///
1641+
/// Consume any number less than 10.
1642+
/// ```
1643+
/// #![feature(peekable_next_if)]
1644+
/// let mut iter = (1..20).peekable();
1645+
/// // Consume all numbers less than 10
1646+
/// while iter.next_if(|&x| x < 10).is_some() {}
1647+
/// // The next value returned will be 10
1648+
/// assert_eq!(iter.next(), Some(10));
1649+
/// ```
1650+
#[unstable(feature = "peekable_next_if", issue = "72480")]
1651+
pub fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item> {
1652+
match self.next() {
1653+
Some(matched) if func(&matched) => Some(matched),
1654+
other => {
1655+
// Since we called `self.next()`, we consumed `self.peeked`.
1656+
assert!(self.peeked.is_none());
1657+
self.peeked = Some(other);
1658+
None
1659+
}
1660+
}
1661+
}
1662+
1663+
/// Consume the next item if it is equal to `expected`.
1664+
///
1665+
/// # Example
1666+
/// Consume a number if it's equal to 0.
1667+
/// ```
1668+
/// #![feature(peekable_next_if)]
1669+
/// let mut iter = (0..5).peekable();
1670+
/// // The first item of the iterator is 0; consume it.
1671+
/// assert_eq!(iter.next_if_eq(&0), Some(0));
1672+
/// // The next item returned is now 1, so `consume` will return `false`.
1673+
/// assert_eq!(iter.next_if_eq(&0), None);
1674+
/// // `next_if_eq` saves the value of the next item if it was not equal to `expected`.
1675+
/// assert_eq!(iter.next(), Some(1));
1676+
/// ```
1677+
#[unstable(feature = "peekable_next_if", issue = "72480")]
1678+
pub fn next_if_eq<R>(&mut self, expected: &R) -> Option<I::Item>
1679+
where
1680+
R: ?Sized,
1681+
I::Item: PartialEq<R>,
1682+
{
1683+
self.next_if(|next| next == expected)
1684+
}
16221685
}
16231686

16241687
/// An iterator that rejects elements while `predicate` returns `true`.

0 commit comments

Comments
 (0)
Please sign in to comment.