about summary refs log tree commit diff
path: root/library/alloc
AgeCommit message (Collapse)AuthorLines
2022-03-29Add debug assertions to some unsafe functionsBen Kimock-2/+2
These debug assertions are all implemented only at runtime using `const_eval_select`, and in the error path they execute `intrinsics::abort` instead of being a normal debug assertion to minimize the impact of these assertions on code size, when enabled. Of all these changes, the bounds checks for unchecked indexing are expected to be most impactful (case in point, they found a problem in rustc).
2022-03-28Auto merge of #95249 - HeroicKatora:set-ptr-value, r=dtolnaybors-6/+7
Refactor set_ptr_value as with_metadata_of Replaces `set_ptr_value` (#75091) with methods of reversed argument order: ```rust impl<T: ?Sized> *mut T { pub fn with_metadata_of<U: ?Sized>(self, val: *mut U) -> *mut U; } impl<T: ?Sized> *const T { pub fn with_metadata_of<U: ?Sized>(self, val: *const U) -> *const U; } ``` By reversing the arguments we achieve several clarifications: - The function closely resembles `cast` with an argument to initialize the metadata. This is easier to teach and answers a long outstanding question that had restricted cast to `Sized` pointee targets. See multiples reviews of <https://github.com/rust-lang/rust/pull/47631> - The 'object identity', in the form of provenance, is now preserved from the receiver argument to the result. This helps explain the method as a builder-style, instead of some kind of setter that would modify something in-place. Ensuring that the result has the identity of the `self` argument is also beneficial for an intuition of effects. - An outstanding concern, 'Correct argument type', is avoided by not committing to any specific argument type. This is consistent with cast which does not require its receiver to be a 'raw address'. Hopefully the usage examples in `sync/rc.rs` serve as sufficient examples of the style to convince the reader of the readability improvements of this style, when compared to the previous order of arguments. I want to take the opportunity to motivate inclusion of this method _separate_ from metadata API, separate from `feature(ptr_metadata)`. It does _not_ involve the `Pointee` trait in any form. This may be regarded as a very, very light form that does not commit to any details of the pointee trait, or its associated metadata. There are several use cases for which this is already sufficient and no further inspection of metadata is necessary. - Storing the coercion of `*mut T` into `*mut dyn Trait` as a way to dynamically cast some an arbitrary instance of the same type to a dyn trait instance. In particular, one can have a field of type `Option<*mut dyn io::Seek>` to memorize if a particular writer is seekable. Then a method `fn(self: &T) -> Option<&dyn Seek>` can be provided, which does _not_ involve the static trait bound `T: Seek`. This makes it possible to create an API that is capable of utilizing seekable streams and non-seekable streams (instead of a possible less efficient manner such as more buffering) through the same entry-point. - Enabling more generic forms of unsizing for no-`std` smart pointers. Using the stable APIs only few concrete cases are available. One can unsize arrays to `[T]` by `ptr::slice_from_raw_parts` but unsizing a custom smart pointer to, e.g., `dyn Iterator`, `dyn Future`, `dyn Debug`, can't easily be done generically. Exposing `with_metadata_of` would allow smart pointers to offer their own `unsafe` escape hatch with similar parameters where the caller provides the unsized metadata. This is particularly interesting for embedded where `dyn`-trait usage can drastically reduce code size.
2022-03-28Rollup merge of #95098 - shepmaster:vec-from-array-ref, r=dtolnayDylan DPC-0/+42
impl From<&[T; N]> and From<&mut [T; N]> for Vec<T> I really wanted to write: ```rust fn example(a: impl Into<Vec<u8>>) {} fn main() { example(b"raw"); } ```
2022-03-28Rollup merge of #95016 - janpaul123:patch-1, r=dtolnayDylan DPC-3/+7
Docs: make Vec::from_raw_parts documentation less strict This is my first PR; be gentle! In https://users.rust-lang.org/t/why-does-vec-from-raw-parts-require-same-size-and-not-same-size-capacity/73036/2?u=janpaul123 it was suggested to me that I should make a PR to make the documentation of `Vec::from_raw_parts` less strict, since we don't require `T` to have the same size, just `size_of::<T>() * capacity` to be the same, since that is what results in `Layout::size` being the same in `dealloc`, which is really what matters. Also in https://users.rust-lang.org/t/why-does-vec-from-raw-parts-require-same-size-and-not-same-size-capacity/73036/8?u=janpaul123 it was suggested that it's better to use `slice::from_raw_parts`, which I think is useful advise that could also be mentioned in the docs, so I added that too. Let me know what you think! :)
2022-03-28Rollup merge of #93755 - ↵Dylan DPC-1/+1
ChayimFriedman2:allow-comparing-vecs-with-different-allocators, r=dtolnay Allow comparing `Vec`s with different allocators using `==` See https://stackoverflow.com/q/71021633/7884305. I did not changed the `PartialOrd` impl too because it was not generic already (didn't support `Vec<T> <=> Vec<U> where T: PartialOrd<U>`). Does it needs tests? I don't think this will hurt type inference much because the default allocator is usually not inferred (`new()` specifies it directly, and even with other allocators, you pass the allocator to `new_in()` so the compiler usually knows the type). I think this requires FCP since the impls are already stable.
2022-03-27Rollup merge of #95368 - ↵Dylan DPC-1/+1
lopopolo:lopopolo/string-try-reserve-exact-doc-typo, r=Dylan-DPC Fix typo in `String::try_reserve_exact` docs Copying the pattern from `Vec::try_reserve_exact` and `String::try_reserve`, it looks like this doc comment is intending to refer to the currently-being-documented function.
2022-03-27Fix typo in `String::try_reserve_exact` docsRyan Lopopolo-1/+1
Copying the pattern from `Vec::try_reserve_exact` and `String::try_reserve`, it looks like this doc comment is intending to refer to the currently-being-documented function.
2022-03-27Use default alloc_error_handler for hermitMartin Kröning-1/+1
Hermit now properly separates kernel from userspace. Applications for hermit can now use Rust's default alloc_error_handler instead of calling the kernel's __rg_oom.
2022-03-27Debug print char 0 as '\0' rather than '\u{0}'David Tolnay-2/+2
2022-03-27Support arrays of zeros in Vec's __rust_alloc_zeroed optimizationScott McMurray-1/+8
2022-03-25Add another assertion without into_iterJörn Horstmann-2/+6
2022-03-25Add a test verifying the number of drop callsJörn Horstmann-0/+24
2022-03-25Use ManuallyDrop::take instead of into_innerJörn Horstmann-1/+1
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
2022-03-25Fix double drop of allocator in IntoIter impl of VecJörn Horstmann-7/+10
2022-03-25Adjust tests for isize::MAX allocation always being checkedCAD97-311/+142
2022-03-24Auto merge of #87667 - the8472:document-in-place-iter, r=yaahcbors-168/+317
add module-level documentation for vec's in-place iteration As requested in the last libs team meeting and during previous reviews. Feel free to point out any gaps you encounter, after all non-obvious things may with hindsight seem obvious to me. r? `@yaahc` CC `@steffahn`
2022-03-23fix some links, clarify documentation based on review feedbackThe 8472-12/+19
2022-03-23Refactor set_ptr_value as with_metadata_ofAndreas Molzer-6/+7
By reversing the arguments we achieve several clarifications: - The function closely resembles `cast` but with an argument to initialized the metadata. This is easier to teach and answers an long outstanding question that had restricted cast to `Sized` targets initially. See multiples reviews of <https://github.com/rust-lang/rust/pull/47631> - The 'object identity', in the form or provenance, is now preserved from the call receiver to the result. This helps explain the method as a builder-style, instead of some kind of setter that would modify something in-place. Ensuring that the result has the identity of the `self` argument is also beneficial for an intuition of effects. - An outstanding concern, 'Correct argument type', is avoided by not committing to any specific argument type. This is consistent with cast which does not require its receiver to be a raw address.
2022-03-23Explicitly mention overflow is what we're checkingMichael Bradshaw-4/+4
2022-03-23Clarify that `Cow::into_owned` returns owned dataJonathan Giddy-3/+3
2022-03-22Format unsafe {} blocksMichael Bradshaw-2/+6
2022-03-22Optimize RcInnerPtr::inc_strong instruction countMichael Bradshaw-10/+24
Inspired by this internals thread: https://internals.rust-lang.org/t/rc-optimization-on-64-bit-targets/16362 [The generated assembly is a bit smaller](https://rust.godbolt.org/z/TeTnf6144) and is a more efficient usage of the CPU's instruction cache. `unlikely` doesn't impact any of the small artificial tests I've done, but I've included it in case it might help more complex scenarios when this is inlined.
2022-03-22Remove impossible panic note from `Vec::append`Tobias Bucher-1/+1
Neither the number of elements in a vector can overflow a `usize`, nor can the amount of elements in two vectors.
2022-03-22rename internal helper trait AsIntoIter to AsVecIntoIterThe 8472-10/+10
2022-03-21weaken needlessly restrictive orderings on `Arc::*_count`Ibraheem Ahmed-6/+6
2022-03-21add module-level documentation for vec's in-place iterationThe8472-20/+159
2022-03-21move AsIntoIter helper trait and mark it as unsafeThe8472-11/+14
2022-03-21rename module to better reflect its purposeThe8472-1/+1
2022-03-21Rename `~const Drop` to `~const Destruct`Deadbeef-11/+26
2022-03-20Auto merge of #92962 - frank-king:btree_entry_no_insert, r=Amanieubors-41/+86
BTreeMap::entry: Avoid allocating if no insertion This PR allows the `VacantEntry` to borrow from an empty tree with no root, and to lazily allocate a new root node when the user calls `.insert(value)`.
2022-03-18impl From<&[T; N]> and From<&mut [T; N]> for Vec<T>Jake Goulding-0/+42
2022-03-18Make Weak::new constCAD97-4/+6
2022-03-16Docs: make Vec::from_raw_parts documentation less strictJP Posma-3/+7
This is my first PR; be gentle! In https://users.rust-lang.org/t/why-does-vec-from-raw-parts-require-same-size-and-not-same-size-capacity/73036/2?u=janpaul123 it was suggested to me that I should make a PR to make the documentation of `Vec::from_raw_parts` less strict, since we don't require `T` to have the same size, just `size_of::<T>() * capacity` to be the same, since that is what results in `Layout::size` being the same in `dealloc`, which is really what matters. Also in https://users.rust-lang.org/t/why-does-vec-from-raw-parts-require-same-size-and-not-same-size-capacity/73036/8?u=janpaul123 it was suggested that it's better to use `slice::from_raw_parts`, which I think is useful advise that could also be mentioned in the docs, so I added that too. Let me know what you think! :)
2022-03-16BTree: evaluate static type-related check at compile timeStein Somers-7/+9
2022-03-15fix typosDylan DPC-1/+1
2022-03-14refactor: VecDeques Iter fields to privateDeveloperC-16/+17
Made the fields of VecDeque's Iter private by creating a Iter::new(...) function to create a new instance of Iter and migrating usage to use Iter::new(...).
2022-03-11Auto merge of #94472 - JmPotato:use_maybeuninit_for_vecdeque, r=m-ou-sebors-30/+95
Use MaybeUninit in VecDeque to remove the undefined behavior of slice Signed-off-by: JmPotato <ghzpotato@gmail.com> Ref https://github.com/rust-lang/rust/issues/74189. Adjust the code to follow the [doc.rust-lang.org/reference/behavior-considered-undefined.html](https://doc.rust-lang.org/reference/behavior-considered-undefined.html). * Change the return type of `buffer_as_slice` from `&[T]` to `&[MaybeUninit<T>]`. * Add some corresponding safety comments. Benchmark results: master 8d6f527530f4ba974d922269267fe89050188789 ```rust test collections::vec_deque::tests::bench_pop_back_100 ... bench: 47 ns/iter (+/- 1) test collections::vec_deque::tests::bench_pop_front_100 ... bench: 50 ns/iter (+/- 4) test collections::vec_deque::tests::bench_push_back_100 ... bench: 69 ns/iter (+/- 10) test collections::vec_deque::tests::bench_push_front_100 ... bench: 72 ns/iter (+/- 6) test collections::vec_deque::tests::bench_retain_half_10000 ... bench: 145,891 ns/iter (+/- 7,975) test collections::vec_deque::tests::bench_retain_odd_10000 ... bench: 141,647 ns/iter (+/- 3,711) test collections::vec_deque::tests::bench_retain_whole_10000 ... bench: 120,132 ns/iter (+/- 4,078) ``` This PR ```rust test collections::vec_deque::tests::bench_pop_back_100 ... bench: 48 ns/iter (+/- 2) test collections::vec_deque::tests::bench_pop_front_100 ... bench: 51 ns/iter (+/- 3) test collections::vec_deque::tests::bench_push_back_100 ... bench: 73 ns/iter (+/- 2) test collections::vec_deque::tests::bench_push_front_100 ... bench: 73 ns/iter (+/- 2) test collections::vec_deque::tests::bench_retain_half_10000 ... bench: 131,796 ns/iter (+/- 5,440) test collections::vec_deque::tests::bench_retain_odd_10000 ... bench: 137,563 ns/iter (+/- 3,349) test collections::vec_deque::tests::bench_retain_whole_10000 ... bench: 128,815 ns/iter (+/- 3,289) ```
2022-03-11Classify BinaryHeap & LinkedList unit tests as suchStein Somers-694/+692
2022-03-11Rollup merge of #94826 - allgoewer:fix-retain-documentation, r=yaahcDylan DPC-6/+6
Improve doc wording for retain on some collections I found the documentation wording on the various retain methods on many collections to be unusual. I tried to invert the relation by switching `such that` with `for which` .
2022-03-11Improve doc wording for retain on some collectionsMaik Allgöwer-6/+6
2022-03-10Rollup merge of #93950 - T-O-R-U-S:use-modern-formatting-for-format!-macros, ↵Dylan DPC-82/+82
r=Mark-Simulacrum Use modern formatting for format! macros This updates the standard library's documentation to use the new format_args syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored). `eprintln!("{}", e)` becomes `eprintln!("{e}")`, but `eprintln!("{}", e.kind())` remains untouched.
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-82/+82
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-10Revert accidental stabilizationOli Scherer-3/+2
2022-03-10Use MaybeUninit in VecDeque to remove the undefined behavior of sliceJmPotato-30/+95
Signed-off-by: JmPotato <ghzpotato@gmail.com>
2022-03-09BTreeMap::entry: Avoid allocating if no insertionFrank King-41/+86
2022-03-09Rollup merge of #94699 - ssomers:btree_prune_insert, r=Dylan-DPCDylan DPC-40/+19
BTree: remove dead data needlessly complicating insert Possibly needless instructions generated r? rust-lang/libs r? ``@Amanieu`` cc ``@frank-king``
2022-03-07Stabilize const_fn_fn_ptr_basics and const_fn_trait_boundEric Holk-1/+1
2022-03-07BTree: remove dead data needlessly complicating insertStein Somers-40/+19
2022-02-28Rollup merge of #92399 - Veeupup:fix_vec_typo, r=Dylan-DPCMatthias Krüger-4/+4
fix typo in btree/vec doc: Self -> self this pr fixes #92345 the documentation refers to the object the method is called for, not the type, so it should be using the lower case self.
2022-02-25Switch bootstrap cfgsMark Rousskov-1/+0