164 Commits

Author SHA1 Message Date
Mazdak Farrokhzad
cceefd3d98
Rollup merge of #65887 - lzutao:doc-vec-get, r=rkruppe
doc: mention `get(_mut)` in Vec
2019-10-28 21:36:06 +01:00
Lzu Tao
e2c450b8da doc: mention get(_mut) in Vec 2019-10-28 05:39:37 +00:00
Lzu Tao
3f980785fb doc: explain why it is unsafe to construct Vec<u8> from Vec<u16>
Co-authored-by: Steve Klabnik <steve@steveklabnik.com>
2019-10-27 17:31:01 +00:00
Jake Goulding
6600cf6040 Add {String,Vec}::into_raw_parts 2019-10-25 11:25:52 -04:00
Jake Goulding
dce8fabc62 Use ManuallyDrop in examples for {Vec,String}::from_raw_parts 2019-10-25 11:22:53 -04:00
Mazdak Farrokhzad
7628cada4f
Rollup merge of #64912 - lzutao:unneeded-main-doc, r=jonas-schievink
Remove unneeded `fn main` blocks from docs

## [No whitespace diff](https://github.com/rust-lang/rust/pull/64912/files?w=1)
2019-10-01 23:56:23 +02:00
Lzu Tao
6c1b447f2e Remove unneeded fn main blocks from docs 2019-10-01 11:55:46 +00:00
Tyler Mandry
a8ed9bfe02
Rollup merge of #64893 - SimonSapin:vec-of-option-box, r=sfackler
Zero-initialize `vec![None; n]` for `Option<&T>`, `Option<&mut T>` and `Option<Box<T>>`
2019-09-30 14:38:27 -07:00
Simon Sapin
6c01c0e9b5 Zero-initialize vec![None; n] for Option<&T>, Option<&mut T> and Option<Box<T>> 2019-09-29 11:14:59 +02:00
Simon Sapin
ce60da497b Fix vec![x; n] with null raw fat pointer zeroing the pointer metadata
https://github.com/rust-lang/rust/pull/49496 introduced specialization based on:

```
unsafe impl<T: ?Sized> IsZero for *mut T {
    fn is_zero(&self) -> bool {
        (*self).is_null()
    }
}
```

… to call `RawVec::with_capacity_zeroed` for creating `Vec<*mut T>`,
which is incorrect for fat pointers
since `<*mut T>::is_null` only looks at the data component.
That is, a fat pointer can be “null” without being made entirely of zero bits.

