summary refs log tree commit diff
path: root/library/alloc/src/vec.rs
AgeCommit message (Collapse)AuthorLines
2020-11-07remove needs_dropThe8472-8/+4
2020-10-01Fix typo in vec doc "tries to reserves"Ivan Tham-3/+4
2020-09-30Rollup merge of #77340 - pickfire:patch-9, r=kennytmJonas Schievink-1/+1
Alloc vec use imported path mem::ManuallyDrop::new -> ManuallyDrop::new cc @the8472
2020-09-29Alloc vec use imported pathIvan Tham-1/+1
mem::ManuallyDrop::new -> ManuallyDrop::new
2020-09-29Fix typo in alloc vec commentIvan Tham-1/+1
2020-09-25Auto merge of #77201 - matthewjasper:rename-get-unchecked, r=spastorinobors-3/+9
Rename Iterator::get_unchecked Closes #76479 r? `@pnkfelix`
2020-09-25Rename Iterator::get_uncheckedMatthew Jasper-1/+1
It's possible for method resolution to pick this method over a lower priority stable method, causing compilation errors. Since this method is permanently unstable, give it a name that is very unlikely to be used in user code.
2020-09-25Improve <vec::IntoIter>::get_unchecked` safety commentMatthew Jasper-2/+8
2020-09-25Remove extra space from vec drawingIvan Tham-1/+0
2020-09-25Rollup merge of #77050 - follower:patch-1, r=oli-obkJonas Schievink-1/+1
Typo fix: "satsify" -> "satisfy"
2020-09-23Rollup merge of #77017 - GuillaumeGomez:vec-missing-examples-iter, r=Dylan-DPCDylan DPC-0/+31
Add missing examples on Vec iter types r? @Dylan-DPC
2020-09-22Add missing examples on Vec iter typesGuillaume Gomez-0/+31
2020-09-22Typo fix: "satsify" -> "satisfy"follower-1/+1
2020-09-19Rollup merge of #76310 - scottmcm:array-try_from-vec, r=dtolnayRalf Jung-0/+52
Add `[T; N]: TryFrom<Vec<T>>` (insta-stable) This is very similar to the [existing](https://doc.rust-lang.org/nightly/std/convert/trait.TryFrom.html#impl-TryFrom%3CBox%3C%5BT%5D%3E%3E) `Box<[T; N]>: TryFrom<Box<[T]>>`, but allows avoiding the `shrink_to_fit` if you have a vector and not a boxed slice. Like the slice equivalents of this, it fails if the length of the vector is not exactly `N`. This uses `Vec<T>` as the `Error` type to return the input, like how the `Rc<[T]> -> Rc<[T; N]>` (and Arc) ones also reflect the input directly in the error type. ```rust #[stable(feature = "array_try_from_vec", since = "1.47.0")] impl<T, const N: usize> TryFrom<Vec<T>> for [T; N] { type Error = Vec<T>; fn try_from(mut vec: Vec<T>) -> Result<[T; N], Vec<T>>; } ``` Inspired by this zulip thread: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/APIs.20for.20getting.20stuff.20from.20a.20Vec.20by.20owned/near/209048103
2020-09-16Rollup merge of #76662 - RalfJung:lib-test-miri, r=Mark-SimulacrumRalf Jung-1/+1
Fix liballoc test suite for Miri Mostly, fix the regression introduced by https://github.com/rust-lang/rust/pull/75207 that caused slices (i.e., references) to be created to invalid memory or memory that has aliasing pointers that we want to keep valid. @dylni this changes the type of `check_range` to only require the length, not the full reference to the slice, which indeed is all the information this function requires. Also reduce the size of a test introduced in https://github.com/rust-lang/rust/pull/70793 to make it not take 3 minutes in Miri. This makes https://github.com/RalfJung/miri-test-libstd work again.
2020-09-16Rollup merge of #76062 - pickfire:patch-13, r=jyn514Ralf Jung-1/+3
Vec slice example fix style and show type elision
2020-09-16Rollup merge of #76056 - pickfire:patch-10, r=jyn514Ralf Jung-0/+1
Add more info for Vec Drain doc See its documentation for more
2020-09-15fix slice::check_range aliasing problemsRalf Jung-1/+1
2020-09-15Vec doc use elision as code rather than commentIvan Tham-1/+3
2020-09-15Auto merge of #76682 - richkadel:vec-take, r=Mark-Simulacrumbors-0/+5
Optimize behavior of vec.split_off(0) (take all) Optimization improvement to `split_off()` so the performance meets the intuitively expected behavior when `at == 0`, avoiding the current behavior of copying the entire vector. The change honors documented behavior that the original vector's "previous capacity unchanged". This improvement better supports the pattern for building and flushing a buffer of elements, such as the following: ```rust let mut vec = Vec::new(); loop { vec.push(something); if condition_is_met { process(vec.split_off(0)); } } ``` `Option` wrapping is the first alternative I thought of, but is much less obvious and more verbose: ```rust let mut capacity = 1; let mut vec: Option<Vec<Stuff>> = None; loop { vec.get_or_insert_with(|| Vec::with_capacity(capacity)).push(something); if condition_is_met { capacity = vec.capacity(); process(vec.take().unwrap()); } } ``` Directly using `mem::replace()` (instead of calling`split_off()`) could work, but `mem::replace()` is a more advanced tool for Rust developers, and in this case, I believe developers would assume the standard library should be sufficient for the purpose described here. The benefit of the approach to this change is it does not change the existing API contract, but improves the peformance of `split_off(0)` for `Vec`, `String` (which delegates `split_off()` to `Vec`), and any other existing use cases. This change adds tests to validate the behavior of `split_off()` with regard to capacity, as originally documented, and confirm that behavior still holds, when `at == 0`. The change is an implementation detail, and does not require a documentation change, but documenting the new behavior as part of its API contract may benefit future users. (Let me know if I should make that documentation update.) Note, for future consideration: I think it would be helpful to introduce an additional method to `Vec` (if not also to `String`): ``` pub fn take_all(&mut self) -> Self { self.split_off(0) } ``` This would make it more clear how `Vec` supports the pattern, and make it easier to find, since the behavior is similar to other `take()` methods in the Rust standard library. r? `@wesleywiser` FYI: `@tmandry`
2020-09-13Optimize behavior of vec.split_off(0) (take all)Rich Kadel-0/+5
Optimization improvement to `split_off()` so the performance meets the intuitively expected behavior when `at == 0`, avoiding the current behavior of copying the entire vector. The change honors documented behavior that the method leaves the original vector's "previous capacity unchanged". This improvement better supports the pattern for building and flushing a buffer of elements, such as the following: ```rust let mut vec = Vec::new(); loop { vec.push(something); if condition_is_met { process(vec.split_off(0)); } } ``` `Option` wrapping is the first alternative I thought of, but is much less obvious and more verbose: ```rust let mut capacity = 1; let mut vec: Option<Vec<Stuff>> = None; loop { vec.get_or_insert_with(|| Vec::with_capacity(capacity)).push(something); if condition_is_met { capacity = vec.capacity(); process(vec.take().unwrap()); } } ``` Directly applying `mem::replace()` could work, but `mem::` functions are typically a last resort, when a developer is actively seeking better performance than the standard library provides, for example. The benefit of the approach to this change is it does not change the existing API contract, but improves the peformance of `split_off(0)` for `Vec`, `String` (which delegates `split_off()` to `Vec`), and any other existing use cases. This change adds tests to validate the behavior of `split_off()` with regard to capacity, as originally documented, and confirm that behavior still holds, when `at == 0`. The change is an implementation detail, and does not require a documentation change, but documenting the new behavior as part of its API contract may benefit future users. (Let me know if I should make that documentation update.) Note, for future consideration: I think it would be helpful to introduce an additional method to `Vec` (if not also to `String`): ``` pub fn take_all(&mut self) -> Self { self.split_off(0) } ``` This would make it more clear how `Vec` supports the pattern, and make it easier to find, since the behavior is similar to other `take()` methods in the Rust standard library.
2020-09-11Auto merge of #73951 - pickfire:liballoc-intoiter, r=Mark-Simulacrumbors-33/+25
Liballoc intoiter refactor
2020-09-07Typo fix scottmcm-1/+1
Thanks, Amanieu Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2020-09-07Rollup merge of #76303 - jyn514:vec-assert-doc, r=Dylan-DPCDylan DPC-0/+3
Link to `#capacity-and-reallocation` when using with_capacity Follow up to https://github.com/rust-lang/rust/pull/76058#discussion_r479655750. r? @pickfire
2020-09-05Nightly is currently 1.48scottmcm-1/+1
2020-09-05Rollup merge of #76060 - pickfire:patch-12, r=jyn514Dylan DPC-1/+3
Link vec doc to & reference It is not always obvious that people could see the docs for `&` especially for beginners, it also helps learnability.
2020-09-04Auto merge of #75207 - dylni:add-slice-check-range, r=KodrAusbors-31/+2
Add `slice::check_range` This method is useful for [`RangeBounds`] parameters. It's even been [rewritten](https://github.com/rust-lang/rust/blob/22ee68dc586440f96b76b32fbd6087507c6afdb9/src/librustc_data_structures/sorted_map.rs#L214) [many](https://github.com/rust-lang/rust/blob/22ee68dc586440f96b76b32fbd6087507c6afdb9/library/alloc/src/vec.rs#L1299) [times](https://github.com/rust-lang/rust/blob/22ee68dc586440f96b76b32fbd6087507c6afdb9/library/core/src/slice/mod.rs#L2441) in the standard library, sometimes assuming that the bounds won't be [`usize::MAX`]. For example, [`Vec::drain`] creates an empty iterator when [`usize::MAX`] is used as an inclusive end bound: ```rust assert!(vec![1].drain(..=usize::max_value()).eq(iter::empty())); ``` If this PR is merged, I'll create another to use it for those methods. [`RangeBounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html [`usize::MAX`]: https://doc.rust-lang.org/std/primitive.usize.html#associatedconstant.MAX [`Vec::drain`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain
2020-09-03Add `[T; N]: TryFrom<Vec<T>>`Scott McMurray-0/+52
This is very similar to the existing `Box<[T; N]>: TryFrom<Box<[T]>>`, but allows avoiding the `shrink_to_fit` if you have a vector and not a boxed slice.
2020-09-04Add slice primitive link to vecIvan Tham-1/+2
2020-09-03Link to `#capacity-and-reallocation` when using with_capacityJoshua Nelson-0/+3
2020-09-03fix debug assertionThe8472-4/+18
The InPlaceIterable debug assert checks that the write pointer did not advance beyond the read pointer. But TrustedRandomAccess never advances the read pointer, thus triggering the assert. Skip the assert if the source pointer did not change during iteration.
2020-09-03improve comments and namingThe8472-25/+56
2020-09-03add explanation to specialization markerThe8472-0/+6
2020-09-03remove separate no-drop code path since it resulted in more LLVM IRThe8472-32/+15
2020-09-03remove empty Vec extend optimizationThe8472-14/+2
The optimization meant that every extend code path had to emit llvm IR for from_iter and extend spec_extend, which likely impacts compile times while only improving a few edge-cases
2020-09-03get things to work under min_specialization by leaning more heavily on ↵The8472-8/+13
#[rustc_unsafe_specialization_marker]
2020-09-03apply required min_specialization attributesThe8472-1/+7
2020-09-03generalize in-place collect to types of same size and alignmentThe8472-19/+27
2020-09-03increase comment verbosityThe8472-2/+2
2020-09-03work around compiler overhead around lambdas in generics by extracting them ↵The8472-34/+39
into free functions
2020-09-03extract IntoIter drop/forget used by specialization into separate methodsThe8472-15/+25
2020-09-03remove redundant castThe8472-1/+1
2020-09-03move unsafety into method, not relevant to callerThe8472-2/+2
2020-09-03replace unsafe ptr::write with deref-write, benchmarks show no differenceThe8472-10/+4
2020-09-03pacify tidyThe8472-3/+3
2020-09-03replace drop flag with ManuallyDropThe8472-6/+4
2020-09-03mark as_inner as unsafe and update commentsThe8472-4/+8
2020-09-03avoid exposing that binary heap's IntoIter is backed by vec::IntoIter, use a ↵The8472-5/+16
private trait instead
2020-09-03impl TrustedRandomAccess for vec::IntoIterThe8472-1/+22
2020-09-03move free-standing method into trait implThe8472-79/+75