about summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2025-07-15Rollup merge of #143907 - Kijewski:pr-inline-split_at_unchecked, ↵Samuel Tardieu-0/+1
r=Mark-Simulacrum core: make `str::split_at_unchecked()` inline This PR adds `#[inline]` to the method `str::split_at_unchecked()`. This is done for two reasons: 1. The method is tiny, e.g. on AMD-64 (<https://godbolt.org/z/ba68fdfxn>): ```asm movq %rdi, %rax subq %rcx, %rdx movq %rsi, (%rdi) addq %rcx, %rsi movq %rcx, 8(%rdi) movq %rsi, 16(%rdi) movq %rdx, 24(%rdi) retq ``` 2. More importantly, inlining the method enables further automatic optimizations. E.g. if you split at index 3, then in the compiler (rustc, llvm or both) knows that this code cannot fail, and the panicking path is omitted in the generated code: ```rust pub fn punctuation(i: &str) -> Result<(), ()> { const THREE_CHARS: &[[u8; 3]] = &[*b"<<=", *b">>=", *b"...", *b"..="]; if let Some((head, _)) = i.split_at_checked(3) && THREE_CHARS.contains(&head.as_bytes().try_into().unwrap()) { Ok(()) } else { Err(()) } } ``` <details> <summary>Without PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=0234de8158f467eebd73286f20d6e27a> ```asm playground::punctuation: subq $40, %rsp movq %rsi, %rdx movq %rdi, %rsi movb $1, %al cmpq $3, %rdx ja .LBB2_2 je .LBB2_3 .LBB2_11: addq $40, %rsp retq .LBB2_2: cmpb $-64, 3(%rsi) jl .LBB2_11 .LBB2_3: leaq 8(%rsp), %rdi movl $3, %ecx callq *core::str::<impl str>::split_at_unchecked@GOTPCREL(%rip) movq 8(%rsp), %rcx movb $1, %al testq %rcx, %rcx je .LBB2_11 cmpq $3, 16(%rsp) jne .LBB2_12 movzwl (%rcx), %edx movzbl 2(%rcx), %ecx shll $16, %ecx orl %edx, %ecx cmpl $4013115, %ecx jg .LBB2_8 cmpl $3026478, %ecx je .LBB2_10 cmpl $4009518, %ecx je .LBB2_10 jmp .LBB2_11 .LBB2_8: cmpl $4013630, %ecx je .LBB2_10 cmpl $4013116, %ecx jne .LBB2_11 .LBB2_10: xorl %eax, %eax addq $40, %rsp retq .LBB2_12: leaq .Lanon.d98a7fbb86d10a97c24516e267466134.2(%rip), %rdi leaq .Lanon.d98a7fbb86d10a97c24516e267466134.1(%rip), %rcx leaq .Lanon.d98a7fbb86d10a97c24516e267466134.6(%rip), %r8 leaq 7(%rsp), %rdx movl $43, %esi callq *core::result::unwrap_failed@GOTPCREL(%rip) ``` </details> <details> <summary>With PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=5d4058c79ce0f6cb1a434190427d2055> ```asm playground::punctuation: movb $1, %al cmpq $3, %rsi ja .LBB0_2 je .LBB0_3 .LBB0_9: retq .LBB0_2: cmpb $-64, 3(%rdi) jl .LBB0_9 .LBB0_3: movzwl (%rdi), %eax movzbl 2(%rdi), %ecx shll $16, %ecx orl %eax, %ecx movb $1, %al cmpl $4013115, %ecx jg .LBB0_6 cmpl $3026478, %ecx je .LBB0_8 cmpl $4009518, %ecx je .LBB0_8 jmp .LBB0_9 .LBB0_6: cmpl $4013630, %ecx je .LBB0_8 cmpl $4013116, %ecx jne .LBB0_9 .LBB0_8: xorl %eax, %eax retq ``` </details>
2025-07-15constify `Index` trait and its slice implsOli Scherer-13/+23
2025-07-15constify some methods using `SliceIndex`Oli Scherer-16/+25
2025-07-15constify `SliceIndex` traitOli Scherer-25/+53
2025-07-14Auto merge of #142885 - a1phyr:borrowed_cursor_to_buf, r=Mark-Simulacrumbors-0/+51
core: Add `BorrowedCursor::with_unfilled_buf` Implementation of https://github.com/rust-lang/libs-team/issues/367. This mainly adds `BorrowedCursor::with_unfilled_buf`, with enables using the unfilled part of a cursor as a `BorrowedBuf`. Note that unlike the ACP, `BorrowedCursor::unfilled_buf` was moved to a `From` conversion. This is more consistent with other ways of creating a `BorrowedBuf` and hides a bit this conversion that requires unsafe code to be used correctly. Cc rust-lang/rust#78485 rust-lang/rust#117693
2025-07-14update `cfg_select!` documentationFolkert de Vries-9/+7
and make internal terminology consistent Co-authored-by: Travis Cross <tc@traviscross.com>
2025-07-14Rollup merge of #143710 - joshtriplett:random-updates, r=joshtriplettSamuel Tardieu-40/+25
Updates to random number generation APIs Updates based on discussions about random number generation. - Add comment on `RandomSource::fill_bytes` about multiple calls, to allow efficient implementations for random sources that generate a word at a time. - Drop the `Random` trait in favor of `Distribution<T>`, which will let people make calls like random(1..=6), and which allows for future expansion to non-uniform distributions, as well as floating-point. (For now, this is only implemented for `RangeFull`, to get the interface in place. Subsequent PRs will implement it for other range types.)
2025-07-14Rollup merge of #143917 - theemathas:change-allocated-object-to-allocation, ↵Jakub Beránek-3/+3
r=oli-obk Change "allocated object" to "allocation". These seem like they were missed in <https://github.com/rust-lang/rust/pull/141224>
2025-07-14Rollup merge of #143875 - fee1-dead-contrib:push-zvqrmzrprpzt, r=compiler-errorsJakub Beránek-22/+22
update issue number for `const_trait_impl` r? project-const-traits cc rust-lang/rust#67792 rust-lang/rust#143874
2025-07-14Change "allocated object" to "allocation".Tim (Theemathas) Chirananthavat-3/+3
These seem like they were missed in <https://github.com/rust-lang/rust/pull/141224>
2025-07-13core: make `str::split_at_unchecked()` inlineRené Kijewski-0/+1
This PR adds `#[inline]` to the method `str::split_at_unchecked()`. This is done for two reasons: 1. The method is tiny, e.g. on AMD-64 (<https://godbolt.org/z/ba68fdfxn>): ```asm movq %rdi, %rax subq %rcx, %rdx movq %rsi, (%rdi) addq %rcx, %rsi movq %rcx, 8(%rdi) movq %rsi, 16(%rdi) movq %rdx, 24(%rdi) retq ``` 2. More importantly, inlining the method enables further automatic optimizations. E.g. if you split at index 3, then in the compiler (rustc, llvm or both) knows that this code cannot fail, and the panicking path is omitted in the generated code: ```rust pub fn punctuation(i: &str) -> Result<(), ()> { const THREE_CHARS: &[[u8; 3]] = &[*b"<<=", *b">>=", *b"...", *b"..="]; if let Some((head, _)) = i.split_at_checked(3) && THREE_CHARS.contains(&head.as_bytes().try_into().unwrap()) { Ok(()) } else { Err(()) } } ``` <details> <summary>Without PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=0234de8158f467eebd73286f20d6e27a> ```asm playground::punctuation: subq $40, %rsp movq %rsi, %rdx movq %rdi, %rsi movb $1, %al cmpq $3, %rdx ja .LBB2_2 je .LBB2_3 .LBB2_11: addq $40, %rsp retq .LBB2_2: cmpb $-64, 3(%rsi) jl .LBB2_11 .LBB2_3: leaq 8(%rsp), %rdi movl $3, %ecx callq *core::str::<impl str>::split_at_unchecked@GOTPCREL(%rip) movq 8(%rsp), %rcx movb $1, %al testq %rcx, %rcx je .LBB2_11 cmpq $3, 16(%rsp) jne .LBB2_12 movzwl (%rcx), %edx movzbl 2(%rcx), %ecx shll $16, %ecx orl %edx, %ecx cmpl $4013115, %ecx jg .LBB2_8 cmpl $3026478, %ecx je .LBB2_10 cmpl $4009518, %ecx je .LBB2_10 jmp .LBB2_11 .LBB2_8: cmpl $4013630, %ecx je .LBB2_10 cmpl $4013116, %ecx jne .LBB2_11 .LBB2_10: xorl %eax, %eax addq $40, %rsp retq .LBB2_12: leaq .Lanon.d98a7fbb86d10a97c24516e267466134.2(%rip), %rdi leaq .Lanon.d98a7fbb86d10a97c24516e267466134.1(%rip), %rcx leaq .Lanon.d98a7fbb86d10a97c24516e267466134.6(%rip), %r8 leaq 7(%rsp), %rdx movl $43, %esi callq *core::result::unwrap_failed@GOTPCREL(%rip) ``` </details> <details> <summary>With PR</summary> <https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=5d4058c79ce0f6cb1a434190427d2055> ```asm playground::punctuation: movb $1, %al cmpq $3, %rsi ja .LBB0_2 je .LBB0_3 .LBB0_9: retq .LBB0_2: cmpb $-64, 3(%rdi) jl .LBB0_9 .LBB0_3: movzwl (%rdi), %eax movzbl 2(%rdi), %ecx shll $16, %ecx orl %eax, %ecx movb $1, %al cmpl $4013115, %ecx jg .LBB0_6 cmpl $3026478, %ecx je .LBB0_8 cmpl $4009518, %ecx je .LBB0_8 jmp .LBB0_9 .LBB0_6: cmpl $4013630, %ecx je .LBB0_8 cmpl $4013116, %ecx jne .LBB0_9 .LBB0_8: xorl %eax, %eax retq ``` </details>
2025-07-13Auto merge of #143461 - folkertdev:cfg-select-builtin-macro, r=petrochenkovbors-21/+5
make `cfg_select` a builtin macro tracking issue: https://github.com/rust-lang/rust/issues/115585 This parses mostly the same as the `macro cfg_select` version, except: 1. wrapping in double brackets is no longer supported (or needed): `cfg_select {{ /* ... */ }}` is now rejected. 2. in an expression context, the rhs is no longer wrapped in a block, so that this now works: ```rust fn main() { println!(cfg_select! { unix => { "foo" } _ => { "bar" } }); } ``` 3. a single wildcard rule is now supported: `cfg_select { _ => 1 }` now works I've also added an error if none of the rules evaluate to true, and warnings for any arms that follow the `_` wildcard rule. cc `@traviscross` if I'm missing any feature that should/should not be included r? `@petrochenkov` for the macro logic details
2025-07-13update issue number for `const_trait_impl`Deadbeef-22/+22
2025-07-13Rollup merge of #143774 - oli-obk:const_from, r=fee1-deadMatthias Krüger-8/+21
constify `From` and `Into` tracking issue rust-lang/rust#143773 r? ``````@fee1-dead`````` I did not mark any impls elsewhere as `const`, those can happen on their own timeframe and don't need to be part of this MVP. But if there are some core ones you think should be in there I'll happily add them, just couldn't think of any
2025-07-13Rollup merge of #143554 - okaneco:const_slice_rotate, r=Amanieu,tgross35Matthias Krüger-9/+21
slice: Mark `rotate_left`, `rotate_right` unstably const Tracking issue rust-lang/rust#143812 - Add the const unstable `const_slice_rotate` feature - Mark `<[T]>::rotate_left` and `<[T]>::rotate_right` as const unstable The internal rotate functions use [`<*mut T>::replace`](https://doc.rust-lang.org/stable/core/primitive.pointer.html#method.replace) and [`ptr::swap_nonoverlapping`](https://doc.rust-lang.org/stable/core/ptr/fn.swap_nonoverlapping.html) which were const-stabilized in 1.88. Two changes were needed in the `rotate.rs` module to make these functions const: 1. A usage of `cmp::min` was replaced with a local function implementation of [`Ord::min`](https://doc.rust-lang.org/1.88.0/src/core/cmp.rs.html#1048-1053). 2. A `for start in 1..gcd` loop was changed to a while loop with an increment variable. This needs libs-api approval and cc-ing const-eval.
2025-07-13make `cfg_select` a builtin macroFolkert de Vries-21/+5
2025-07-13Rollup merge of #143803 - RalfJung:const-trait-tracking, r=compiler-errorsLeón Orell Valerian Liehr-18/+18
New tracking issues for const_ops and const_cmp Let's do a clean start with new tracking issues to avoid mixing things up with the previous constification. I assume the fact that the `PartialEq` *trait* and *impls* used different feature names was a mistake (the feature name on the impl is entirely irrelevant anyway). Part of https://github.com/rust-lang/rust/issues/143800, https://github.com/rust-lang/rust/issues/143802 r? ``@oli-obk``
2025-07-12Auto merge of #143766 - matthiaskrgr:rollup-0x7t69s, r=matthiaskrgrbors-2/+5
Rollup of 8 pull requests Successful merges: - rust-lang/rust#142391 (rust: library: Add `setsid` method to `CommandExt` trait) - rust-lang/rust#143302 (`tests/ui`: A New Order [27/N]) - rust-lang/rust#143303 (`tests/ui`: A New Order [28/28] FINAL PART) - rust-lang/rust#143568 (std: sys: net: uefi: tcp4: Add timeout support) - rust-lang/rust#143611 (Mention more APIs in `ParseIntError` docs) - rust-lang/rust#143661 (chore: Improve how the other suggestions message gets rendered) - rust-lang/rust#143708 (fix: Include frontmatter in -Zunpretty output ) - rust-lang/rust#143718 (Make UB transmutes really UB in LLVM) r? `@ghost` `@rustbot` modify labels: rollup try-job: i686-gnu-nopt-1 try-job: test-various
2025-07-12Fixed a core crate compilation failure when enabling the `optimize_for_size` ↵nazo6-4/+5
feature on some targets
2025-07-12Add Ref/RefMut try_map methodPavel Grigorenko-0/+93
2025-07-11slice: Mark `rotate_left`, `rotate_right` unstably constokaneco-9/+21
2025-07-11random: Provide a `Distribution<T>` traitJosh Triplett-40/+20
This will let people make calls like random(1..=6), and allows for future expansion to non-uniform distributions, as well as floating-point. For now, this is only implemented for `RangeFull`, to get the interface in place. Subsequent commits will implement it for other range types.
2025-07-11fix const_ops tracking issueRalf Jung-12/+12
2025-07-11fix PartialEq const feature name and const_cmp tracking issueRalf Jung-6/+6
2025-07-11constify `From` and `Into`Oli Scherer-8/+21
2025-07-11Rollup merge of #143611 - GrigorenkoPV:ParseIntError, r=tgross35Matthias Krüger-2/+5
Mention more APIs in `ParseIntError` docs Fixes rust-lang/rust#143602 r? `@lolbinarycat` `@rustbot` label +A-docs
2025-07-11Auto merge of #142911 - mejrs:unsized, r=compiler-errorsbors-1/+1
Remove support for dynamic allocas Followup to rust-lang/rust#141811
2025-07-10Rollup merge of #143640 - oli-obk:const-fn-traits, r=compiler-errorsMatthias Krüger-14/+22
Constify `Fn*` traits r? `@compiler-errors` `@fee1-dead` this should unlock a few things. A few `const_closures` tests have broken even more than before, but that feature is marked as incomplete anyway cc rust-lang/rust#67792
2025-07-10Rollup merge of #143652 - moulins:doc-unsize-trait-upcasting, r=compiler-errorsMatthias Krüger-0/+5
docs: document trait upcasting rules in `Unsize` trait The trait upcasting feature stabilized in 1.86 added new `Unsize` implementation, but this wasn't reflected in the trait's documentation.
2025-07-10Rollup merge of #136906 - chenyukang:yukang-fix-136741-closure-body, r=oli-obkMatthias Krüger-1/+1
Add checking for unnecessary delims in closure body Fixes #136741
2025-07-10core: add Peekable::next_if_mapkennytm-0/+102
2025-07-10Auto merge of #143721 - tgross35:rollup-sjdfp6r, r=tgross35bors-0/+32
Rollup of 9 pull requests Successful merges: - rust-lang/rust#141996 (Fix `proc_macro::Ident`'s handling of `$crate`) - rust-lang/rust#142950 (mbe: Rework diagnostics for metavariable expressions) - rust-lang/rust#143011 (Make lint `ambiguous_glob_imports` deny-by-default and report-in-deps) - rust-lang/rust#143265 (Mention as_chunks in the docs for chunks) - rust-lang/rust#143270 (tests/codegen/enum/enum-match.rs: accept negative range attribute) - rust-lang/rust#143298 (`tests/ui`: A New Order [23/N]) - rust-lang/rust#143396 (Move NaN tests to floats/mod.rs) - rust-lang/rust#143398 (tidy: add support for `--extra-checks=auto:` feature) - rust-lang/rust#143644 (Add triagebot stdarch mention ping) r? `@ghost` `@rustbot` modify labels: rollup
2025-07-10Rollup merge of #143265 - scottmcm:mention-as-chunks, r=ibraheemdevTrevor Gross-0/+32
Mention as_chunks in the docs for chunks and `as_rchunks_mut` from `rchunks_exact_mut`, and such. As suggested in https://github.com/rust-lang/rust/issues/76354#issuecomment-3015376438 (but does not close that issue).
2025-07-10Remove uncessary parens in closure body with unused lintyukang-1/+1
2025-07-09random: Add comment on `RandomSource::fill_bytes` about multiple callsJosh Triplett-0/+5
This allows efficient implementations for random sources that generate a word at a time.
2025-07-09Add opaque TypeId handles for CTFEOli Scherer-28/+71
2025-07-09core: Change `BorrowedCursor::written`'s originBenoît du Garreau-9/+3
This enable removing the `start` field, so `BorrowedCursor` fits in a single register. Because `written` is almost always used in difference with another call, this changes nothing else in practice.
2025-07-09core: Remove `BorrowedCursor::uninit_mut`Benoît du Garreau-10/+3
I assume that this method was there for completeness, but there is hardly any useful use of it: the buffer it gave was not always connected to the start of the cursor and its use required `unsafe` anyway to mark the bytes as initialized.
2025-07-09core: Remove `BorrowedCursor::init_ref` methodBenoît du Garreau-10/+0
This method was not really useful: at no point one would only need to read the initialized part of the cursor without mutating it.
2025-07-08Rollup merge of #143426 - hkBst:clippy-fix-indent-1, r=jhprattTrevor Gross-46/+47
clippy fix: indentation Fixes indentation of markdown comments.
2025-07-08docs: document trait upcasting rules in `Unsize` traitMoulins-0/+5
2025-07-08Remove `const_eval_select` hackOli Scherer-1/+1
2025-07-08Constify `Fn*` traitsOli Scherer-13/+21
2025-07-08Auto merge of #134628 - estebank:const-default, r=oli-obkbors-13/+28
Make `Default` const and add some `const Default` impls Full list of `impl const Default` types: - () - bool - char - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>
2025-07-08clippy fix: markdown indentation for indented items after line breakMarijn Schouten-16/+16
2025-07-08collect.rs: remove empty line after doc commentMarijn Schouten-8/+3
2025-07-08int_log10.rs: change top level doc comments to outerMarijn Schouten-2/+2
2025-07-08Auto merge of #143540 - yotamofek:pr/library/simplify-num-fmt, r=tgross35bors-22/+3
Simplify num formatting helpers Noticed `ilog10` was being open-coded when looking at this diff: https://github.com/rust-lang/rust/pull/143423/files/85d6768f4c437a0f3799234df20535ff65ee17c2..76d9775912ef3a7ee145053a5119538bf229d6e5#diff-6be9b44b52d946ccac652ddb7c98146a01b22ea0fc5737bc10db245a24796a45 That, and two other small cleanups 😁 (should probably go through perf just to make sure it doesn't regress formatting)
2025-07-08Rollup merge of #142098 - GuillaumeGomez:int_format_into, r=AmanieuMatthias Krüger-60/+294
Implement `int_format_into` feature I took over rust-lang/rust#138338 with `@madhav-madhusoodanan's` approval. Since https://github.com/rust-lang/rust/pull/136264, a lot of changes happened so I made use of them to reduce the number of changes. ACP approval: https://github.com/rust-lang/libs-team/issues/546#issuecomment-2707244569 ## Associated Issue - https://github.com/rust-lang/rust/issues/138215 r? `@hanna-kruppe`
2025-07-07Make `Default` const and add some `const Default` implsEsteban Küber-13/+28
Full list of `impl const Default` types: - () - bool - char - Cell - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>