This commit fixes it by removing the `?Sized` bound on this impl
(and the corresponding `*const T` one).
This regresses `vec![x; n]` with `x` a null raw slice of length zero,
but that seems exceptionally uncommon.
(Vtable pointers are never null, so raw trait objects would not take
the fast path anyway.

An alternative to keep the `?Sized` bound
(or even generalize to `impl<U: Copy> IsZero for U`)
would be to cast to `&[u8]` of length `size_of::<U>()`,
but the optimizer seems not to be able to propagate alignment information
and sticks with comparing one byte at a time:

https://rust.godbolt.org/z/xQFkwL

----

Without the library change, the new test fails as follows:

```
---- vec::vec_macro_repeating_null_raw_fat_pointer stdout ----
[src/liballoc/tests/vec.rs:1301] ptr_metadata(raw_dyn) = 0x00005596ef95f9a8
[src/liballoc/tests/vec.rs:1306] ptr_metadata(vec[0]) = 0x0000000000000000
thread 'vec::vec_macro_repeating_null_raw_fat_pointer' panicked at 'assertion failed: vec[0] == null_raw_dyn', src/liballoc/tests/vec.rs:1307:5
```
2019-09-29 11:06:53 +02:00
Mark Rousskov
f359a94849 Snap cfgs to new beta 2019-09-25 08:42:46 -04:00
Mazdak Farrokhzad
d5fe5831ec Const-stabilize Vec::new. 2019-09-16 16:45:16 +02:00
Kornel
223600ac2c Guarantee vec.clear/truncate is O(1) for trivial types 2019-09-11 20:14:23 +01:00
Oliver Scherer
26e9990198 Add a "diagnostic item" scheme
This allows lints and other diagnostics to refer to items
by a unique ID instead of relying on whacky path
resolution schemes that may break when items are
relocated.
2019-08-30 01:00:55 +02:00
Simon Sapin
36b18a1901 Rename CollectionAllocError to TryReserveError 2019-08-16 18:08:06 +02:00
Mazdak Farrokhzad
8b57f689d5 Use const generics for some Vec/CCow impls. 2019-07-28 06:33:54 +02:00
bors
890881f8f4 Auto merge of #60340 - mgeier:cap-vs-capacity, r=alexcrichton
Rename .cap() methods to .capacity()

As mentioned in #60316, there are a few `.cap()` methods, which seem out-of-place because such methods are called `.capacity()` in the rest of the code.

This PR renames them to `.capacity()` but leaves `RawVec::cap()` in there for backwards compatibility.

I didn't try to mark the old version as "deprecated", because I guess this would cause too much noise.
2019-07-25 18:45:42 +00:00
bors
09ab31bc64 Auto merge of #61224 - aloucks:drain_filter, r=Gankro
Prevent Vec::drain_filter from double dropping on panic

Fixes: #60977

The changes in this PR prevent leaking and double-panicking in addition to double-drop.

Tracking issue: #43244
2019-07-08 22:03:26 +00:00
Aaron Loucks
df5b32ee9b Clarify double-drop comment 2019-07-07 16:36:19 -04:00
Aaron Loucks
a4a6a67a01 Remove while loop in DrainFilter::drop and add additional docs 2019-07-07 13:44:21 -04:00
Taylor Cramer
95275658f2 Add Vec::leak 2019-06-28 13:24:58 -07:00
Felix Rabe
7bdc38d3a4
Succinctify splice docs 2019-06-01 11:26:08 +02:00
Aaron Loucks
b13ae65b8b Refactor DrainFilter::next and update comments 2019-05-27 12:47:47 -04:00
Aaron Loucks
17a517a42a Fix formatting nit 2019-05-27 11:30:23 -04:00
Aaron Loucks
84ae9699c6 Prevent Vec::drain_filter from double dropping on panic
Fixes: #60977
2019-05-26 23:27:31 -04:00
Ralf Jung
428ab7e1bd shadow as_ptr as as_mut_ptr in Vec to avoid going through Deref 2019-05-25 10:36:07 +02:00
Ralf Jung
f44b264447 fix dangling reference in Vec::append 2019-05-23 16:30:54 +02:00
Steven Fackler
8a22bc3b30 Revert "Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators."
This reverts commit 3e86cf36b5114f201868bf459934fe346a76a2d4.
2019-05-22 14:09:34 -07:00
Mazdak Farrokhzad
bab03cecfe
Rollup merge of #60130 - khuey:efficient_last, r=sfackler
Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators

Provided a `DoubleEndedIterator` has finite length, `Iterator::last` is equivalent to `DoubleEndedIterator::next_back`. But searching forwards through the iterator when it's unnecessary is obviously not good for performance. I ran into this on one of the collection iterators.

I tried adding appropriate overloads for a bunch of the iterator adapters like filter, map, etc, but I ran into a lot of type inference failures after doing so.

The other interesting case is what to do with `Repeat`. Do we consider it part of the contract that `Iterator::last` will loop forever on it? The docs do say that the iterator will be evaluated until it returns None. This is also relevant for the adapters, it's trivially easy to observe whether a `Map` adapter invoked its closure a zillion times or just once for the last element.
2019-05-14 22:00:09 +02:00
Josh Stone
0545375ca6 Add examples of ordered retain 2019-05-10 18:01:50 -07:00
Josh Stone
9b3583375d Document the order of {Vec,VecDeque,String}::retain
It's natural for `retain` to work in order from beginning to end, but
this wasn't actually documented to be the case. If we actually promise
this, then the caller can do useful things like track the index of each
element being tested, as [discussed in the forum][1]. This is now
documented for `Vec`, `VecDeque`, and `String`.

[1]: https://users.rust-lang.org/t/vec-retain-by-index/27697

`HashMap` and `HashSet` also have `retain`, and the `hashbrown`
implementation does happen to use a plain `iter()` order too, but it's
not certain that this should always be the case for these types.
2019-04-29 18:32:05 -07:00
Matthias Geier
0967d28be7 Rename .cap() methods to .capacity()
... but leave the old names in there for backwards compatibility.
2019-04-27 22:43:10 +02:00
Kyle Huey
3e86cf36b5 Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators.
r?Manishearth
2019-04-19 21:52:43 -07:00
Scott McMurray
df4ea90b39 Use lifetime contravariance to elide more lifetimes in core+alloc+std 2019-03-09 19:10:28 -08:00
Josh Stone
e478cadbbe Add a tracking issue for new as_slice methods 2019-03-05 16:28:32 -08:00
Josh Stone
538a0963ff Add as_slice() to slice::IterMut and vec::Drain
In bluss/indexmap#88, we found that there was no easy way to implement
`Debug` for our `IterMut` and `Drain` iterators. Those are built on
`slice::IterMut` and `vec::Drain`, which implement `Debug` themselves,
but have no other way to access their data. With a new `as_slice()`
method, we can read the data and customize its presentation.
2019-03-04 15:12:45 -08:00
Jens Hausdorf
670a4d65d5
Fix typo in Vec#resize_with documentation 2019-03-01 13:19:00 +01:00
Mazdak Farrokhzad
93bfa92a3d
Rollup merge of #58628 - RReverser:optimise-vec-false, r=oli-obk
Optimise vec![false; N] to zero-alloc

Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
2019-02-23 09:25:32 +01:00
Mazdak Farrokhzad
0bcb66469c
Rollup merge of #57656 - scottmcm:deprecate-resize_default, r=SimonSapin
Deprecate the unstable Vec::resize_default

As a way to either get additional feedback to stabilize or help move nightly users off it.

Tracking issue: https://github.com/rust-lang/rust/issues/41758#issuecomment-449719961

r? @SimonSapin
2019-02-22 14:57:55 +01:00
Ingvar Stepanyan
9f58c5fa7c Optimise vec![false; N] to zero-alloc
Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
2019-02-21 23:02:34 +00:00
kennytm
e3a8f7db47
Rollup merge of #58553 - scottmcm:more-ihle, r=Centril
Use more impl header lifetime elision

Inspired by seeing explicit lifetimes on these two:

- https://doc.rust-lang.org/nightly/std/slice/struct.Iter.html#impl-FusedIterator
- https://doc.rust-lang.org/nightly/std/primitive.u32.html#impl-Not

And a follow-up to https://github.com/rust-lang/rust/pull/54687, that started using IHLE in libcore.

Most of the changes in here fall into two big categories:

- Removing lifetimes from common traits that can essentially never user a lifetime from an input (particularly `Drop`, `Debug`, and `Clone`)

- Forwarding impls that are only possible because the lifetime doesn't matter (like `impl<R: Read + ?Sized> Read for &mut R`)

I omitted things that seemed like they could be more controversial, like the handful of iterators that have a `Item: 'static` despite the iterator having a lifetime or the `PartialEq` implementations [where the flipped one cannot elide the lifetime](https://internals.rust-lang.org/t/impl-type-parameter-aliases/9403/2?u=scottmcm).

I also removed two lifetimes that turned out to be completely unused; see https://github.com/rust-lang/rust/issues/41960#issuecomment-464557423
2019-02-20 11:59:10 +08:00
Scott McMurray
3bea2ca49d Use more impl header lifetime elision
There are two big categories of changes in here

- Removing lifetimes from common traits that can essentially never user a lifetime from an input (particularly `Drop` & `Debug`)
- Forwarding impls that are only possible because the lifetime doesn't matter (like `impl<R: Read + ?Sized> Read for &mut R`)

I omitted things that seemed like they could be more controversial, like the handful of iterators that have a `Item: 'static` despite the iterator having a lifetime or the `PartialEq` implementations where the flipped one cannot elide the lifetime.
2019-02-17 19:42:36 -08:00
Alexander Regueiro
99ed06eb88 libs: doc comments 2019-02-10 23:57:25 +00:00
Alexander Regueiro
b87363e763 tests: doc comments 2019-02-10 23:42:32 +00:00
Mazdak Farrokhzad
2396780cda liballoc: revert nested imports style changes. 2019-02-03 08:27:44 +01:00
Mazdak Farrokhzad
857530cef1 liballoc: fix some idiom lints. 2019-02-02 12:48:12 +01:00
Mazdak Farrokhzad
e70c2fbd5c liballoc: elide some lifetimes. 2019-02-02 12:23:15 +01:00
Mazdak Farrokhzad
3bfa0a35f6 liballoc: prefer imports of borrow from libcore. 2019-02-02 10:53:27 +01:00
Mazdak Farrokhzad
f09f62f62c liballoc: adjust abolute imports + more import fixes. 2019-02-02 10:34:36 +01:00
Mazdak Farrokhzad
7693e3e666 liballoc: refactor & fix some imports. 2019-02-02 10:14:40 +01:00