1
Fork 0
Commit graph

244 commits

Author SHA1 Message Date
Ralf Jung
e6510babc7
Rollup merge of #72584 - CAD97:stabilize-58957, r=dtolnay
Stabilize vec::Drain::as_slice

and add `AsRef<[T]> for Drain<'_, T>`.

Tracking issue: #58957. Does not stabilize `slice::IterMut::as_slice` yet. cc @cuviper
This PR proposes stabilizing just the `vec::Drain::as_slice` part of that tracking issue.

My ultimate goal here: being able to use `for<T, I: Iterator<Item=T> + AsRef<[T]>> I` to refer to `vec::IntoIter`, `vec::Drain`, and eventually `array::IntoIter`, as an approximation of the set of by-value iterators that can be "previewed" as by-ref iterators. (Actually expressing that as a trait requires GAT.)
2020-06-15 09:57:26 +02:00
Nicholas Nethercote
c9cbe7e7eb Rename some identifiers in RawVec and libarena.
- Use `len` more consistently for the number of elements in a vector,
  because that's the usual name.
- Use `additional` more consistently for the number of elements we want
  to add, because that's what `Vec::reserve()` uses.
- Use `cap` consistently rather than `capacity`.
- Plus a few other tweaks.

This increases consistency and conciseness.
2020-06-09 07:55:35 +10:00
Ralf Jung
13815e4b35
Rollup merge of #72811 - pickfire:liballoc-impl, r=Amanieu
Liballoc impl

Mainly code rearrangements
2020-06-08 09:55:28 +02:00
Ralf Jung
b0559bebd8
Rollup merge of #72583 - CAD97:vec-iter-asref-slice, r=dtolnay
impl AsRef<[T]> for vec::IntoIter<T>

