about summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2025-01-11Add #[inline] to copy_from_sliceBen Kimock-0/+1
2025-01-11Auto merge of #135360 - RalfJung:structural-partial-eq, r=compiler-errorsbors-18/+13
update and clarify StructuralPartialEq docs This apparently hasn't been updated when we finalized the current const pattern matching behavior. Fixes https://github.com/rust-lang/rust/issues/92454 by providing rationale and context in the docs linked from that error message.
2025-01-11update and clarify StructuralPartialEq docsRalf Jung-18/+13
2025-01-11Rename `pos` to `position`Yuri Astrakhan-6/+6
2025-01-11Convert `struct FromBytesWithNulError` into enumYuri Astrakhan-31/+19
One of `CStr` constructors, `CStr::from_bytes_with_nul(bytes: &[u8])` handles 3 cases: 1. `bytes` has one NULL as the last value - creates CStr 2. `bytes` has no NULL - error 3. `bytes` has a NULL in some other position - error The 3rd case is error that may require lossy conversion, but the 2nd case can easily be handled by the user code. Unfortunately, this function returns an opaque `FromBytesWithNulError` error in both 2nd and 3rd case, so the user cannot detect just the 2nd case - having to re-implement the entire function and bring in the `memchr` dependency. In [this code](https://github.com/gquintard/varnish-rs/blob/f86d7a87683b08d2e634d63e77d9dc1d24ed4a13/varnish-sys/src/vcl/ws.rs#L158), my FFI code needs to copy user's `&[u8]` into a C-allocated memory blob in a NUL-terminated `CStr` format. My code must first validate if `&[u8]` has a trailing NUL (case 1), no NUL (adds one on the fly - case 2), or NUL in the middle (3rd case - error). I had to re-implement `from_bytes_with_nul` and add `memchr`dependency just to handle the 2nd case. This PR renames the former `kind` enum from `FromBytesWithNulErrorKind` to `FromBytesWithNulError`, and removes the original struct.
2025-01-11Rollup merge of #135347 - samueltardieu:push-qvyxtxsqyxyr, r=jhprattJacob Pratt-2/+1
Use `NonNull::without_provenance` within the standard library This API removes the need for several `unsafe` blocks, and leads to clearer code. It uses feature `nonnull_provenance` (#135243). Close #135343
2025-01-11Rollup merge of #135236 - scottmcm:more-mcp807-library-updates, r=ChrisDentonJacob Pratt-84/+215
Update a bunch of library types for MCP807 This greatly reduces the number of places that actually use the `rustc_layout_scalar_valid_range_*` attributes down to just 3: ``` library/core\src\ptr\non_null.rs 68:#[rustc_layout_scalar_valid_range_start(1)] library/core\src\num\niche_types.rs 19: #[rustc_layout_scalar_valid_range_start($low)] 20: #[rustc_layout_scalar_valid_range_end($high)] ``` Everything else -- PAL Nanoseconds, alloc's `Cap`, niched FDs, etc -- all just wrap those `niche_types` types. r? ghost
2025-01-11Implement `ByteStr` and `ByteString` typesJosh Triplett-0/+580
Approved ACP: https://github.com/rust-lang/libs-team/issues/502 Tracking issue: https://github.com/rust-lang/rust/issues/134915 These types represent human-readable strings that are conventionally, but not always, UTF-8. The `Debug` impl prints non-UTF-8 bytes using escape sequences, and the `Display` impl uses the Unicode replacement character. This is a minimal implementation of these types and associated trait impls. It does not add any helper methods to other types such as `[u8]` or `Vec<u8>`. I've omitted a few implementations of `AsRef`, `AsMut`, `Borrow`, `From`, and `PartialOrd`, when those would be the second implementation for a type (counting the `T` impl) or otherwise may cause inference failures. These impls are important, but we can attempt to add them later in standalone commits, and run them through crater. In addition to the `bstr` feature, I've added a `bstr_internals` feature for APIs provided by `core` for use by `alloc` but not currently intended for stabilization. This API and its implementation are based *heavily* on the `bstr` crate by Andrew Gallant (@BurntSushi).
2025-01-10Improve the safety documentation on new_uncheckedScott McMurray-1/+7
2025-01-10Use `NonNull::without_provenance` within the standard librarySamuel Tardieu-2/+1
This API removes the need for several `unsafe` blocks, and leads to clearer code.
2025-01-09Update a bunch of library types for MCP807Scott McMurray-84/+209
This greatly reduces the number of places that actually use the `rustc_layout_scalar_valid_range_*` attributes down to just 3: ``` library/core\src\ptr\non_null.rs 68:#[rustc_layout_scalar_valid_range_start(1)] library/core\src\num\niche_types.rs 19: #[rustc_layout_scalar_valid_range_start($low)] 20: #[rustc_layout_scalar_valid_range_end($high)] ``` Everything else -- PAL Nanoseconds, alloc's `Cap`, niched FDs, etc -- all just wrap those `niche_types` types.
2025-01-10Rollup merge of #134908 - madsmtm:ptr-from_ref-docs, r=ibraheemdevMatthias Krüger-2/+2
Fix `ptr::from_ref` documentation example comment The comment says that the expression involves no function call, but that was only true for the example above, the example here _does_ contain a function call. ``@rustbot`` label A-docs
2025-01-10Rollup merge of #134619 - hkBst:patch-7, r=jhprattMatthias Krüger-22/+20
Improve prose around `as_slice` example of IterMut I've removed the cryptic message about not being able to call `&mut self` methods while retaining a shared borrow of the iterator, such as `as_slice` produces. This is just normal borrowing rules and does not seem especially relevant here. I can whip up a replacement if someone thinks it has value.
2025-01-09Auto merge of #135268 - pietroalbini:pa-bump-stage0, r=Mark-Simulacrumbors-195/+84
Master bootstrap update Part of the release process. r? `@Mark-Simulacrum`
2025-01-08fmtPietro Albini-4/+1
2025-01-08update cfg(bootstrap)Pietro Albini-141/+33
2025-01-08update version placeholdersPietro Albini-59/+59
2025-01-08add missing provenance APIs on NonNullRalf Jung-3/+45
2025-01-06Rollup merge of #135139 - c410-f3r:8-years-rfc, r=jhprattJacob Pratt-8/+8
[generic_assert] Constify methods used by the formatting system cc #44838 Starts the "constification" of all the elements required to allow the execution of the formatting system in constant environments. ```rust const _: () = { panic!("{:?}", 1i32); }; ``` Further stuff is blocked by #133999.
2025-01-05[generic_assert] Constify methods used by the formatting systemCaio-8/+8
2025-01-05Rollup merge of #135121 - okaneco:const_slice_reverse, r=jhprattJacob Pratt-3/+5
Mark `slice::reverse` unstably const Tracking issue #135120 This is unblocked by the stabilization of `const_swap`
2025-01-05Mark `slice::reverse` unstably constokaneco-3/+5
2025-01-05Clarified the documentation on core::iter::from_fn and core::iter::successorsranger-ross-0/+3
2025-01-04do not in-place-iterate over flatmap/flattenThe 8472-32/+2
The implementation is unsound when a partially consumed iterator has some elements buffered in the front/back parts and cloning the Iterator removes the capacity from the backing vec::IntoIter.
2025-01-04Rollup merge of #133964 - joboet:select_unpredictable, r=tgross35Matthias Krüger-3/+51
core: implement `bool::select_unpredictable` Tracking issue: #133962 ACP: https://github.com/rust-lang/libs-team/issues/468
2025-01-01Rollup merge of #134985 - ↵Matthias Krüger-1/+1
mgsloan:remove-unnecessary-qualification-in-Ord-trait-docs, r=Noratrieb Remove qualification of `std::cmp::Ordering` in `Ord` doc
2025-01-01Stabilize const_slice_flattenDaniPopes-2/+2
Const-stabilizes `slice::as_flattened{,_mut}`: ```rust // core::slice impl<T, const N: usize> [[T; N]] { pub const fn as_flattened(&self) -> &[T]; pub const fn as_flattened_mut(&mut self) -> &mut [T]; } ``` Tracking issue: https://github.com/rust-lang/rust/issues/95629 Requires separate libs-api FCP, as per https://github.com/rust-lang/rust/issues/95629#issuecomment-2566546257. Closes https://github.com/rust-lang/rust/issues/95629.
2025-01-01Auto merge of #134969 - Marcondiro:master, r=jhpratt,programmerjakebors-7/+11
char to_digit: avoid unnecessary casts to u64 Hello, in the `char::to_digit` method there are a few `as u64` casts that are not strictly necessary. I assume that the reason behind these casts is to avoid possible overflows in the `+ 10` add. This PR removes the aforementioned casts, avoiding the overflow issue by slightly modifying the ASCII letter to int mapping. Thanks, Happy new year.
2024-12-31Improve `select_nth_unstable` documentation clarityMichael Sloan-42/+40
* Instead uses `before` and `after` variable names in the example where `greater` and `lesser` are flipped. * Uses `<=` and `>=` instead of "less than or equal to" and "greater than or equal to" to make the docs more concise. * General attempt to remove unnecessary words and be more precise. For example it seems slightly wrong to say "its final sorted position", since this implies there is only one sorted position for this element.
2024-12-31Remove qualification of `std::cmp::Ordering` in `Ord` docMichael Sloan-1/+1
2024-12-31Auto merge of #132195 - clarfonthey:bigint-mul, r=scottmcmbors-116/+265
Tidy up bigint multiplication methods This tidies up the library version of the bigint multiplication methods after the addition of the intrinsics in #133663. It follows [this summary](https://github.com/rust-lang/rust/issues/85532#issuecomment-2403442775) of what's desired for these methods. Note that, if `2H = N`, then `uH::MAX * uH::MAX + uH::MAX + uH::MAX` is `uN::MAX`, and that we can effectively add two "carry" values without overflowing. For ease of terminology, the "low-order" or "least significant" or "wrapping" half of multiplication will be called the low part, and the "high-order" or "most significant" or "overflowing" half of multiplication will be called the high part. In all cases, the return convention is `(low, high)` and left unchanged by this PR, to be litigated later. ## API Changes The original API: ```rust impl uN { // computes self * rhs pub const fn widening_mul(self, rhs: uN) -> (uN, uN); // computes self * rhs + carry pub const fn carrying_mul(self, rhs: uN, carry: uN) -> (uN, uN); } ``` The added API: ```rust impl uN { // computes self * rhs + carry1 + carry2 pub const fn carrying2_mul(self, rhs: uN, carry: uN, add: uN) -> (uN, uN); } impl iN { // note that the low part is unsigned pub const fn widening_mul(self, rhs: iN) -> (uN, iN); pub const fn carrying_mul(self, rhs: iN, carry: iN) -> (uN, iN); pub const fn carrying_mul_add(self, rhs: iN, carry: iN, add: iN) -> (uN, iN); } ``` Additionally, a naive implementation has been added for `u128` and `i128` since there are no double-wide types for those. Eventually, an intrinsic will be added to make these more efficient, but rather than doing this all at once, the library changes are added first. ## Justifications for API The unsigned parts are done to ensure consistency with overflowing addition: for a two's complement integer, you want to have unsigned overflow semantics for all parts of the integer except the highest one. This is because overflow for unsigned integers happens on the highest bit (from `MAX` to zero), whereas overflow for signed integers happens on the second highest bit (from `MAX` to `MIN`). Since the sign information only matters in the highest part, we use unsigned overflow for everything but that part. There is still discussion on the merits of signed bigint *addition* methods, since getting the behaviour right is very subtle, but at least for signed bigint *multiplication*, the sign of the operands does make a difference. So, it feels appropriate that at least until we've nailed down the final API, there should be an option to do signed versions of these methods. Additionally, while it's unclear whether we need all three versions of bigint multiplication (widening, carrying-1, and carrying-2), since it's possible to have up to two carries without overflow, there should at least be a method to allow that. We could potentially only offer the carry-2 method and expect that adding zero carries afterword will optimise correctly, but again, this can be litigated before stabilisation. ## Note on documentation While a lot of care was put into the documentation for the `widening_mul` and `carrying_mul` methods on unsigned integers, I have not taken this same care for `carrying_mul_add` or the signed versions. While I have updated the doc tests to be more appropriate, there will likely be many documentation changes done before stabilisation. ## Note on tests Alongside this change, I've added several tests to ensure that these methods work as expected. These are alongside the codegen tests for the intrinsics.
2024-12-31char to_digit: avoid unnecessary casts to u64Marcondiro-7/+11
2024-12-31Rollup merge of #134953 - DiuDiu777:unaligned-doc, r=RalfJungMatthias Krüger-4/+0
Fix doc for read&write unaligned in zst operation ### PR Description This PR addresses an inconsistency in the Rust documentation regarding `read_unaligned ` and `write_unaligned` on zero-sized types (ZSTs). The current documentation for [pointer validity](https://doc.rust-lang.org/nightly/std/ptr/index.html#safety) states that for zero-sized types (ZSTs), null pointers are valid: > For zero-sized types (ZSTs), every pointer is valid, including the null pointer. However, there is an inconsistency in the documentation for the unaligned read operation in the function [ptr::read_unaligned](https://doc.rust-lang.org/nightly/std/ptr/fn.read_unaligned.html)(as well as `write_unaligned`), which states: > Note that even if T has size 0, the pointer must be non-null. This change is also supported by [PR #134912](https://github.com/rust-lang/rust/pull/134912) > the _unaligned method docs should be fixed.
2024-12-31Rollup merge of #134930 - RalfJung:ptr-docs-valid-access, r=jhprattStuart Cook-2/+2
ptr docs: make it clear that we are talking only about memory accesses This should make it harder to take this sentence out of context and misunderstand it.
2024-12-31Rollup merge of #134927 - DaniPopes:const-as_flattened_mut, r=scottmcmStuart Cook-1/+2
Make slice::as_flattened_mut unstably const Tracking issue: https://github.com/rust-lang/rust/issues/95629 Unblocked by const_mut_refs being stabilized: https://github.com/rust-lang/rust/pull/129195
2024-12-31fix doc for read write unaligned in zst operationLemonJ-4/+0
2024-12-30Auto merge of #134757 - RalfJung:const_swap, r=scottmcmbors-11/+25
stabilize const_swap libs-api FCP passed in https://github.com/rust-lang/rust/issues/83163. However, I only just realized that this actually involves an intrinsic. The intrinsic could be implemented entirely with existing stable const functionality, but we choose to make it a primitive to be able to detect more UB. So nominating for `@rust-lang/lang` to make sure they are aware; I leave it up to them whether they want to FCP this. While at it I also renamed the intrinsic to make the "nonoverlapping" constraint more clear. Fixes #83163
2024-12-30ptr docs: make it clear that we are talking only about memory accessesRalf Jung-2/+2
2024-12-30Make slice::as_flattened_mut unstably constDaniPopes-1/+2
Tracking issue: https://github.com/rust-lang/rust/issues/95629 Unblocked by const_mut_refs being stabilized: https://github.com/rust-lang/rust/pull/129195
2024-12-30Fix ptr::from_ref documentation example commentMads Marquart-2/+2
The comment says that the expression involves no function call, but that was only true for the example above, the example here _does_ contain a function call.
2024-12-28Fix sentence fragment in `pin` module docsGeoffrey Thomas-1/+1
Looks like this was inadvertently dropped in 8241ca60. Restore the words from before that commit.
2024-12-27Tidy up bigint mul methodsltdk-116/+265
2024-12-27Implement `int_from_ascii` (#134821)Niklas Fiekas-89/+131
Provides unstable `T::from_ascii()` and `T::from_ascii_radix()` for integer types `T`, as drafted in tracking issue #134821. To deduplicate documentation without additional macros, implementations of `isize` and `usize` no longer delegate to equivalent integer types. After #132870 they are inlined anyway.
2024-12-27Rollup merge of #133663 - scottmcm:carrying_mul_add, r=AmanieuMatthias Krüger-135/+257
Add a compiler intrinsic to back `bigint_helper_methods` cc https://github.com/rust-lang/rust/issues/85532 This adds a new `carrying_mul_add` intrinsic, to implement `wide_mul` and `carrying_mul`. It has fallback MIR for all types -- including `u128`, which isn't currently supported on nightly -- so that it'll continue to work on all backends, including CTFE. Then it's overridden in `cg_llvm` to use wider intermediate types, including `i256` for `u128::carrying_mul`.
2024-12-27Override `carrying_mul_add` in cg_llvmScott McMurray-2/+2
2024-12-27Move `{widening, carrying}_mul` to an intrinsic with fallback MIRScott McMurray-135/+257
Including implementing it for `u128`, so it can be defined in `uint_impl!`. This way it works for all backends, including CTFE.
2024-12-27Rollup merge of #134606 - RalfJung:ptr-copy-docs, r=Mark-Simulacrum许杰友 Jieyou Xu (Joe)-5/+3
ptr::copy: fix docs for the overlapping case Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/549 As discussed in that issue, it doesn't make any sense for `copy` to read a byte via `src` after it was already written via `dst`. The entire point of this method is that is copies correctly even if they overlap, and that requires always reading any given location before writing it. Cc `@rust-lang/opsem`
2024-12-26Rollup merge of #134791 - notriddle:notriddle/inline-ffi-error-types, r=tgross35Jacob Pratt-2/+2
docs: inline `std::ffi::c_str` types to `std::ffi` Rustdoc has no way to show that an item is stable, but only at a different path. `std::ffi::c_str::NulError` is not stable, but `std::ffi::NulError` is. To avoid marking these types as unstable when someone just wants to follow a link from `CString`, inline them into their stable paths. Fixes #134702 r? `@tgross35`
2024-12-26Rollup merge of #134782 - wtlin1228:docs/iter-rposition, r=Mark-SimulacrumJacob Pratt-0/+1
Update Code Example for `Iterator::rposition` Added an additional assertion to the example to show the behavior of `iter.next_back` after using `iter.rposition`.
2024-12-26docs: inline `core::ffi::c_str` types to `core::ffi`Michael Howell-2/+2