summary refs log tree commit diff
path: root/library/alloc/src/vec
AgeCommit message (Collapse)AuthorLines
2021-02-02Auto merge of #79015 - WaffleLapkin:vec_append_from_within, r=KodrAusbors-4/+109
add `Vec::extend_from_within` method under `vec_extend_from_within` feature gate Implement <https://github.com/rust-lang/rfcs/pull/2714> ### tl;dr This PR adds a `extend_from_within` method to `Vec` which allows copying elements from a range to the end: ```rust #![feature(vec_extend_from_within)] let mut vec = vec![0, 1, 2, 3, 4]; vec.extend_from_within(2..); assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]); vec.extend_from_within(..2); assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]); vec.extend_from_within(4..8); assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]); ``` ### Implementation notes Originally I've copied `@Shnatsel's` [implementation](https://github.com/WanzenBug/rle-decode-helper/blob/690742a0de158d391b7bde1a0c71cccfdad33ab3/src/lib.rs#L74) with some minor changes to support other ranges: ```rust pub fn append_from_within<R>(&mut self, src: R) where T: Copy, R: RangeBounds<usize>, { let len = self.len(); let Range { start, end } = src.assert_len(len);; let count = end - start; self.reserve(count); unsafe { // This is safe because `reserve()` above succeeded, // so `self.len() + count` did not overflow usize ptr::copy_nonoverlapping( self.get_unchecked(src.start), self.as_mut_ptr().add(len), count, ); self.set_len(len + count); } } ``` But then I've realized that this duplicates most of the code from (private) `Vec::append_elements`, so I've used it instead. Then I've applied `@KodrAus` suggestions from https://github.com/rust-lang/rust/pull/79015#issuecomment-727200852.
2021-02-02update tracking issue for vec_extend_from_withinAshley Mannix-1/+1
2021-01-31add `Vec::extend_from_within` methodWaffle-4/+109
Implement <https://github.com/rust-lang/rfcs/pull/2714>, changes from the RFC: - Rename the method `append_from_within` => `extend_from_within` - Loose :Copy bound => :Clone - Specialize in case of :Copy This commit also adds `Vec::split_at_spare` private method and use it to implement `Vec::spare_capacity_mut` and `Vec::extend_from_within`. This method returns 2 slices - initialized elements (same as `&mut vec[..]`) and uninitialized but allocated space (same as `vec.spare_capacity_mut()`).
2021-01-29Updated Vec::splice documentationChan Kwan Yin-1/+1
Replacing with equal number of values does not increase the length of the vec. Reference: https://stackoverflow.com/a/62559271/3990767
2021-01-26shrink_to shouldn't panic on len greater than capacityThom Wiggers-6/+6
2021-01-22Add doc aliases for memory allocationsYoshua Wuyts-0/+7
- Vec::with_capacity / Box::new -> alloc + malloc - Box::new_zeroed -> calloc - Vec::{reserve,reserve_exact,try_reserve_exact,shrink_to_fit,shrink_to} -> realloc
2021-01-21Rollup merge of #81179 - CPerezz:fix_interal_doc_warns, r=jyn514Yuki Okushi-1/+1
Fix broken links with `--document-private-items` in the standard library As it was suggested in #81037 `SpecFromIter` is not in the scope and therefore we get a warning when we try to do document private intems in `rust/library/alloc/`. This addresses #81037 by adding the trait in the scope as ```@jyn514``` suggested and also adding an `allow(unused_imports)` flag so that the compiler does not complain, Since the trait is not used per se in the code, it's just needed to have properly documented docs.
2021-01-21Remove link to current sectionIvan Tham-2/+1
Co-authored-by: Mara Bos <m-ou.se@m-ou.se>
2021-01-20Add more details explaning the Vec visualizationIvan Tham-20/+22
Suggested by oli-obk
2021-01-20Add Vec visualization to understand capacityIvan Tham-0/+21
Visualize vector while differentiating between stack and heap. Inspired by cheats.rs, as this is probably the first place beginner go, they could understand stack and heap, length and capacity with this. Not sure if adding this means we should add to other places too. Superseeds #76066
2021-01-19Add SpecFromIter ref in the comments directlyCPerezz-3/+1
2021-01-18Fix internal rustdoc broken linksCPerezz-0/+2
As it was suggested in #81037 `SpecFromIter` is not in the scope and therefore (even it should fail), we get a warning when we try do document private intems in `rust/library/alloc/`. This fixes #81037 by adding the trait in the scope and also adding an `allow(unused_imports)` flag so that the compiler does not complain, Since the trait is not used per se in the code, it's just needed to have properly documented docs.
2021-01-14Rollup merge of #80972 - KodrAus:deprecate/remove_item, r=nagisaMara Bos-21/+0
Remove unstable deprecated Vec::remove_item Closes #40062 The `Vec::remove_item` method was deprecated in `1.46.0` (in August of 2020). This PR now removes that unstable method entirely.
2021-01-13remove unstable deprecated Vec::remove_itemAshley Mannix-21/+0
2021-01-09Add comment to `Vec::truncate` explaining `>` vs `>=`Camelid-0/+3
Hopefully this will prevent people from continuing to ask about this over and over again :) See [this Zulip discussion][1] for more. [1]: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation
2020-12-30Fix rustdoc link in vec/into_iter.rs.Mara Bos-2/+2
2020-12-29docs: fixing referencesC-1/+1
2020-12-29fix: moved import into #[cfg(test)]C-2/+1
2020-12-29style: applying Rust styleC-80/+74
2020-12-29refactor: removing // ignore-tidy-filelengthC-1/+0
2020-12-29refactor: moved SpecExtend into spec_extend.rsC-79/+87
2020-12-29refactor: moving SpecFromIter into spec_from_iter.rsC-92/+102
2020-12-29refactor: moved SpecFromIterNested to spec_from_iter_nested.rsC-52/+60
2020-12-29refactor: moved InPlaceDrop into in_place_drop.rsC-22/+28
2020-12-29refactor: moved SetLenOnDrop to set_len_on_dropC-29/+32
2020-12-29refactor: moved SpecFromElem to spec_from_elem.rsC-55/+64
2020-12-29refactor: moved PartialEq into partial_eqC-39/+45
2020-12-29refactor: moving SourceIterMarker into source_iter_marker.rsC-104/+113
2020-12-29refactor: moved IsZero into is_zero.rsC-70/+75
2020-12-29refactor: moving AsIntoIter into into_iter.rsC-15/+16
2020-12-29refactor: moved IntoIter into into_iter.rsC-264/+276
2020-12-29refactor: moved Vec impl Cow into cow.rsC-35/+37
2020-12-29refactor: moving Drain into drain.rsC-147/+162
2020-12-29refactor: moving Splice into splice.rsC-128/+138
2020-12-29refactor: moving DrainFilter into drain_filter.rsC-138/+148
2020-12-29refactor: moving vec.rs to vec/mod.rsC-0/+3725