Adds `impl<T> AsRef<[T]> for vec::IntoIter<T>`. This mirrors the same trait impl for [`slice::Iter`](https://doc.rust-lang.org/nightly/std/slice/struct.Iter.html). Both types already offer `fn as_slice(&self) -> &[T]`, this just adds the trait impl for `vec::IntoIter`.

If/when `fn as_slice(&self) -> &[T]` stabilizes for `vec::Drain` and `slice::IterMut`, they should get `AsRef<[T]>` impls as well. As thus, tangentially related to #58957.

My ultimate goal here: being able to use `for<T, I: Iterator<Item=T> + AsRef<[T]>> I` to refer to `vec::IntoIter`, `vec::Drain`, and eventually `array::IntoIter`, as an approximation of the set of by-value iterators that can be "previewed" as by-ref iterators. (Actually expressing that as a trait requires GAT.)
2020-06-08 09:55:20 +02:00
Ivan Tham
758aa23e44 Elide type on liballoc vec 2020-06-06 23:07:02 +08:00
Ivan Tham
29ab6b73e1 Add more assert to Vec with_capacity docs
Show assertion on len too to show them how adding new items will affect both the
length and capacity, before and after.
2020-06-05 01:11:01 +08:00
Ivan Tham
596b0c88cc Add assert to Vec with_capacity docs 2020-06-03 01:29:02 +08:00
Ivan Tham
79449986b7 Rearrange impl blocks with Deref as first
The other blocks depends on Deref to make it easier for readers when
reimplementing or reading the implementations.
2020-05-31 17:16:47 +08:00
Josh Stone
6700e18688 Add Extend::{extend_one,extend_reserve}
This adds new optional methods on `Extend`: `extend_one` add a single
element to the collection, and `extend_reserve` pre-allocates space for
the predicted number of incoming elements. These are used in `Iterator`
for `partition` and `unzip` as they shuffle elements one-at-a-time into
their respective collections.
2020-05-29 17:05:17 -07:00
CAD97
738f8487fd stabilize vec_drain_as_slice 2020-05-25 15:35:59 -04:00
CAD97
91f52a51a2 impl AsRef<[T]> for vec::IntoIter<T> 2020-05-25 15:17:28 -04:00
bors
85f0da67ff Auto merge of #71321 - matthewjasper:alloc-min-spec, r=sfackler
Use min_specialization in liballoc

- Remove a type parameter from `[A]RcFromIter`.
- Remove an implementation of `[A]RcFromIter` that didn't actually
  specialize anything.
- Remove unused implementation of `IsZero` for `Option<&mut T>`.
- Change specializations of `[A]RcEqIdent` to use a marker trait version
of `Eq`.
- Remove `BTreeClone`. I couldn't find a way to make this work with
  `min_specialization`.
- Add `rustc_unsafe_specialization_marker` to `Copy` and `TrustedLen`.

After this only libcore is the only standard library crate using `feature(specialization)`.
cc #31844
2020-05-14 23:22:47 +00:00
Tyler Mandry
4adebb9f29
Rollup merge of #71148 - bluss:vec-drop-raw-slice, r=RalfJung
Vec drop and truncate: drop using raw slice *mut [T]

By creating a *mut [T] directly (without going through &mut [T]), avoid
questions of validity of the contents of the slice.

Consider the following risky code:

```rust
unsafe {
    let mut v = Vec::<bool>::with_capacity(16);
    v.set_len(16);
}
```

The intention is that with this change, we avoid one of the soundness
questions about the above snippet, because Vec::drop no longer
produces a mutable slice of the vector's contents.

r? @RalfJung
2020-04-30 15:23:08 -07:00
Ulrik Sverdrup
f654daf3a6 Vec IntoIter: Drop using raw slice
Update Vec drop with a comment to explain why we want to use a raw
slice, and extend this pattern to also include the Vec's IntoIter.
2020-04-28 23:31:32 +02:00
Matthew Jasper
cb2703945c Use min_specialization in liballoc
- Remove a type parameter from `[A]RcFromIter`.
- Remove an implementation of `[A]RcFromIter` that didn't actually
  specialize anything.
- Remove unused implementation of `IsZero` for `Option<&mut T>`.
- Change specializations of `[A]RcEqIdent` to use a marker trait version
of `Eq`.
- Remove `BTreeClone`. I couldn't find a way to make this work with
  `min_specialization`.
- Add `rustc_unsafe_specialization_marker` to `Copy` and `TrustedLen`.
2020-04-26 16:27:13 +01:00
Matthias Krüger
139c646251 Fix clippy warnings
clippy::{filter_next,single_char_pattern,unit_arg,identity_conversion,nonminimal_bool}
2020-04-15 23:38:48 +02:00
Ulrik Sverdrup
7612ad7797 Vec drop and truncate: drop using raw slice *mut [T]
By creating a *mut [T] directly (without going through &mut [T]), avoid
questions of validity of the contents of the slice.

Consider the following risky code:

```rust
unsafe {
    let mut v = Vec::<bool>::with_capacity(16);
    v.set_len(16);
}
```

The intention is that with this change, the above snippet will be
sound because Vec::drop does no longer produces a mutable slice of
the vector's contents.
2020-04-14 21:51:48 +02:00
IgorPerikov
9fc77c0e15 add detailed panic messages for Vec functions 2020-04-06 17:53:56 +03:00
Dylan DPC
7e4ed72d64
Rollup merge of #70558 - RalfJung:vec-extend-aliasing, r=Amanieu
Fix some aliasing issues in Vec

`Vec::extend` and `Vec::truncate` invalidated references into the vector even without reallocation, because they (implicitly) created a mutable reference covering the *entire* initialized part of the vector.

Fixes https://github.com/rust-lang/rust/issues/70301
I verified the fix by adding some new tests here that I ran in Miri.
2020-04-05 13:13:08 +02:00
Ralf Jung
7e81c11aa8 tweak swap_remove 2020-04-05 08:40:40 +02:00
Trevor Spiteri
2b718e8d9c use ManuallyDrop instead of forget inside collections
This commit changes some usage of mem::forget into mem::ManuallyDrop
in some Vec, VecDeque, BTreeMap and Box methods.

Before the commit, the generated IR for some of the methods was
longer, and even after optimization, some unwinding artifacts were
still present.
2020-04-04 14:30:33 +02:00
bors
127a11a344 Auto merge of #70362 - TimDiekmann:alloc-overhaul, r=Amanieu
Overhaul of the `AllocRef` trait to match allocator-wg's latest consens; Take 2

GitHub won't let me reopen #69889 so I make a new PR.

In addition to #69889 this fixes the unsoundness of `RawVec::into_box` when using allocators supporting overallocating. Also it uses `MemoryBlock` in `AllocRef` to unify `_in_place` methods by passing `&mut MemoryBlock`. Additionally, `RawVec` now checks for `size_of::<T>()` again and ignore every ZST. The internal capacity of `RawVec` isn't used by ZSTs anymore, as `into_box` now requires a length to be specified.

r? @Amanieu

fixes rust-lang/wg-allocators#38
fixes rust-lang/wg-allocators#41
fixes rust-lang/wg-allocators#44
fixes rust-lang/wg-allocators#51
2020-04-02 06:08:35 +00:00
Ralf Jung
5bbaac357d fix and test aliasing in swap_remove 2020-03-30 13:34:03 +02:00
Ralf Jung
8f479e362f fix aliasing in remove()
also add smoke test to detect relocation even in rustc runs
2020-03-30 13:24:55 +02:00
Ralf Jung
fa6c883074 fix ptr invalidation in Vec::truncate 2020-03-30 11:58:16 +02:00
Ralf Jung
86c1c43420 fix pointer invalidation when extnding a vector from an untrusted iterator 2020-03-30 11:58:16 +02:00
Ralf Jung
6556549fa6 fix Vec::extend invalidating unrelated pointers 2020-03-30 11:58:16 +02:00
Mazdak Farrokhzad
c51fcb5f38
Rollup merge of #68692 - jyn514:vec-from-array, r=LukasKalbertodt
impl From<[T; N]> for Vec<T>

Closes https://github.com/rust-lang/rust/issues/67963
2020-03-29 11:50:10 +02:00
Tim Diekmann
2526accdd3 Fix issues from review and unsoundness of RawVec::into_box 2020-03-26 17:11:47 +01:00
Tim Diekmann
56cbf2f22a Overhaul of the AllocRef trait to match allocator-wg's latest consens 2020-03-26 17:10:54 +01:00
Kornel
2f7d7c0333 must_use on split_off 2020-03-20 14:40:35 +00:00
Mazdak Farrokhzad
080d41391d
Rollup merge of #69828 - RalfJung:vec-leak, r=kennytm
fix memory leak when vec::IntoIter panics during drop

Fixes https://github.com/rust-lang/rust/issues/69770
2020-03-11 14:03:47 +01:00
Joshua Nelson
3477e67a92 Allow vec.rs to be over 3000 lines :( 2020-03-10 23:49:45 +00:00
Joshua Nelson
8212584c9e Bump release cutoff 2020-03-10 23:44:46 +00:00
Joshua Nelson
1ac4a46142 make the impl a little prettier 2020-03-10 23:44:46 +00:00
Joshua Nelson
96794d86f1 fix test failure 2020-03-10 23:44:46 +00:00
Joshua Nelson
f267d9dc19 limit From impl to LengthAtMost32
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
2020-03-10 23:44:46 +00:00
Joshua Nelson
daeb8ece8c fix error compiling stage2
Co-Authored-By: lzutao <taolzu@gmail.com>
2020-03-10 23:44:46 +00:00
Joshua Nelson
62722735fb impl From<[T; N]> for Vec<T> 2020-03-10 23:44:46 +00:00
Christopher Durham
a56196205b
Vec::new is const tstable in 1.39 not 1.32 2020-03-09 23:07:04 -04:00
Ralf Jung
528cbc4879 fix memory leak when vec::IntoIter panics during drop 2020-03-08 16:43:03 +01:00
Mazdak Farrokhzad
10f999b72d
Rollup merge of #69773 - matthiaskrgr:typos, r=petrochenkov
fix various typos
2020-03-07 17:27:32 +01:00
Matthias Krüger
83980aca20 Don't redundantly repeat field names (clippy::redundant_field_names) 2020-03-06 19:42:18 +01:00
Matthias Krüger
136ad015b6 fix various typos 2020-03-06 15:19:31 +01:00
Dylan DPC
55d0a8b201
Rollup merge of #69568 - JOE1994:patch-2, r=Dylan-DPC
Clarify explanation of Vec<T> 'fn resize'

1. Clarified on what should implement `Clone` trait.
2. Minor grammar fix:
to be able clone => to be able **to** clone
2020-03-01 17:23:29 +01:00
Ralf Jung
0edc90cd18 clarify alignment requirements in Vec::from_raw_parts 2020-02-29 14:07:20 +01:00
Youngsuk Kim
6e265c5bc5
Remove trailing whitespace
Removed trailing whitespace which caused to fail pretty-check
2020-02-29 00:55:05 -05:00
Youngsuk Kim
fb46d2b82e
Update src/liballoc/vec.rs
Following suggestion from @jonas-schievink

Co-Authored-By: Jonas Schievink <jonasschievink@gmail.com>
2020-02-29 00:52:32 -05:00
Youngsuk Kim
2ad52cd16d
Clarify explanation of 'fn resize'
1. Clarified on what should implement 'Clone' trait.
2. Minor grammar fix:
to be able clone => to be able to clone
2020-02-28 19:28:26 -05:00
bors
892cb143e5 Auto merge of #67290 - jonas-schievink:leak-audit, r=KodrAus
Audit liballoc for leaks in `Drop` impls when user destructor panics

Inspired by https://github.com/rust-lang/rust/pull/67243 and https://github.com/rust-lang/rust/pull/67235, this audits and hopefully fixes the remaining `Drop` impls in liballoc for resource leaks in the presence of panics in destructors called by the affected `Drop` impl.

This does not touch `Hash{Map,Set}` since they live in hashbrown. They have similar issues though.

r? @KodrAus
2020-02-26 12:48:53 +00:00