Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 6 pull requests #72678

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ David Manescu <david.manescu@gmail.com> <dman2626@uni.sydney.edu.au>
David Ross <daboross@daboross.net>
Derek Chiang <derekchiang93@gmail.com> Derek Chiang (Enchi Jiang) <derekchiang93@gmail.com>
Diggory Hardy <diggory.hardy@gmail.com> Diggory Hardy <github@dhardy.name>
Donough Liu <ldm2993593805@163.com> <donoughliu@gmail.com>
Donough Liu <ldm2993593805@163.com> DingMing Liu <liudingming@bupt.edu.cn>
Dustin Bensing <dustin.bensing@googlemail.com>
Dylan Braithwaite <dylanbraithwaite1@gmail.com> <mail@dylanb.me>
Dzmitry Malyshau <kvarkus@gmail.com>
Expand Down
20 changes: 20 additions & 0 deletions src/liballoc/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,16 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
<Self as SpecExtend<I>>::spec_extend(self, iter);
}

#[inline]
fn extend_one(&mut self, item: T) {
self.push(item);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}

impl<T: Ord, I: IntoIterator<Item = T>> SpecExtend<I> for BinaryHeap<T> {
Expand Down Expand Up @@ -1406,4 +1416,14 @@ impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BinaryHeap<T> {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}

#[inline]
fn extend_one(&mut self, &item: &'a T) {
self.push(item);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}
10 changes: 10 additions & 0 deletions src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1901,13 +1901,23 @@ impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
self.insert(k, v);
});
}

#[inline]
fn extend_one(&mut self, (k, v): (K, V)) {
self.insert(k, v);
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, K: Ord + Copy, V: Copy> Extend<(&'a K, &'a V)> for BTreeMap<K, V> {
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I) {
self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
}

#[inline]
fn extend_one(&mut self, (&k, &v): (&'a K, &'a V)) {
self.insert(k, v);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
10 changes: 10 additions & 0 deletions src/liballoc/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,13 +1152,23 @@ impl<T: Ord> Extend<T> for BTreeSet<T> {
self.insert(elem);
});
}

#[inline]
fn extend_one(&mut self, elem: T) {
self.insert(elem);
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet<T> {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}

#[inline]
fn extend_one(&mut self, &elem: &'a T) {
self.insert(elem);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
10 changes: 10 additions & 0 deletions src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,11 @@ impl<T> Extend<T> for LinkedList<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
<Self as SpecExtend<I>>::spec_extend(self, iter);
}

#[inline]
fn extend_one(&mut self, elem: T) {
self.push_back(elem);
}
}

impl<I: IntoIterator> SpecExtend<I> for LinkedList<I::Item> {
Expand All @@ -1767,6 +1772,11 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T> {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}

#[inline]
fn extend_one(&mut self, &elem: &'a T) {
self.push_back(elem);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
20 changes: 20 additions & 0 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2881,13 +2881,33 @@ impl<A> Extend<A> for VecDeque<A> {
}
}
}

#[inline]
fn extend_one(&mut self, elem: A) {
self.push_back(elem);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Copy> Extend<&'a T> for VecDeque<T> {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}

#[inline]
fn extend_one(&mut self, &elem: &T) {
self.push_back(elem);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
#![feature(container_error_extra)]
#![feature(dropck_eyepatch)]
#![feature(exact_size_is_empty)]
#![feature(extend_one)]
#![feature(fmt_internals)]
#![feature(fn_traits)]
#![feature(fundamental)]
Expand Down
35 changes: 35 additions & 0 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,34 +1799,69 @@ impl Extend<char> for String {
self.reserve(lower_bound);
iterator.for_each(move |c| self.push(c));
}

#[inline]
fn extend_one(&mut self, c: char) {
self.push(c);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a> Extend<&'a char> for String {
fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}

#[inline]
fn extend_one(&mut self, &c: &'a char) {
self.push(c);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Extend<&'a str> for String {
fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(s));
}

#[inline]
fn extend_one(&mut self, s: &'a str) {
self.push_str(s);
}
}

#[stable(feature = "extend_string", since = "1.4.0")]
impl Extend<String> for String {
fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
}

#[inline]
fn extend_one(&mut self, s: String) {
self.push_str(&s);
}
}

#[stable(feature = "herd_cows", since = "1.19.0")]
impl<'a> Extend<Cow<'a, str>> for String {
fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
}

