about summary refs log tree commit diff
path: root/library/alloc/src/vec
AgeCommit message (Collapse)AuthorLines
2023-03-27replace advance_by returning usize with Result<(), NonZeroUsize>The 8472-4/+5
2023-03-27Change advance(_back)_by to return `usize` instead of `Result<(), usize>`The 8472-10/+4
A successful advance is now signalled by returning `0` and other values now represent the remaining number of steps that couldn't be advanced as opposed to the amount of steps that have been advanced during a partial advance_by. This simplifies adapters a bit, replacing some `match`/`if` with arithmetic. Whether this is beneficial overall depends on whether `advance_by` is mostly used as a building-block for other iterator methods and adapters or whether we also see uses by users where `Result` might be more useful.
2023-03-25Auto merge of #99929 - the8472:default-iters, r=scottmcmbors-0/+18
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: https://github.com/rust-lang/libs-team/issues/77
2023-03-21Auto merge of #106967 - saethlin:remove-vec-as-ptr-assume, r=thomccbors-11/+2
Remove the assume(!is_null) from Vec::as_ptr At a guess, this code is leftover from LLVM was worse at keeping track of the niche information here. In any case, we don't need this anymore: Removing this `assume` doesn't get rid of the `nonnull` attribute on the return type.
2023-02-28Support allocators in various Default for IntoIter implsThe 8472-2/+5
Global implements Default so we can use that as bound for all allocators
2023-02-28rewrite iterator `Default` tests as doctestsThe 8472-0/+8
2023-02-28Implement Default for some alloc/core iteratorsThe 8472-0/+7
This way one can `mem::take()` them out of structs or #[derive(Default)] on structs containing them. These changes will be insta-stable.
2023-02-27Remove or justify use of #[rustc_box]Ben Kimock-4/+1
2023-02-23docs: use intra-doc links for `Vec::get(_mut)`Michael Howell-2/+2
Now that #63351 is fixed, there's no reason not to.
2023-02-19Remove the assume(!is_null) from Vec::as_ptrBen Kimock-11/+2
2023-01-28vec: Use SpecCloneIntoVec::clone_into to implement Vec::clone_fromNeil Roberts-30/+1
In the past, Vec::clone_from was implemented using slice::clone_into. The code from clone_into was later duplicated into clone_from in 8725e4c337, which is the commit that adds custom allocator support to Vec. Presumably this was done because the slice::clone_into only works for vecs with the default allocator so it would have the wrong type to clone into Vec<T, A>. Now that the clone_into implementation is moved out into a specializable trait anyway we might as well use that to share the code between the two methods.
2023-01-19Auto merge of #106989 - clubby789:is-zero-num, r=scottmcmbors-1/+19
Implement `alloc::vec::IsZero` for `Option<$NUM>` types Fixes #106911 Mirrors the `NonZero$NUM` implementations with an additional `assert_zero_valid`. `None::<i32>` doesn't stricly satisfy `IsZero` but for the purpose of allocating we can produce more efficient codegen.
2023-01-18Update `IsZero` documentationclubby789-1/+2
2023-01-18Implement `alloc::vec::IsZero` for `Option<$NUM>` typesclubby789-0/+17
2023-01-17Don't do pointer arithmetic on pointers to deallocated memoryThe 8472-3/+9
vec::Splice can invalidate the slice::Iter inside vec::Drain. So we replace them with dangling pointers which, unlike ones to deallocated memory, are allowed.
2023-01-14Remove various double spaces in source comments.André Vennberg-6/+6
2023-01-07Document that `Vec::from_raw_parts[_in]` must be given a pointer from the ↵Kevin Reid-0/+5
correct allocator.
2022-12-30Replace libstd, libcore, liballoc in line comments.jonathanCogan-2/+2
2022-12-27Update the documentation of `Vec` to use `extend(array)` instead of ↵Chayim Refael Friedman-1/+1
`extend(array.iter().copied())`
2022-12-23fix one more unaligned self.ptr, and add testsRalf Jung-1/+1
2022-12-23fix IntoIter::drop on high-alignment ZSTRalf Jung-10/+11
2022-12-08Apply review feedback; Fix no_global_oom_handling buildScott McMurray-0/+2
2022-12-08Make `VecDeque::from_iter` O(1) from `vec(_deque)::IntoIter`Scott McMurray-0/+27
2022-11-28improve docFabian Hintringer-1/+10
2022-11-24Tune RepeatWith::try_fold and Take::for_each and Vec::extend_trustedScott McMurray-5/+9
2022-11-24Stop peeling the last iteration of the loop in `Vec::repeat_with`Scott McMurray-11/+1
2022-11-24Extract the logic for `TrustedLen` to a named method that can be called directlyScott McMurray-32/+37
2022-11-06Vec: IntoIterator signature consistencyripytide-3/+3
Also makes the code dryer.
2022-11-03Fixed typosDouwe Schulte-2/+2
Fixed a typo that has been found on two locations in comments.
2022-10-24Clairify Vec::capacity docsNixon Enraght-Moony-2/+3
Fixes #103326
2022-10-22Rollup merge of #103359 - WaffleLapkin:drain_no_mut_qqq, r=scottmcmMatthias Krüger-3/+1
Remove incorrect comment in `Vec::drain` r? ``@scottmcm`` Turns out this comment wasn't correct for 6 years, since #34951, which switched from using `slice::IterMut` into using `slice::Iter`.
2022-10-21Remove incorrect comment in `Vec::drain`Maybe Waffle-3/+1
2022-10-19Rollup merge of #103153 - ChrisDenton:leak-oom, r=m-ou-seDylan DPC-1/+0
Allow `Vec::leak` when using `no_global_oom_handling` As [the documentation notes](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.leak), `Vec::leak` hasn't allocated since 1.57. cc `@Ericson2314` in case I'm missing something.
2022-10-17Remove all uses of array_assume_initAlex Saveau-2/+2
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-10-17Allow `Vec::leak` with `no_global_oom_handling`Chris Denton-1/+0
2022-10-10Auto merge of #102596 - scottmcm:option-bool-calloc, r=Mark-Simulacrumbors-0/+22
Do the `calloc` optimization for `Option<bool>` Inspired by <https://old.reddit.com/r/rust/comments/xtiqj8/why_is_this_functional_version_faster_than_my_for/iqqy37b/>.
2022-10-09Auto merge of #89123 - the8472:push_in_capacity, r=amanieubors-0/+45
add Vec::push_within_capacity - fallible, does not allocate This method can serve several purposes. It * is fallible * guarantees that items in Vec aren't moved * allows loops that do `reserve` and `push` separately to avoid pulling in the allocation machinery a second time in the `push` part which should make things easier on the optimizer * eases the path towards `ArrayVec` a bit since - compared to `push()` - there are fewer questions around how it should be implemented I haven't named it `try_push` because that should probably occupy a middle ground that will still try to reserve and only return an error in the unlikely OOM case. resolves #84649
2022-10-04Rollup merge of #101642 - SkiFire13:fix-inplace-collection-leak, r=the8472Dylan DPC-6/+32
Fix in-place collection leak when remaining element destructor panic Fixes #101628 cc `@the8472` I went for the drop guard route, placing it immediately before the `forget_allocation_drop_remaining` call and after the comment, as to signal they are closely related. I also updated the test to check for the leak, though the only change really needed was removing the leak clean up for miri since now that's no longer leaked.
2022-10-03Rollup merge of #99216 - duarten:master, r=joshtriplettMatthias Krüger-8/+73
docs: be less harsh in wording for Vec::from_raw_parts In particular, be clear that it is sound to specify memory not originating from a previous `Vec` allocation. That is already suggested in other parts of the documentation about zero-alloc conversions to Box<[T]>. Incorporate a constraint from `slice::from_raw_parts` that was missing but needs to be fulfilled, since a `Vec` can be converted into a slice. Fixes https://github.com/rust-lang/rust/issues/98780.
2022-10-03Clarify documentationGiacomo Stevanato-4/+3
2022-10-02Do the `calloc` optimization for `Option<bool>`Scott McMurray-0/+22
Inspired by <https://old.reddit.com/r/rust/comments/xtiqj8/why_is_this_functional_version_faster_than_my_for/iqqy37b/>.
2022-09-22Make ZST checks in core/alloc more readableScott McMurray-15/+15
There's a bunch of these checks because of special handing for ZSTs in various unsafe implementations of stuff. This lets them be `T::IS_ZST` instead of `mem::size_of::<T>() == 0` every time, making them both more readable and more terse. *Not* proposed for stabilization at this time. Would be `pub(crate)` except `alloc` wants to use it too. (And while it doesn't matter now, if we ever get something like 85836 making it a const can help codegen be simpler.)
2022-09-10Update documentationGiacomo Stevanato-3/+11
2022-09-10Fix leaking in inplace collection when destructor panicsGiacomo Stevanato-3/+22
2022-09-02implement IsZero for Saturating and Wrappingasquared31415-0/+16
2022-09-01Auto merge of #100707 - dzvon:fix-typo, r=davidtwcobors-2/+2
Fix a bunch of typo This PR will fix some typos detected by [typos]. I only picked the ones I was sure were spelling errors to fix, mostly in the comments. [typos]: https://github.com/crate-ci/typos
2022-08-31fix into_iter on ZSTRalf Jung-1/+1
2022-08-31Fix a bunch of typoDezhi Wu-2/+2
This PR will fix some typos detected by [typos]. I only picked the ones I was sure were spelling errors to fix, mostly in the comments. [typos]: https://github.com/crate-ci/typos
2022-08-30Rollup merge of #95376 - WaffleLapkin:drain_keep_rest, r=dtolnayDylan DPC-3/+130
Add `vec::Drain{,Filter}::keep_rest` This PR adds `keep_rest` methods to `vec::Drain` and `vec::DrainFilter` under `drain_keep_rest` feature gate: ```rust // mod alloc::vec impl<T, A: Allocator> Drain<'_, T, A> { pub fn keep_rest(self); } impl<T, F, A: Allocator> DrainFilter<'_, T, F, A> where F: FnMut(&mut T) -> bool, { pub fn keep_rest(self); } ``` Both these methods cancel draining of elements that were not yet yielded from the iterators. While this needs more testing & documentation, I want at least start the discussion. This may be a potential way out of the "should `DrainFilter` exhaust itself on drop?" argument.
2022-08-28fill-in tracking issue for `feature(drain_keep_rest)`Maybe Waffle-2/+2