about summary refs log tree commit diff
path: root/library/alloc/src
AgeCommit message (Collapse)AuthorLines
2025-05-21Rollup merge of #140526 - Natr1x:sort-direction-documentation, r=dtolnayMatthias Krüger-4/+7
docs: Specify that common sort functions sort in an ascending direction From [forum discussion](https://users.rust-lang.org/t/is-there-a-way-to-sort-a-slice-in-specifically-ascending-or-descending-order/128998?u=natr1x) it seems like the sorting direction can be expected to always be ascending (in terms of `cmp::Ordering`). If this is the case then it would be nice to have this stated in the documentation.
2025-05-21std: fix doctest and explain for as_slices and as_mut_slices in VecDequexizheyin-5/+28
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-05-20Rollup merge of #141289 - compiler-errors:more-self, r=jhprattMatthias Krüger-2/+2
use `Self` alias in self types rather than manually substituting it Of the rougly 145 uses of `self: Ty` in the standard library, 5 of them don't use `Self` but instead choose to manually "substitute" the `impl`'s self type into the type. This leads to weird behavior sometimes (https://github.com/rust-lang/rust/issues/140611#issuecomment-2883761300) -- **to be clear**, none of these usages actually trigger any bugs, but it's possible that they may break in the future (or at least lead to lints), so let's just "fix" them proactively.
2025-05-20use Self alias in self types rather than manually substituting itMichael Goulet-2/+2
2025-05-17Docs(lib/extract_if): Unify example descriptionPaul Mabileau-2/+2
Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17Docs(lib/extract_if): Unify paragraph about elements mutationPaul Mabileau-4/+4
Take the one from `BTreeMap` that seems the best-worded and most precise among the available variations. Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17Docs(lib/extract_if): Unify paragraph about closure actionsPaul Mabileau-6/+6
Also fixes `HashSet`'s that incorrectly designated itself as a `list`. Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17Docs(lib/coll/btm): Split `extract_if`'s first sentence from the following onesPaul Mabileau-4/+6
This also seems like a small mistake: the first main sentence is put in the same paragraph as the other two following ones while other equivalents all have it split. Therefore, do the same here. Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17Docs(lib/alloc/vec): Add the missing `an` to `extract_if`'s first sentencePaul Mabileau-1/+1
As inspired by the equivalent methods from other collection types. Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-15Auto merge of #141050 - matthiaskrgr:rollup-uyzqbmj, r=matthiaskrgrbors-4/+4
Rollup of 7 pull requests Successful merges: - #139749 (docs(library/core/src/pin): fix typo "necessarily" -> "necessary") - #140685 (Simplify `Vec::as_non_null` implementation and make it `const`) - #140712 (normalization: avoid incompletely constraining GAT args) - #140768 (Improve `dangerous_implicit_aurorefs` diagnostic output) - #140947 (Flush errors before deep normalize in `dropck_outlives`) - #140990 (VxWorks: updates from recent libc versions) - #141027 (remove `RustfmtState` to reduce `initial_rustfmt` complexity) r? `@ghost` `@rustbot` modify labels: rollup
2025-05-15Rollup merge of #140685 - viliml:patch-1, r=Mark-SimulacrumMatthias Krüger-4/+4
Simplify `Vec::as_non_null` implementation and make it `const` Tracking issue: #130364.
2025-05-15Auto merge of #136264 - GuillaumeGomez:optimize-integers-to-string, r=Amanieubors-0/+48
Optimize `ToString` implementation for integers Part of https://github.com/rust-lang/rust/issues/135543. Follow-up of https://github.com/rust-lang/rust/pull/133247 and https://github.com/rust-lang/rust/pull/128204. The benchmark results are: | name| 1.87.0-nightly (3ea711f17 2025-03-09) | With this PR | diff | |-|-|-|-| | bench_i16 | 32.06 ns/iter (+/- 0.12) | 17.62 ns/iter (+/- 0.03) | -45% | | bench_i32 | 31.61 ns/iter (+/- 0.04) | 15.10 ns/iter (+/- 0.06) | -52% | | bench_i64 | 31.71 ns/iter (+/- 0.07) | 15.02 ns/iter (+/- 0.20) | -52% | | bench_i8 | 13.21 ns/iter (+/- 0.14) | 14.93 ns/iter (+/- 0.16) | +13% | | bench_u16 | 31.20 ns/iter (+/- 0.06) | 16.14 ns/iter (+/- 0.11) | -48% | | bench_u32 | 33.27 ns/iter (+/- 0.05) | 16.18 ns/iter (+/- 0.10) | -51% | | bench_u64 | 31.44 ns/iter (+/- 0.06) | 16.62 ns/iter (+/- 0.21) | -47% | | bench_u8 | 10.57 ns/iter (+/- 0.30) | 13.00 ns/iter (+/- 0.43) | +22% | More information about it in [the original comment](https://github.com/rust-lang/rust/pull/136264#discussion_r1987542954). r? `@workingjubilee`
2025-05-12update cfg(bootstrap)Pietro Albini-2/+2
2025-05-12update version placeholdersPietro Albini-1/+1
2025-05-07Rollup merge of #140668 - vkrivopalov:vecdeque-truncate-front, r=jhprattJacob Pratt-0/+67
Implement `VecDeque::truncate_front()` Tracking issue: #140667
2025-05-07Rollup merge of #140483 - baumanj:patch-1, r=workingjubileeJacob Pratt-5/+5
Comment on `Rc` abort-guard strategy without naming unrelated fn `wrapped_add` is used, not `checked_add`, so avoid mentioning specific fn calls that may vary slightly based on "whatever produces the best code" and focus on things that will remain constant into the future.
2025-05-06Update rc.rs docsJon Bauman-5/+5
Update comment per review feedback
2025-05-06Rollup merge of #139773 - thaliaarchi:vec-into-iter-last, r=workingjubileeStuart Cook-0/+5
Implement `Iterator::last` for `vec::IntoIter` Avoid iterating everything when we have random access to the last element.
2025-05-06Rollup merge of #139764 - dtolnay:extractif, r=AmanieuStuart Cook-17/+31
Consistent trait bounds for ExtractIf Debug impls Closes #137654. Refer to that issue for a table of the **4** different impl signatures we previously had in the standard library for Debug impls of various ExtractIf iterator types. The one we are standardizing on is the one so far only used by `alloc::collections::linked_list::ExtractIf`, which is _no_ `F: Debug` bound, _no_ `F: FnMut` bound, only `T: Debug` bound. This PR applies the following signature changes: ```diff /* alloc::collections::btree_map */ pub struct ExtractIf<'a, K, V, F, A = Global> where - F: 'a + FnMut(&K, &mut V) -> bool, Allocator + Clone, impl Debug for ExtractIf<'a, K, V, F, + A, > where K: Debug, V: Debug, - F: FnMut(&K, &mut V) -> bool, + A: Allocator + Clone, ``` ```diff /* alloc::collections::btree_set */ pub struct ExtractIf<'a, T, F, A = Global> where - T: 'a, - F: 'a + FnMut(&T) -> bool, Allocator + Clone, impl Debug for ExtractIf<'a, T, F, A> where T: Debug, - F: FnMut(&T) -> bool, A: Allocator + Clone, ``` ```diff /* alloc::collections::linked_list */ impl Debug for ExtractIf<'a, T, F, + A, > where T: Debug, + A: Allocator, ``` ```diff /* alloc::vec */ impl Debug for ExtractIf<'a, T, F, A> where T: Debug, - F: Debug, A: Allocator, - A: Debug, ``` ```diff /* std::collections::hash_map */ pub struct ExtractIf<'a, K, V, F> where - F: FnMut(&K, &mut V) -> bool, impl Debug for ExtractIf<'a, K, V, F> where + K: Debug, + V: Debug, - F: FnMut(&K, &mut V) -> bool, ``` ```diff /* std::collections::hash_set */ pub struct ExtractIf<'a, T, F> where - F: FnMut(&T) -> bool, impl Debug for ExtractIf<'a, T, F> where + T: Debug, - F: FnMut(&T) -> bool, ``` I have made the following changes to bring these types into better alignment with one another. - Delete `F: Debug` bounds. These are especially problematic because Rust closures do not come with a Debug impl, rendering the impl useless. - Delete `A: Debug` bounds. Allocator parameters are unstable for now, but in the future this would become an API commitment that we do not debug-print a representation of the allocator when printing an iterator. - Delete `F: FnMut` bounds. Requires `hashbrown` PR: https://github.com/rust-lang/hashbrown/pull/616. **API commitment:** we commit to not doing RefCell voodoo inside ExtractIf to have some way for its Debug impl (which takes &amp;self) to call a FnMut closure, if this is even possible. - Add `T: Debug` bounds (or `K`/`V`), even on Debug impls that do not currently make use of them, but might in the future. **Breaking change.** Must backport into Rust 1.87 (current beta) or do a de-stabilization PR in beta to delay those types by one release. - Render using `debug_struct` + `finish_non_exhaustive`, instead of `debug_tuple`. - Do not render the _entire_ underlying collection. - Show a "peek" field indicating the current position of the iterator.
2025-05-05Consistent trait bounds for ExtractIf Debug implsDavid Tolnay-17/+31
2025-05-05Simplify `Vec::as_non_null` implementation and make it `const`Vilim Lendvaj-4/+4
2025-05-05Implement `VecDeque::truncate_front()`Vladimir Krivopalov-0/+67
Tracking issue: #140667 Signed-off-by: Vladimir Krivopalov <vladimir.krivopalov@gmail.com>
2025-05-05Rollup merge of #135734 - nk9:extract_if-doc-equivalent, r=tgross35Trevor Gross-6/+12
Correct `extract_if` sample equivalent. Tracking issue: https://github.com/rust-lang/rust/issues/43244 Original PR: #133265 The sample code marked as equivalent in the doc comment isn't currently equivalent. Given the same predicate and range, if your vector were `[1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 6]`, then all of the 3s would be removed. `i` is only incremented when an element is dropped, but `range.end` is unchanged, so the items shift down. I got very confused when reading the docs and trying to square this sample code with the explanation of how the function works. Fortunately, the real `extract_if()` does not have this problem. I've added an `end` variable to align the behavior. I've also taken the opportunity to simplify the predicate, which now just matches odd numbers, and to pad out the vec of numbers to line up the zero-indexed range with the integers in the vec. r? the8472
2025-05-04extract_if's sample equivalent now really equivalent.Nick Kocharhook-6/+12
Simpler predicate. Compare sample code output to that of the library function.
2025-05-03Suggest `retain_mut` over `retain` as `Vec::extract_if` alternativePaolo Barbolini-2/+2
2025-05-02Implement Iterator::last for vec::IntoIterThalia Archibald-0/+5
2025-04-30docs: Specify that common sort functions sort in an ascending directionNatrix-4/+7
2025-04-30Auto merge of #140188 - nnethercote:streamline-format-macro, r=cuviperbors-2/+1
Streamline the `format` macro. Removing the unnecessary local variable speeds up compilation a little. r? `@cuviper`
2025-04-29Update rc.rs docsJon Bauman-1/+1
`wrapped_add` is used, not `checked_add`
2025-04-28Rename sub_ptr to offset_from_unsigned in docsDaniPopes-2/+2
2025-04-28Auto merge of #123239 - Urgau:dangerous_implicit_autorefs, ↵bors-1/+3
r=jdonszelmann,traviscross Implement a lint for implicit autoref of raw pointer dereference - take 2 *[t-lang nomination comment](https://github.com/rust-lang/rust/pull/123239#issuecomment-2727551097)* This PR aims at implementing a lint for implicit autoref of raw pointer dereference, it is based on #103735 with suggestion and improvements from https://github.com/rust-lang/rust/pull/103735#issuecomment-1370420305. The goal is to catch cases like this, where the user probably doesn't realise it just created a reference. ```rust pub struct Test { data: [u8], } pub fn test_len(t: *const Test) -> usize { unsafe { (*t).data.len() } // this calls <[T]>::len(&self) } ``` Since #103735 already went 2 times through T-lang, where they T-lang ended-up asking for a more restricted version (which is what this PR does), I would prefer this PR to be reviewed first before re-nominating it for T-lang. ---- Compared to the PR it is as based on, this PR adds 3 restrictions on the outer most expression, which must either be: 1. A deref followed by any non-deref place projection (that intermediate deref will typically be auto-inserted) 2. A method call annotated with `#[rustc_no_implicit_refs]`. 3. A deref followed by a `addr_of!` or `addr_of_mut!`. See bottom of post for details. There are several points that are not 100% clear to me when implementing the modifications: - ~~"4. Any number of automatically inserted deref/derefmut calls." I as never able to trigger this. Am I missing something?~~ Fixed - Are "index" and "field" enough? ---- cc `@JakobDegen` `@WaffleLapkin` r? `@RalfJung` try-job: dist-various-1 try-job: dist-various-2
2025-04-28Auto merge of #136316 - GrigorenkoPV:generic_atomic, r=Mark-Simulacrumbors-5/+6
Create `Atomic<T>` type alias (rebase) Rebase of #130543. Additional changes: - Switch from `allow` to `expect` for `private_bounds` on `AtomicPrimitive` - Unhide `AtomicPrimitive::AtomicInner` from docs, because rustdoc shows the definition `pub type Atomic<T> = <T as AtomicPrimitive>::AtomicInner;` and generated links for it. - `NonZero` did not have this issue, because they kept the new alias private before the direction was changed. - Use `Atomic<_>` in more places, including inside `Once`'s `Futex`. This is possible thanks to https://github.com/rust-lang/rust-clippy/pull/14125 The rest will either get moved back to #130543 or #130543 will be closed in favor of this instead. --- * ACP: https://github.com/rust-lang/libs-team/issues/443#event-14293381061 * Tracking issue: #130539
2025-04-28Rollup merge of #138939 - SabrinaJewson:arc-is-unique, r=tgross35Chris Denton-8/+61
Add `Arc::is_unique` Adds ```rs impl<T> Arc<T> { pub fn is_unique(this: &Self) -> bool; } ``` Tracking issue: #138938 ACP: https://github.com/rust-lang/libs-team/issues/560
2025-04-28Streamline the `format` macro.Nicholas Nethercote-2/+1
Removing the unnecessary local variable speeds up compilation a little.
2025-04-27Add `Arc::is_unique`SabrinaJewson-8/+61
2025-04-27Rollup merge of #137714 - DiuDiu777:doc-fix, r=tgross35Matthias Krüger-3/+12
Update safety documentation for `CString::from_ptr` and `str::from_boxed_utf8_unchecked` ## PR Description​ This PR addresses missing safety documentation for two APIs: ​**1. alloc::ffi::CStr::from_raw**​ - ​`Alias`: The pointer ​must not be aliased​ (accessed via other pointers) during the reconstructed CString's lifetime. - `Owning`: Calling this function twice on the same pointer and creating two objects with overlapping lifetimes, introduces two alive owners of the same memory. This may result in a double-free. - `Dangling`: The prior documentation required the pointer to originate from CString::into_raw, but this constraint is incomplete. A validly sourced pointer can also cause undefined behavior (UB) if it becomes dangling. A simple Poc for this situation: ``` use std::ffi::CString; use std::os::raw::c_char; fn create_dangling() -> *mut c_char { let local_ptr: *mut c_char = { let valid_data = CString::new("valid").unwrap(); valid_data.into_raw() }; unsafe { let _x = CString::from_raw(local_ptr); } local_ptr } fn main() { let dangling = create_dangling(); unsafe {let _y = CString::from_raw(dangling);} // Cause UB! } ``` ​**2. alloc::str::from_boxed_utf8_unchecked**​ - `ValidStr`: Bytes must contain a ​valid UTF-8 sequence.
2025-04-27Rollup merge of #137439 - clarfonthey:c-str-module, r=tgross35Matthias Krüger-1/+3
Stabilise `std::ffi::c_str` This finished FCP in #112134 but never actually got a stabilisation PR. Since the FCP in #120048 recently passed to add the `os_str` module, it would be nice to also merge this too, to ensure that both get added in the next version. Note: The added stability attributes which *somehow* were able to be omitted before (rustc bug?) were added based on the fact that they were added in 302551388b1942bb4216bb5a15d9d55cee3643a8, which ended up in 1.85.0. Closes: https://github.com/rust-lang/rust/issues/112134 r? libs-api
2025-04-27fix missing doc in CString::from_raw and str::from_boxed_utf8_uncheckedLemonJ-3/+12
2025-04-27use generic Atomic type where possibleChristopher Durham-5/+6
in core/alloc/std only for now, and ignoring test files Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-04-24Rollup merge of #140232 - nnethercote:rm-unnecessary-clones, r=SparrowLiiMatthias Krüger-1/+1
Remove unnecessary clones r? `@SparrowLii`
2025-04-24Remove some unnecessary clones.Nicholas Nethercote-1/+1
I found these by grepping for `&[a-z_\.]*\.clone()`, i.e. expressions like `&a.b.clone()`, which are sometimes unnecessary clones, and also looking at clones nearby to cases like that.
2025-04-21Rollup merge of #140118 - tamird:cstr-cleanup, r=joboetChris Denton-3/+3
{B,C}Str: minor cleanup (hopefully) uncontroversial bits extracted from #139994.
2025-04-20Implement lint against dangerous implicit autorefsUrgau-1/+1
2025-04-20Add `#[rustc_no_implicit_autorefs]` and apply it to std methodsUrgau-0/+2
2025-04-18Invert `<CString as Deref>::deref` and `CString::as_c_str`Tamir Duberstein-3/+3
This is consistent with the style of `ByteString`.
2025-04-16fix incorrect type in cstr `to_string_lossy()` docsLyndon Brown-1/+1
Restoring what it said prior to commit 67065fe in which it was changed incorrectly with no supporting explanation. Closes #139835.
2025-04-12Optimize `ToString` implementation for integersGuillaume Gomez-0/+48
2025-04-12Fix typo in documentationGenYuLi-1/+1
Correct the misspelling of "indentifier" to "identifier" in `library/alloc/src/fmt.rs`.
2025-04-10Auto merge of #139279 - BoxyUwU:bump-boostrap, r=jieyouxubors-35/+35
Bump boostrap compiler to new beta try-job: `*msvc*`
2025-04-09intra-doc linkBoxy-1/+1