about summary refs log tree commit diff
path: root/library/alloc/src
AgeCommit message (Collapse)AuthorLines
2024-05-01Describe and use CStr literals in CStr and CString docsGeorge Bateman-9/+5
2024-04-28Stabilize `non_null_convenience`Trevor Gross-1/+0
Fully stabilize the following API, including const where applicable: impl <T> NonNull<T> { pub const unsafe fn offset(self, count: isize) -> Self; pub const unsafe fn add(self, count: usize) -> Self; pub const unsafe fn sub(self, count: usize) -> Self; pub const unsafe fn offset_from(self, origin: NonNull<T>) -> isize; pub const unsafe fn read(self) -> T; pub unsafe fn read_volatile(self) -> T; pub const unsafe fn read_unaligned(self) -> T; pub unsafe fn write_volatile(self, val: T); pub unsafe fn replace(self, src: T) -> T; } impl<T: ?Sized> NonNull<T> { pub const unsafe fn byte_offset(self, count: isize) -> Self; pub const unsafe fn byte_add(self, count: usize) -> Self; pub const unsafe fn byte_sub(self, count: usize) -> Self; pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: NonNull<U>) -> isize; pub unsafe fn drop_in_place(self); } Stabilize the following without const: impl <T> NonNull<T> { // const under `const_intrinsic_copy` pub const unsafe fn copy_to(self, dest: NonNull<T>, count: usize); pub const unsafe fn copy_to_nonoverlapping(self, dest: NonNull<T>, count: usize); pub const unsafe fn copy_from(self, src: NonNull<T>, count: usize); pub const unsafe fn copy_from_nonoverlapping(self, src: NonNull<T>, count: usize); // const under `const_ptr_write` pub const unsafe fn write(self, val: T); pub const unsafe fn write_bytes(self, val: u8, count: usize); pub const unsafe fn write_unaligned(self, val: T); // const under `const_swap` pub const unsafe fn swap(self, with: NonNull<T>); // const under `const_align_offset` pub const fn align_offset(self, align: usize) -> usize; // const under `const_pointer_is_aligned` pub const fn is_aligned(self) -> bool; } Left the following unstable: impl <T> NonNull<T> { // moved gate to `ptr_sub_ptr` pub const unsafe fn sub_ptr(self, subtracted: NonNull<T>) -> usize; } impl <T: ?Sized> NonNull<T> { // moved gate to `pointer_is_aligned_to` pub const fn is_aligned_to(self, align: usize) -> bool; } Fixes: https://github.com/rust-lang/rust/issues/117691
2024-04-27WS fix.JirCep-1/+1
2024-04-27String.truncate calls Vec.truncate, in turn, and that statesJirCep-1/+1
"is greater or equal to". Beside common sense.
2024-04-27Auto merge of #124432 - zetanumbers:non_copy_into_raw_with_alloc, r=Nilstriebbors-7/+4
Relax `A: Clone` bound for `rc::Weak::into_raw_and_alloc` Makes this method to behave the same way as [`Box::into_raw_with_allocator`](https://doc.rust-lang.org/1.77.2/alloc/boxed/struct.Box.html#method.into_raw_with_allocator) and [`Vec::into_raw_parts_with_alloc`](https://doc.rust-lang.org/1.77.2/alloc/vec/struct.Vec.html#method.into_raw_parts_with_alloc). I have also noticed the inconsistent presence and naming, should probably be addressed in the future.
2024-04-27Relax `A: Clone` bound for `rc::Weak::into_raw_and_alloc`Daria Sukhonina-7/+4
2024-04-26Auto merge of #123909 - dtolnay:utf8chunks, r=joboetbors-5/+2
Stabilize `Utf8Chunks` Pending FCP in https://github.com/rust-lang/rust/issues/99543. This PR includes the proposed modification in https://github.com/rust-lang/libs-team/issues/190 as agreed in https://github.com/rust-lang/rust/issues/99543#issuecomment-2050406568.
2024-04-24Stabilize Utf8ChunksDavid Tolnay-5/+2
2024-04-24fix typo in binary_heap docsThomas Lindae-1/+1
2024-04-24Stabilise `inline_const`Gary Guo-1/+1
2024-04-22Stabilize generic `NonZero`.Markus Reiter-1/+0
2024-04-20Avoid reloading Vec::len across grow_one in pushMark Rousskov-3/+5
This saves an extra load from memory.
2024-04-17Rollup merge of #122201 - coolreader18:doc-clone_from, r=dtolnayMatthias Krüger-18/+78
Document overrides of `clone_from()` in core/std As mentioned in https://github.com/rust-lang/rust/pull/96979#discussion_r1379502413 Specifically, when an override doesn't just forward to an inner type, document the behavior and that it's preferred over simply assigning a clone of source. Also, change instances where the second parameter is "other" to "source". I reused some of the wording over and over for similar impls, but I'm not sure that the wording is actually *good*. Would appreciate feedback about that. Also, now some of these seem to provide pretty specific guarantees about behavior (e.g. will reuse the exact same allocation iff the len is the same), but I was basing it off of the docs for [`Box::clone_from`](https://doc.rust-lang.org/1.75.0/std/boxed/struct.Box.html#method.clone_from-1) - I'm not sure if providing those strong guarantees is actually good or not.
2024-04-17Address commentsNoa-3/+1
2024-04-16Stabilize `BinaryHeap::as_slice`Slanterns-2/+1
2024-04-16Box::into_raw: make Miri understand that this is a box-to-raw castRalf Jung-2/+6
2024-04-15Add vec_deque::Iter::as_slices and friendschloekek-0/+142
Add the following methods, that work similarly to VecDeque::as_slices: - alloc::collections::vec_deque::Iter::as_slices - alloc::collections::vec_deque::IterMut::into_slices - alloc::collections::vec_deque::IterMut::as_slices - alloc::collections::vec_deque::IterMut::as_mut_slices Obtaining slices from a VecDeque iterator was not previously possible.
2024-04-13Rollup merge of #123868 - eduardosm:stabilize-slice_ptr_len, r=jhprattJacob Pratt-1/+0
Stabilize (const_)slice_ptr_len and (const_)slice_ptr_is_empty_nonnull Stabilized API: ```rust impl<T> *mut [T] { pub const fn len(self) -> usize; pub const fn is_empty(self) -> bool; } impl<T> *const [T] { pub const fn len(self) -> usize; pub const fn is_empty(self) -> bool; } impl<T> NonNull<[T]> { pub const fn is_empty(self) -> bool; } ``` FCP completed in tracking issue: https://github.com/rust-lang/rust/issues/71146
2024-04-13Rollup merge of #123835 - saethlin:vec-from-nonnull, r=the8472Jacob Pratt-15/+55
Avoid more NonNull-raw-NonNull roundtrips in Vec r? the8472 The standard library in general has a lot of these round-trips from niched types to their raw innards and back. Such round-trips have overhead in debug builds since https://github.com/rust-lang/rust/pull/120594. I removed some such round-trips in that initial PR and I've been meaning to come back and hunt down more such examples (this is the last item on https://github.com/rust-lang/rust/issues/120848).
2024-04-12Avoid more NonNull-raw-NonNull roundtrips in VecBen Kimock-15/+55
2024-04-12Stabilize (const_)slice_ptr_len and (const_)slice_ptr_is_empty_nonnullEduardo Sánchez Muñoz-1/+0
2024-04-12Auto merge of #120092 - zetanumbers:pin_in_static_allocator, r=Amanieubors-3/+12
Add `A: 'static` bound for `Arc/Rc::pin_in` Analogous to https://github.com/rust-lang/rust/pull/79327 Needed to preserve pin's [drop guarantee](https://doc.rust-lang.org/std/pin/index.html#drop-guarantee)
2024-04-09Rollup merge of #123254 - stepancheg:thin-box-0-const-alloc, r=oli-obkGuillaume Gomez-13/+85
Do not allocate for ZST ThinBox (attempt 2 using const_allocate) There's PR https://github.com/rust-lang/rust/pull/123184 which avoids allocation for ZST ThinBox. That PR has an issue with unsoundness with padding in `MaybeUninit` (see comments in that PR). Also that PR relies on `Freeze` trait. This PR is much simpler implementation which does not have this problem, but it uses `const_allocate` feature. `@oli-obk` suggested that `const_allocate` should not be used for that feature. But I like how easy it to do this feature with `const_allocate`. Maybe it's OK to use `const_allocate` while `ThinBox` is unstable? Or, well, we can abandon this PR. r? `@oli-obk`
2024-04-08Add invariant to VecDeque::pop_* that len < cap if pop successfulPhilippe-Cholet-2/+8
Similar to #114370 for VecDeque instead of Vec. It now uses `core::hint::assert_unchecked`.
2024-04-07make a doctest less slow in MiriRalf Jung-1/+3
2024-04-06remove miri-test-libstd hacks that are no longer neededRalf Jung-6/+0
2024-04-05Do not allocate for ZST ThinBox attempt 2 (using const_allocate)Stepan Koltsov-13/+85
There's PR https://github.com/rust-lang/rust/pull/123184 which avoids allocation for ZST ThinBox. That PR has an issue with unsoundness with misuse of `MaybeUninit` (see comments in that PR). This PR is much simpler implementation which does not have this problem, but it uses `const_allocate` feature.
2024-04-05Auto merge of #123317 - RalfJung:test-in-miri, r=m-ou-se,saethlin,onur-ozkanbors-0/+4
Support running library tests in Miri This adds a new bootstrap subcommand `./x.py miri` which can test libraries in Miri. This is in preparation for eventually doing that as part of bors CI, but this PR only adds the infrastructure, and doesn't enable it yet. `@rust-lang/bootstrap` should this be `x.py test --miri library/core` or `x.py miri library/core`? The flag has the advantage that we don't have to copy all the arguments from `Subcommand::Test`. It has the disadvantage that most test steps just ignore `--miri` and still run tests the regular way. For clippy you went the route of making it a separate subcommand. ~~I went with a flag now as that seemed easier, but I can change this.~~ I made it a new subcommand. Note however that the regular cargo invocation would be `cargo miri test ...`, so `x.py` is still going to be different in that the `test` is omitted. That said, we could also make it `./x.py miri-test` to make that difference smaller -- that's in fact more consistent with the internal name of the command when bootstrap invokes cargo. `@rust-lang/libs` ~~unfortunately this PR does some unholy things to the `lib.rs` files of our library crates.~~ `@m-ou-se` found a way that entirely avoids library-level hacks, except for some new small `lib.miri.rs` files that hopefully you will never have to touch. There's a new hack in cargo-miri but there it is in good company...
2024-04-05Impl `DerefPure` for more std typesNadrieril-1/+4
2024-04-03add 'x.py miri', and make it work for 'library/{core,alloc,std}'Ralf Jung-0/+4
2024-04-02Auto merge of #122945 - andy-k:sorted-vec-example, r=jhprattbors-2/+4
improve example on inserting to a sorted vector to avoid shifting equal elements
2024-04-01doc: mention heap allocation earlier in String docsJani Mustonen-3/+3
Just a tiny addition. Helps with #123263.
2024-03-30Auto merge of #121948 - Gankra:stab-align, r=dtolnaybors-1/+0
stabilize ptr.is_aligned, move ptr.is_aligned_to to a new feature gate This is an alternative to #121920
2024-03-30Auto merge of #122976 - caibear:optimize_reserve_for_push, r=cuviperbors-7/+7
Remove len argument from RawVec::reserve_for_push Removes `RawVec::reserve_for_push`'s `len` argument since it's always the same as capacity. Also makes `Vec::insert` use `RawVec::reserve_for_push`.
2024-03-29stabilize ptr.is_aligned, move ptr.is_aligned_to to a new feature gateAria Beingessner-1/+0
This is an alternative to #121920
2024-03-29Auto merge of #122520 - scottmcm:stabilize_unchecked_math_basics, r=jhprattbors-1/+0
Stabilize `unchecked_{add,sub,mul}` Tracking issue: #85122 I think we might as well just stabilize these basic three. They're the ones that have `nuw`/`nsw` flags in LLVM. Notably, this doesn't include the potentially-more-complex or -more-situational things like `unchecked_neg` or `unchecked_shr` that are under different feature flags. To quote Ralf https://github.com/rust-lang/rust/issues/85122#issuecomment-1681669646, > Are there any objections to stabilizing at least `unchecked_{add,sub,mul}`? For those there shouldn't be any surprises about what their safety requirements are. *Semantially* these are [already available on stable, even in `const`, via](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=bdb1ff889b61950897f1e9f56d0c9a36) `checked_*`+`unreachable_unchecked`. So IMHO we might as well just let people write them directly, rather than try to go through a `let Some(x) = x.checked_add(y) else { unsafe { hint::unreachable_unchecked() }};` dance. I added additional text to each method to attempt to better describe the behaviour and encourage `wrapping_*` instead. r? rust-lang/libs-api
2024-03-29Auto merge of #122975 - DianQK:simplify_ub_check, r=saethlinbors-0/+1
Eliminate `UbChecks` for non-standard libraries The purpose of this PR is to allow other passes to treat `UbChecks` as constants in MIR for optimization after #122629. r? RalfJung
2024-03-28Rename reserve_for_push to grow_one and fix comment.Cai Bear-6/+6
2024-03-28Fix previous.Cai Bear-1/+2
2024-03-28Remove len argument from RawVec::reserve_for_push because it's always equal ↵Cai Bear-7/+6
to capacity. Also make Vec::insert use reserve_for_push.
2024-03-27Less generic code for Vec allocationsKornel-13/+29
2024-03-27Eliminate `UbCheck` for non-standard librariesDianQK-0/+1
2024-03-27Rollup merge of #123083 - klensy:clippy-me, r=workingjubileeGuillaume Gomez-8/+8
lib: fix some unnecessary_cast clippy lint Fixes few instances of `unnecessary_cast` clippy lint
2024-03-27Rollup merge of #123107 - avandesa:vec_pop_if, r=joboetMatthias Krüger-0/+26
Implement `Vec::pop_if` This PR adds `Vec::pop_if` to the public API, behind the `vec_pop_if` feature. ```rust impl<T> Vec<T> { pub fn pop_if<F>(&mut self, f: F) -> Option<T> where F: FnOnce(&mut T) -> bool; } ``` Tracking issue: #122741 ## Open questions - [ ] Should the first unit test be split up? - [ ] I don't see any guidance on ordering of methods in impl blocks, should I move the method elsewhere?
2024-03-26Implement `Vec::pop_if`Alex van de Sandt-0/+26
2024-03-26Rollup merge of #122835 - compiler-errors:deref-pure, r=NadrierilMatthias Krüger-3/+19
Require `DerefMut` and `DerefPure` on `deref!()` patterns when appropriate Waiting on the deref pattern syntax pr to merge r? nadrieril
2024-03-25Require DerefPure for patternsMichael Goulet-3/+19
2024-03-25Rollup merge of #122707 - reedwoodruff:string_docs_typo, r=workingjubileeJubilee-1/+1
Fix a typo in the alloc::string::String docs
2024-03-25lib: fix some unnecessary_cast clippy lintklensy-8/+8
warning: casting raw pointers to the same type and constness is unnecessary (`*mut V` -> `*mut V`) --> library\alloc\src\collections\btree\map\entry.rs:357:31 | 357 | let val_ptr = root.borrow_mut().push(self.key, value) as *mut V; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `root.borrow_mut().push (self.key, value)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting to the same type is unnecessary (`usize` -> `usize`) --> library\alloc\src\ffi\c_str.rs:411:56 | 411 | let slice = slice::from_raw_parts_mut(ptr, len as usize); | ^^^^^^^^^^^^ help: try: `len` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*mut T` -> `*mut T`) --> library\alloc\src\slice.rs:516:25 | 516 | (buf.as_mut_ptr() as *mut T).add(buf.len()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `buf.as_mut_ptr()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*mut T` -> `*mut T`) --> library\alloc\src\slice.rs:537:21 | 537 | (buf.as_mut_ptr() as *mut T).add(buf.len()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `buf.as_mut_ptr()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*const ()` -> `*const ()`) --> library\alloc\src\task.rs:151:13 | 151 | waker as *const (), | ^^^^^^^^^^^^^^^^^^ help: try: `waker` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*const ()` -> `*const ()`) --> library\alloc\src\task.rs:323:13 | 323 | waker as *const (), | ^^^^^^^^^^^^^^^^^^ help: try: `waker` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting to the same type is unnecessary (`usize` -> `usize`) --> library\std\src\sys_common\net.rs:110:21 | 110 | assert!(len as usize >= mem::size_of::<c::sockaddr_in>()); | ^^^^^^^^^^^^ help: try: `len` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting to the same type is unnecessary (`usize` -> `usize`) --> library\std\src\sys_common\net.rs:116:21 | 116 | assert!(len as usize >= mem::size_of::<c::sockaddr_in6>()); | ^^^^^^^^^^^^ help: try: `len` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
2024-03-24Amended wordingReed-2/+2