summary refs log tree commit diff
path: root/library/alloc/tests
AgeCommit message (Collapse)AuthorLines
2021-02-03Revert stabilizing integer::BITS.Mara Bos-0/+1
2021-02-02Auto merge of #79015 - WaffleLapkin:vec_append_from_within, r=KodrAusbors-0/+71
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-01-31add `Vec::extend_from_within` methodWaffle-0/+71
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-31stabilize int_bits_constAshley Mannix-1/+0
2021-01-18Fix soundness issue for `replace_range` and `range`dylni-0/+50
2021-01-04Stabilize split_inclusiveIan Jackson-1/+0
Closes #72360. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2020-12-31Auto merge of #79895 - Kerollmops:slice-group-by, r=m-ou-sebors-0/+59
The return of the GroupBy and GroupByMut iterators on slice According to https://github.com/rust-lang/rfcs/pull/2477#issuecomment-742034372, I am opening this PR again, this time I implemented it in safe Rust only, it is therefore much easier to read and is completely safe. This PR proposes to add two new methods to the slice, the `group_by` and `group_by_mut`. These two methods provide a way to iterate over non-overlapping sub-slices of a base slice that are separated by the predicate given by the user (e.g. `Partial::eq`, `|a, b| a.abs() < b.abs()`). ```rust let slice = &[1, 1, 1, 3, 3, 2, 2, 2]; let mut iter = slice.group_by(|a, b| a == b); assert_eq!(iter.next(), Some(&[1, 1, 1][..])); assert_eq!(iter.next(), Some(&[3, 3][..])); assert_eq!(iter.next(), Some(&[2, 2, 2][..])); assert_eq!(iter.next(), None); ``` [An RFC](https://github.com/rust-lang/rfcs/pull/2477) was open 2 years ago but wasn't necessary.
2020-12-26Auto merge of #79022 - SpyrosRoum:stabilize-deque_range, r=m-ou-sebors-1/+0
stabilize deque_range Make #74217 stable, stabilizing `VecDeque::range` and `VecDeque::range_mut`. Pr: #74099 r? `@m-ou-se`
2020-12-13replace assert! with assert_eq!Justus K-1/+1
2020-12-13fix panic if converting ZST Vec to VecDequeJustus K-0/+7
2020-12-10Update the slice GroupBy/Mut testClément Renault-4/+16
2020-12-10Implement it with only safe codeClément Renault-10/+29
2020-12-10Introduce the GroupBy and GroupByMut IteratorsClément Renault-0/+28
2020-12-04 Rename `AllocRef` to `Allocator` and `(de)alloc` to `(de)allocate`Tim Diekmann-4/+4
2020-12-02break formatting so rustfmt is happyRalf Jung-1/+2
2020-12-02disable a ptr equality test on MiriRalf Jung-1/+5
2020-11-30Make ui test that are run-pass and do not test the compiler itself library testsChristiaan Dirkx-1/+95
2020-11-26Fix new 'unnecessary trailing semicolon' warningsAaron Hill-3/+3
2020-11-15stabilize deque_rangeSpyros Roum-1/+0
2020-11-05Rollup merge of #76718 - poliorcetics:vec-ui-to-unit-test, r=jyn514Mara Bos-1/+193
Move Vec UI tests to unit tests when possible Helps with #76268. I'm moving the tests using `Vec` or `VecDeque`. ````@rustbot```` modify labels: A-testsuite C-cleanup T-libs
2020-10-29Added test for issue #78498Giacomo Stevanato-0/+15
2020-10-20Check for exhaustion in SliceIndex for RangeInclusiveJosh Stone-0/+29
2020-10-17Move subslice pattern tests to alloc/tests/slice.rsAlexis Bourget-0/+120
2020-10-17Move vec swap testAlexis Bourget-1/+13
2020-10-17Move vec-macro-repeat testAlexis Bourget-0/+13
2020-10-17Rebase conflictsAlexis Bourget-0/+30
2020-10-17Move zero-sized-vec-push testAlexis Bourget-0/+17
2020-10-09liballoc: VecDeque: Add binary search functionsVojtech Kral-0/+40
2020-10-07Auto merge of #74194 - mbrubeck:slice-eq, r=sfacklerbors-1/+2
Add PartialEq impls for Vec <-> slice This is a follow-up to #71660 and rust-lang/rfcs#2917 to add two more missing vec/slice PartialEq impls: ``` impl<A, B> PartialEq<[B]> for Vec<A> where A: PartialEq<B> { .. } impl<A, B> PartialEq<Vec<B>> for [A] where A: PartialEq<B> { .. } ``` Since this is insta-stable, it should go through the `@rust-lang/libs` FCP process. Note that I used version 1.47.0 for the `stable` attribute because I assume this will not merge before the 1.46.0 branch is cut next week.
2020-09-28Rollup merge of #76454 - poliorcetics:ui-to-unit-test-1, r=matkladRalf Jung-1/+409
UI to unit test for those using Cell/RefCell/UnsafeCell Helps with #76268. I'm working on all files using `Cell` and moving them to unit tests when possible. r? @matklad
2020-09-25review: fix nits and move panic safety tests to the correct placeAlexis Bourget-13/+11
2020-09-23Rollup merge of #76993 - blitzerr:alloc-ref, r=AmanieuDylan DPC-1/+1
Changing the alloc() to accept &self instead of &mut self Fixes: [#55](https://github.com/rust-lang/wg-allocators/issues/55) This is the first cut. It only makes the change for `alloc` method.
2020-09-22Update library functions with stability attributesDylan MacKenzie-0/+1
This may not be strictly minimal, but all unstable functions also need a `rustc_const_unstable` attribute.
2020-09-22removing &mut self for other methods of AllocRefblitzerr-1/+1
2020-09-21Move vec-cycle-wrapped testAlexis Bourget-0/+41
2020-09-21Move vec-cycle testAlexis Bourget-0/+39
2020-09-21Move deref-lval testAlexis Bourget-0/+9
2020-09-21move format! interface testsAlexis Bourget-1/+322
2020-09-19Use `T::BITS` instead of `size_of::<T> * 8`.Mara Bos-4/+4
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 #76369 - ayushmishra2005:move_various_str_tests_library, ↵Ralf Jung-0/+21
r=jyn514 Move Various str tests in library Moved various string ui tests in library as a part of #76268 r? @matklad
2020-09-16Rollup merge of #75882 - pickfire:patch-6, r=jyn514Dylan DPC-2/+2
Use translated variable for test string Test should be educative, added english translation and pronounciation.
2020-09-16Rollup merge of #75146 - tmiasko:range-overflow, r=Mark-SimulacrumDylan DPC-0/+70
Detect overflow in proc_macro_server subspan * Detect overflow in proc_macro_server subspan * Add tests for overflow in Vec::drain * Add tests for overflow in String / VecDeque operations using ranges
2020-09-15fix slice::check_range aliasing problemsRalf Jung-1/+1
2020-09-15reduce size of test_from_iter_specialization_with_iterator_adapters test in MiriRalf Jung-1/+1
2020-09-15Auto merge of #76682 - richkadel:vec-take, r=Mark-Simulacrumbors-0/+18
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/+18
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-13note that test_stable_pointers does not reflect a stable guaranteeRalf Jung-0/+3
2020-09-07Auto merge of #76368 - ayushmishra2005:move_str_contact_library, r=jyn514bors-0/+8
Added str tests in library Added str tests in library as a part of #76268 r? @matklad
2020-09-07Rollup merge of #76324 - ayushmishra2005:move_vec_tests_in_library, r=matkladDylan DPC-0/+23
Move Vec slice UI tests in library Moved some of Vec slice UI tests in Library as a part of #76268 r? @matklad