about summary refs log tree commit diff
path: root/src/liballoc/vec.rs
AgeCommit message (Collapse)AuthorLines
2019-10-28Rollup merge of #65887 - lzutao:doc-vec-get, r=rkruppeMazdak Farrokhzad-2/+4
doc: mention `get(_mut)` in Vec
2019-10-28doc: mention `get(_mut)` in VecLzu Tao-2/+4
2019-10-27doc: explain why it is unsafe to construct Vec<u8> from Vec<u16>Lzu Tao-1/+5
Co-authored-by: Steve Klabnik <steve@steveklabnik.com>
2019-10-25Add {String,Vec}::into_raw_partsJake Goulding-0/+39
2019-10-25Use ManuallyDrop in examples for {Vec,String}::from_raw_partsJake Goulding-5/+5
2019-10-01Rollup merge of #64912 - lzutao:unneeded-main-doc, r=jonas-schievinkMazdak Farrokhzad-24/+20
Remove unneeded `fn main` blocks from docs ## [No whitespace diff](https://github.com/rust-lang/rust/pull/64912/files?w=1)
2019-10-01Remove unneeded `fn main` blocks from docsLzu Tao-24/+20
2019-09-30Rollup merge of #64893 - SimonSapin:vec-of-option-box, r=sfacklerTyler Mandry-0/+25
Zero-initialize `vec![None; n]` for `Option<&T>`, `Option<&mut T>` and `Option<Box<T>>`
2019-09-29Zero-initialize `vec![None; n]` for `Option<&T>`, `Option<&mut T>` and ↵Simon Sapin-0/+25
`Option<Box<T>>`
2019-09-29Fix `vec![x; n]` with null raw fat pointer zeroing the pointer metadataSimon Sapin-2/+2
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-25Snap cfgs to new betaMark Rousskov-2/+1
2019-09-16Const-stabilize `Vec::new`.Mazdak Farrokhzad-2/+2
2019-09-15Make the semantics of Vec::truncate(N) consistent with slices.gnzlbg-22/+12
This commit simplifies the implementation of `Vec::truncate(N)` and makes its semantics identical to dropping the `[vec.len() - N..]` sub-slice tail of the vector, which is the same behavior as dropping a vector containing the same sub-slice. This changes two unspecified aspects of `Vec::truncate` behavior: * the drop order, from back-to-front to front-to-back, * the behavior of `Vec::truncate` on panics: if dropping one element of the tail panics, currently, `Vec::truncate` panics, but with this PR all other elements are still dropped, and if dropping a second element of the tail panics, with this PR, the program aborts. Programs can trivially observe both changes. For example ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7bef575b83b06e82b3e3529e4edbcac7)): ```rust fn main() { struct Bomb(usize); impl Drop for Bomb { fn drop(&mut self) { panic!(format!("{}", self.0)); } } let mut v = vec![Bomb(0), Bomb(1)]; std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { v.truncate(0); })); assert_eq!(v.len(), 1); std::mem::forget(v); } ``` panics printing `1` today and succeeds. With this change, it panics printing `0` first (due to the drop order change), and then aborts with a double-panic printing `1`, just like dropping the `[Bomb(0), Bomb(1)]` slice does, or dropping `vec![Bomb(0), Bomb(1)]` does.
2019-09-11Guarantee vec.clear/truncate is O(1) for trivial typesKornel-13/+17
2019-08-30Add a "diagnostic item" schemeOliver Scherer-0/+1
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-16Rename CollectionAllocError to TryReserveErrorSimon Sapin-7/+7
2019-07-28Use const generics for some Vec/CCow impls.Mazdak Farrokhzad-33/+23
2019-07-25Auto merge of #60340 - mgeier:cap-vs-capacity, r=alexcrichtonbors-4/+4
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-08Auto merge of #61224 - aloucks:drain_filter, r=Gankrobors-9/+60
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-07Clarify double-drop commentAaron Loucks-1/+1
2019-07-07Remove while loop in DrainFilter::drop and add additional docsAaron Loucks-19/+21
2019-06-28Add Vec::leakTaylor Cramer-0/+34
2019-06-01Succinctify splice docsFelix Rabe-6/+4
2019-05-27Refactor DrainFilter::next and update commentsAaron Loucks-16/+13
2019-05-27Fix formatting nitAaron Loucks-2/+1
2019-05-26Prevent Vec::drain_filter from double dropping on panicAaron Loucks-10/+63
Fixes: #60977
2019-05-25shadow as_ptr as as_mut_ptr in Vec to avoid going through DerefRalf Jung-7/+71
2019-05-23fix dangling reference in Vec::appendRalf Jung-1/+1
2019-05-22Revert "Add implementations of last in terms of next_back on a bunch of ↵Steven Fackler-14/+0
DoubleEndedIterators." This reverts commit 3e86cf36b5114f201868bf459934fe346a76a2d4.
2019-05-14Rollup merge of #60130 - khuey:efficient_last, r=sfacklerMazdak Farrokhzad-0/+14
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-10Add examples of ordered retainJosh Stone-0/+10
2019-04-29Document the order of {Vec,VecDeque,String}::retainJosh Stone-2/+2
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-27Rename .cap() methods to .capacity()Matthias Geier-4/+4
... but leave the old names in there for backwards compatibility.
2019-04-19Add implementations of last in terms of next_back on a bunch of ↵Kyle Huey-0/+14
DoubleEndedIterators. r?Manishearth
2019-03-09Use lifetime contravariance to elide more lifetimes in core+alloc+stdScott McMurray-8/+8
2019-03-05Add a tracking issue for new as_slice methodsJosh Stone-1/+1
2019-03-04Add as_slice() to slice::IterMut and vec::DrainJosh Stone-0/+19
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-01Fix typo in Vec#resize_with documentationJens Hausdorf-1/+1
2019-02-23Rollup merge of #58628 - RReverser:optimise-vec-false, r=oli-obkMazdak Farrokhzad-0/+1
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-22Rollup merge of #57656 - scottmcm:deprecate-resize_default, r=SimonSapinMazdak Farrokhzad-0/+4
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-21Optimise vec![false; N] to zero-allocIngvar Stepanyan-0/+1
Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
2019-02-20Rollup merge of #58553 - scottmcm:more-ihle, r=Centrilkennytm-1/+1
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-17Use more impl header lifetime elisionScott McMurray-1/+1
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-10libs: doc commentsAlexander Regueiro-4/+4
2019-02-10tests: doc commentsAlexander Regueiro-1/+1
2019-02-03liballoc: revert nested imports style changes.Mazdak Farrokhzad-23/+16
2019-02-02liballoc: fix some idiom lints.Mazdak Farrokhzad-7/+7
2019-02-02liballoc: elide some lifetimes.Mazdak Farrokhzad-15/+15
2019-02-02liballoc: prefer imports of borrow from libcore.Mazdak Farrokhzad-2/+1
2019-02-02liballoc: adjust abolute imports + more import fixes.Mazdak Farrokhzad-9/+3