about summary refs log tree commit diff
path: root/library/core
AgeCommit message (Collapse)AuthorLines
2024-01-22Rollup merge of #118714 - The-Ludwig:explain_ord_derive_enum_field, r=NilstriebMatthias Krüger-2/+4
Explanation that fields are being used when deriving `(Partial)Ord` on enums When deriving `std::cmp::Ord` or `std::cmp::PartialOrd` on enums, their fields are compared if the variants are equal. This means that the last assertion in the following snipped panics. ```rust use std::cmp::{PartialEq, Eq, PartialOrd, Ord}; #[derive(PartialEq, Eq, PartialOrd, Ord)] enum Sizes { Small(usize), Big(usize), } fn main() { let a = Sizes::Big(3); let b = Sizes::Big(5); let c = Sizes::Small(10); assert!( c < a); assert_eq!(a, c); } ``` This is more often expected behavior than not, and can be easily circumvented, as discussed in [this thread](https://users.rust-lang.org/t/how-to-sort-enum-variants/52291/4). But it is addressed nowhere in the documentation, yet. So I stumbled across this, as I personally did not expect fields being used in `PartialOrd`. I added the explanation to the documentation.
2024-01-21reviewMichal Nazarewicz-178/+20
2024-01-21Manually implement derived `NonZero` traits.Markus Reiter-8/+101
2024-01-21Auto merge of #119807 - Emilgardis:track_caller_from_impl_into, r=Nilstriebbors-0/+1
Add `#[track_caller]` to the "From implies Into" impl This pr implements what was mentioned in https://github.com/rust-lang/rust/issues/77474#issuecomment-1074480790 This follows from my URLO https://users.rust-lang.org/t/104497 ```rust #![allow(warnings)] fn main() { // Gives a good location let _: Result<(), Loc> = dbg!(Err::<(), _>(()).map_err(|e| e.into())); // still doesn't work, gives location of `FnOnce::call_once()` let _: Result<(), Loc> = dbg!(Err::<(), _>(()).map_err(Into::into)); } #[derive(Debug)] pub struct Loc { pub l: &'static std::panic::Location<'static>, } impl From<()> for Loc { #[track_caller] fn from(_: ()) -> Self { Loc { l: std::panic::Location::caller(), } } } ```
2024-01-21Fix `clippy::correctness` in the libraryNilstrieb-0/+12
2024-01-21Add `#[coverage(off)]` to closures introduced by `#[test]`/`#[bench]`Zalathar-2/+2
2024-01-21Rollup merge of #120045 - scottmcm:unhide-repeat-n, r=Mark-SimulacrumMatthias Krüger-2/+0
Un-hide `iter::repeat_n` ACP accepted in https://github.com/rust-lang/libs-team/issues/120#issuecomment-1894144403
2024-01-21Auto merge of #85528 - the8472:iter-markers, r=dtolnaybors-14/+133
Implement iterator specialization traits on more adapters This adds * `TrustedLen` to `Skip` and `StepBy` * `TrustedRandomAccess` to `Skip` * `InPlaceIterable` and `SourceIter` to `Copied` and `Cloned` The first two might improve performance in the compiler itself since `skip` is used in several places. Constellations that would exercise the last point are probably rare since it would require an owning iterator that has references as Items somewhere in its iterator pipeline. Improvements for `Skip`: ``` # old test iter::bench_skip_trusted_random_access ... bench: 8,335 ns/iter (+/- 90) # new test iter::bench_skip_trusted_random_access ... bench: 2,753 ns/iter (+/- 27) ```
2024-01-21chore: suggest wrapping in an `assert!()` insteadHTGAzureX1212-1/+1
This shortens the `#[must_use]` diagnostics displayed, in light of the [review comment](https://github.com/rust-lang/rust/pull/62431/files#r300819839) on when this was originally added.
2024-01-21Rollup merge of #119081 - jstasiak:is-ipv4-mapped, r=dtolnayNadrieril-1/+36
Add Ipv6Addr::is_ipv4_mapped This change consists of cherry-picking the content from the original PR[1], which got closed due to inactivity, and applying the following changes: * Resolving merge conflicts (obviously) * Linked to to_ipv4_mapped instead of to_ipv4 in the documentation (seems more appropriate) * Added the must_use and rustc_const_unstable attributes the original didn't have I think it's a reasonably useful method to have. [1] https://github.com/rust-lang/rust/pull/86490
2024-01-21Rollup merge of #118811 - EbbDrop:is-sorted-by-bool, r=Mark-SimulacrumNadrieril-24/+35
Use `bool` instead of `PartiolOrd` as return value of the comparison closure in `{slice,Iteraotr}::is_sorted_by` Changes the function signature of the closure given to `{slice,Iteraotr}::is_sorted_by` to return a `bool` instead of a `PartiolOrd` as suggested by the libs-api team here: https://github.com/rust-lang/rust/issues/53485#issuecomment-1766411980. This means these functions now return true if the closure returns true for all the pairs of values.
2024-01-21Rollup merge of #116090 - rmehri01:strict_integer_ops, r=m-ou-seNadrieril-15/+996
Implement strict integer operations that panic on overflow This PR implements the first part of the ACP for adding panic on overflow style arithmetic operations (https://github.com/rust-lang/libs-team/issues/270), mentioned in #116064. It adds the following operations on both signed and unsigned integers: - `strict_add` - `strict_sub` - `strict_mul` - `strict_div` - `strict_div_euclid` - `strict_rem` - `strict_rem_euclid` - `strict_neg` - `strict_shl` - `strict_shr` - `strict_pow` Additionally, signed integers have: - `strict_add_unsigned` - `strict_sub_unsigned` - `strict_abs` And unsigned integers have: - `strict_add_signed` The `div` and `rem` operations are the same as normal division and remainder but are added for completeness similar to the corresponding `wrapping_*` operations. I'm not sure if I missed any operations, I basically found them from the `wrapping_*` and `checked_*` operations on both integer types.
2024-01-20Auto merge of #111803 - scottmcm:simple-swap-alternative, r=Mark-Simulacrumbors-1/+1
Tweak the threshold for chunked swapping Thanks to `@AngelicosPhosphoros` for the tests here, which I copied from #98892. This is an experiment as a simple alternative to that PR that just tweaks the existing threshold, since that PR showed that 3×Align (like `String`) currently doesn't work as well as it could.
2024-01-20Use bool instead of PartiolOrd in is_sorted_byEbbDrop-24/+35
2024-01-20Spelling fixsunrosa-1/+1
"It's" expands to "it is". "Its" is the possessive form.
2024-01-20core: introduce split_at{,_mut}_checkedMichal Nazarewicz-28/+375
Introduce split_at_checked and split_at_mut_checked methods to slices types (including str) which are non-panicking versions of split_at and split_at_mut respectively. This is analogous to get method being non-panicking version of indexing.
2024-01-20doc: fix some doctests after rebaseTomás Vallotton-3/+3
2024-01-20refactor: make waker mandatory.Tomás Vallotton-81/+18
This also removes * impl From<&Context> for ContextBuilder * Context::try_waker() The from implementation is removed because now that wakers are always supported, there are less incentives to override the current context. Before, the incentive was to add Waker support to a reactor that didn't have any.
2024-01-20chore: make method order consistent with wakerTomás Vallotton-13/+13
2024-01-20docs: remove recommendations to use LocalWaker in stable API documentationTomás Vallotton-57/+46
2024-01-20fix: Apply suggestions from code reviewtvallotton-3/+2
Co-authored-by: Mark Rousskov <mark.simulacrum@gmail.com>
2024-01-20doc: update thread safety explanation for RawWakerVTable and RawWaker.Tomás Vallotton-19/+43
2024-01-20chore: add and !Sync impls for LocalWaker as a stability guarantee.Tomás Vallotton-0/+5
2024-01-20fix: change issue number of waker_getters from #87021 to #96992.Tomás Vallotton-1/+1
2024-01-20chore: fix ci failuresTomás Vallotton-3/+3
2024-01-20feat: impl AsRef<LocalWaker> for Waker.Tomás Vallotton-0/+8
2024-01-20chore: add tracking issue number to local waker featureTomás Vallotton-14/+14
2024-01-20fix: make LocalWake available in targets that don't support atomics by ↵Tomás Vallotton-2/+2
removing a #[cfg(target_has_atomic = ptr)]
2024-01-20feat: add try_waker and From<&mut Context> for ContextBuilder to allow the ↵Tomás Vallotton-12/+67
extention of contexts by futures
2024-01-20refactor: remove in favor of and to make the API infallible.Tomás Vallotton-52/+29
2024-01-20perf: move null check from local_wake() to build()Tomás Vallotton-9/+16
2024-01-20feat: add LocalWaker type, ContextBuilder type, and LocalWake trait.Tomás Vallotton-24/+339
2024-01-20Rollup merge of #118799 - GKFX:stabilize-simple-offsetof, r=wesleywiserMatthias Krüger-10/+8
Stabilize single-field offset_of This PR stabilizes offset_of for a single field. There has been some further discussion at https://github.com/rust-lang/rust/issues/106655 about whether this is advisable; I'm opening the PR anyway so that the code is available.
2024-01-20Rollup merge of #113142 - the8472:opt-cstr-display, r=Mark-SimulacrumMatthias Krüger-1/+105
optimize EscapeAscii's Display and CStr's Debug ``` old: ascii::bench_ascii_escape_display_mixed 17.97µs/iter +/- 204.00ns ascii::bench_ascii_escape_display_no_escape 545.00ns/iter +/- 6.00ns new: ascii::bench_ascii_escape_display_mixed 4.99µs/iter +/- 56.00ns ascii::bench_ascii_escape_display_no_escape 91.00ns/iter +/- 1.00ns ```
2024-01-20Rollup merge of #103730 - SOF3:nonzero-from-mut, r=Mark-Simulacrum,dtolnayMatthias Krüger-0/+33
Added NonZeroXxx::from_mut(_unchecked)? ACP: rust-lang/libs-team#129 Tracking issue: #106290
2024-01-19Tweak the threshold for chunked swappingAngelicosPhosphoros-1/+1
Thanks to 98892 for the tests I brought in here, as it demonstrated that 3×usize is currently suboptimal.
2024-01-19Assign tracking issue number for feature(nonzero_from_mut)SOFe-2/+2
2024-01-19Added assert_unsafe_precondition! check for NonZeroXxx::from_mut_uncheckedSOFe-13/+20
2024-01-19Added NonZeroXxx::from_mut(_unchecked)?SOFe-0/+26
2024-01-19Consolidate logic around resolving built-in coroutine trait implsMichael Goulet-0/+1
2024-01-19Remove feature(offset_of) from testsGeorge Bateman-0/+1
2024-01-19Stabilize simple offset_ofGeorge Bateman-10/+7
2024-01-19Clarify Panicking Behavior in Integer Division DocsNicholas Thompson-19/+8
2024-01-19Make `saturating_div` Docs Consistent with OthersNicholas Thompson-10/+8
2024-01-19Adjust Attributes of Integer Division MethodsNicholas Thompson-7/+18
2024-01-19Add new intrinsic `is_constant` and optimize `pow`Catherine Flores-34/+138
Fix overflow check Make MIRI choose the path randomly and rename the intrinsic Add back test Add miri test and make it operate on `ptr` Define `llvm.is.constant` for primitives Update MIRI comment and fix test in stage2 Add const eval test Clarify that both branches must have the same side effects guaranteed non guarantee use immediate type instead Co-Authored-By: Ralf Jung <post@ralfj.de>
2024-01-19Rollup merge of #119984 - kpreid:waker-noop, r=dtolnayMatthias Krüger-7/+13
Change return type of unstable `Waker::noop()` from `Waker` to `&Waker`. The advantage of this is that it does not need to be assigned to a variable to be used in a `Context` creation, which is the most common thing to want to do with a noop waker. It also avoids unnecessarily executing the dynamically dispatched drop function when the noop waker is dropped. If an owned noop waker is desired, it can be created by cloning, but the reverse is harder to do since it requires declaring a constant. Alternatively, both versions could be provided, like `futures::task::noop_waker()` and `futures::task::noop_waker_ref()`, but that seems to me to be API clutter for a very small benefit, whereas having the `&'static` reference available is a large reduction in boilerplate. [Previous discussion on the tracking issue starting here](https://github.com/rust-lang/rust/issues/98286#issuecomment-1862159766)
2024-01-19Rollup merge of #117561 - tgross35:split-array, r=scottmcmMatthias Krüger-260/+90
Stabilize `slice_first_last_chunk` This PR does a few different things based around stabilizing `slice_first_last_chunk`. They are split up so this PR can be by-commit reviewed, I can move parts to a separate PR if desired. This feature provides a very elegant API to extract arrays from either end of a slice, such as for parsing integers from binary data. ## Stabilize `slice_first_last_chunk` ACP: https://github.com/rust-lang/libs-team/issues/69 Implementation: https://github.com/rust-lang/rust/issues/90091 Tracking issue: https://github.com/rust-lang/rust/issues/111774 This stabilizes the functionality from https://github.com/rust-lang/rust/issues/111774: ```rust impl [T] { pub const fn first_chunk<const N: usize>(&self) -> Option<&[T; N]>; pub fn first_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>; pub const fn last_chunk<const N: usize>(&self) -> Option<&[T; N]>; pub fn last_chunk_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>; pub const fn split_first_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])>; pub fn split_first_chunk_mut<const N: usize>(&mut self) -> Option<(&mut [T; N], &mut [T])>; pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T], &[T; N])>; pub fn split_last_chunk_mut<const N: usize>(&mut self) -> Option<(&mut [T], &mut [T; N])>; } ``` Const stabilization is included for all non-mut methods, which are blocked on `const_mut_refs`. This change includes marking the trivial function `slice_split_at_unchecked` const-stable for internal use (but not fully stable). ## Remove `split_array` slice methods Tracking issue: https://github.com/rust-lang/rust/issues/90091 Implementation: https://github.com/rust-lang/rust/pull/83233#pullrequestreview-780315524 This PR also removes the following unstable methods from the `split_array` feature, https://github.com/rust-lang/rust/issues/90091: ```rust impl<T> [T] { pub fn split_array_ref<const N: usize>(&self) -> (&[T; N], &[T]); pub fn split_array_mut<const N: usize>(&mut self) -> (&mut [T; N], &mut [T]); pub fn rsplit_array_ref<const N: usize>(&self) -> (&[T], &[T; N]); pub fn rsplit_array_mut<const N: usize>(&mut self) -> (&mut [T], &mut [T; N]); } ``` This is done because discussion at #90091 and its implementation PR indicate a strong preference for nonpanicking APIs that return `Option`. The only difference between functions under the `split_array` and `slice_first_last_chunk` features is `Option` vs. panic, so remove the duplicates as part of this stabilization. This does not affect the array methods from `split_array`. We will want to revisit these once `generic_const_exprs` is further along. ## Reverse order of return tuple for `split_last_chunk{,_mut}` An unresolved question for #111774 is whether to return `(preceding_slice, last_chunk)` (`(&[T], &[T; N])`) or the reverse (`(&[T; N], &[T])`), from `split_last_chunk` and `split_last_chunk_mut`. It is currently implemented as `(last_chunk, preceding_slice)` which matches `split_last -> (&T, &[T])`. The first commit changes these to `(&[T], &[T; N])` for these reasons: - More consistent with other splitting methods that return multiple values: `str::rsplit_once`, `slice::split_at{,_mut}`, `slice::align_to` all return tuples with the items in order - More intuitive (arguably opinion, but it is consistent with other language elements like pattern matching `let [a, b, rest @ ..] ...` - If we ever added a varidic way to obtain multiple chunks, it would likely return something in order: `.split_many_last::<(2, 4)>() -> (&[T], &[T; 2], &[T; 4])` - It is the ordering used in the `rsplit_array` methods I think the inconsistency with `split_last` could be acceptable in this case, since for `split_last` the scalar `&T` doesn't have any internal order to maintain with the other items. ## Unresolved questions Do we want to reserve the same names on `[u8; N]` to avoid inference confusion? https://github.com/rust-lang/rust/pull/117561#issuecomment-1793388647 --- `slice_first_last_chunk` has only been around since early 2023, but `split_array` has been around since 2021. `@rustbot` label -T-libs +T-libs-api -T-libs +needs-fcp cc `@rust-lang/wg-const-eval,` `@scottmcm` who raised this topic, `@clarfonthey` implementer of `slice_first_last_chunk` `@jethrogb` implementer of `split_array` Zulip discussion: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Stabilizing.20array-from-slice.20*something*.3F Fixes: #111774
2024-01-19Rollup merge of #119907 - asquared31415:fn_trait_docs, r=NilstriebMatthias Krüger-8/+2
Update `fn()` trait implementation docs Fixes #119903 This was FCP'd and approved for the 1.70.0 release, this is just a docs update to match that change.
2024-01-19Rollup merge of #119138 - ↵Matthias Krüger-4/+9
AngelicosPhosphoros:use_proper_atomics_in_spinlock_example, r=Nilstrieb Docs: Use non-SeqCst in module example of atomics I done this for this reasons: 1. The example now shows that there is more Orderings than just SeqCst. 2. People who would copy from example would now have more suitable orderings for the job. 3. SeqCst is both much harder to reason about and not needed in most situations. IMHO, we should encourage people to think and use memory orderings that is suitable to task instead of blindly defaulting to SeqCst. r? `@m-ou-se`