#[inline]
fn extend_one(&mut self, s: Cow<'a, str>) {
self.push_str(&s);
}
}

/// A convenience impl that delegates to the impl for `&str`.
Expand Down
20 changes: 20 additions & 0 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,16 @@ impl<T> Extend<T> for Vec<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
<Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter())
}

#[inline]
fn extend_one(&mut self, item: T) {
self.push(item);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}

// Specialization trait used for Vec::from_iter and Vec::extend
Expand Down Expand Up @@ -2316,6 +2326,16 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
self.spec_extend(iter.into_iter())
}

#[inline]
fn extend_one(&mut self, &item: &'a T) {
self.push(item);
}

#[inline]
fn extend_reserve(&mut self, additional: usize) {
self.reserve(additional);
}
}

macro_rules! __impl_slice_eq1 {
Expand Down
17 changes: 16 additions & 1 deletion src/libcore/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<I: Iterator> IntoIterator for I {
pub trait Extend<A> {
/// Extends a collection with the contents of an iterator.
///
/// As this is the only method for this trait, the [trait-level] docs
/// As this is the only required method for this trait, the [trait-level] docs
/// contain more details.
///
/// [trait-level]: trait.Extend.html
Expand All @@ -341,11 +341,26 @@ pub trait Extend<A> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T);

/// Extends a collection with exactly one element.
#[unstable(feature = "extend_one", issue = "72631")]
fn extend_one(&mut self, item: A) {
self.extend(Some(item));
}

/// Reserves capacity in a collection for the given number of additional elements.
///
/// The default implementation does nothing.
#[unstable(feature = "extend_one", issue = "72631")]
fn extend_reserve(&mut self, additional: usize) {
let _ = additional;
}
}

#[stable(feature = "extend_for_unit", since = "1.28.0")]
impl Extend<()> for () {
fn extend<T: IntoIterator<Item = ()>>(&mut self, iter: T) {
iter.into_iter().for_each(drop)
}
fn extend_one(&mut self, _item: ()) {}
}
14 changes: 10 additions & 4 deletions src/libcore/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,9 +1700,9 @@ pub trait Iterator {
) -> impl FnMut((), T) + 'a {
move |(), x| {
if f(&x) {
left.extend(Some(x));
left.extend_one(x);
} else {
right.extend(Some(x));
right.extend_one(x);
}
}
}
Expand Down Expand Up @@ -2675,14 +2675,20 @@ pub trait Iterator {
us: &'a mut impl Extend<B>,
) -> impl FnMut((), (A, B)) + 'a {
move |(), (t, u)| {
ts.extend(Some(t));
us.extend(Some(u));
ts.extend_one(t);
us.extend_one(u);
}
}

let mut ts: FromA = Default::default();
let mut us: FromB = Default::default();

let (lower_bound, _) = self.size_hint();
if lower_bound > 0 {
ts.extend_reserve(lower_bound);
us.extend_reserve(lower_bound);
}

self.fold((), extend(&mut ts, &mut us));

(ts, us)
Expand Down
41 changes: 41 additions & 0 deletions src/libcore/tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,44 @@ fn iterator_drops() {
}
assert_eq!(i.get(), 5);
}

#[test]
fn array_default_impl_avoids_leaks_on_panic() {
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
struct Bomb(usize);

impl Default for Bomb {
fn default() -> Bomb {
if COUNTER.load(Relaxed) == 3 {
panic!("bomb limit exceeded");
}

COUNTER.fetch_add(1, Relaxed);
Bomb(COUNTER.load(Relaxed))
}
}

impl Drop for Bomb {
fn drop(&mut self) {
COUNTER.fetch_sub(1, Relaxed);
}
}

let res = std::panic::catch_unwind(|| <[Bomb; 5]>::default());
let panic_msg = match res {
Ok(_) => unreachable!(),
Err(p) => p.downcast::<&'static str>().unwrap(),
};
assert_eq!(*panic_msg, "bomb limit exceeded");
// check that all bombs are successfully dropped
assert_eq!(COUNTER.load(Relaxed), 0);
}

#[test]
fn empty_array_is_always_default() {
struct DoesNotImplDefault;

let _arr = <[DoesNotImplDefault; 0]>::default();
}
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![feature(test)]
#![feature(associated_type_bounds)]
#![feature(thread_id_value)]
#![feature(extend_one)]
#![allow(rustc::default_hash_types)]

#[macro_use]
Expand Down
Loading