about summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2024-02-14Rollup merge of #118890 - Amanieu:allocator-lifetime, r=Mark-SimulacrumOli Scherer-2/+8
Clarify the lifetimes of allocations returned by the `Allocator` trait The previous definition (accidentally) disallowed the implementation of stack-based allocators whose memory would become invalid once the lifetime of the allocator type ended. This also ensures the validity of the following blanket implementation: ```rust impl<A: Allocator> Allocator for &'_ A {} ```
2024-02-14Rollup merge of #116387 - kpreid:wake-doc, r=cuviperOli Scherer-3/+17
Additional doc links and explanation of `Wake`. This is intended to clarify: * That `Wake` exists and can be used instead of `RawWaker`. * How to construct a `Waker` when you are looking at `Wake` (which was previously only documented in the example).
2024-02-13Store core::str::CharSearcher::utf8_size as u8GnomedDev-11/+23
2024-02-13Add information about allocation lifetime to Allocator::allocateAmanieu d'Antras-0/+4
2024-02-13implement `Default` for `AsciiChar`joseLuís-1/+4
2024-02-12iterator.rs: remove "Basic usage" textTshepang Mbambo-40/+0
Only one example is given (for each method)
2024-02-12Support safe intrinsics with fallback bodiesOli Scherer-8/+4
Turn `is_val_statically_known` into such an intrinsic to demonstrate. It is perfectly safe to call after all.
2024-02-12Give const_deallocate a default bodyOli Scherer-14/+17
2024-02-12Teach llvm backend how to fall back to default bodiesOli Scherer-12/+16
2024-02-12Check signature of intrinsics with fallback bodiesOli Scherer-1/+1
2024-02-12Clarify the lifetimes of allocations returned by the `Allocator` traitAmanieu d'Antras-2/+4
The previous definition (accidentally) disallowed the implementation of stack-based allocators whose memory would become invalid once the lifetime of the allocator type ended. This also ensures the validity of the following blanket implementation: ```rust impl<A: Allocator> Allocator for &'_ A {} ```
2024-02-12Fix comment in core/src/str/validations.rsPizzasBear-1/+1
2024-02-12Implement intrinsics with fallback bodiesOli Scherer-69/+69
2024-02-11Rollup merge of #120888 - saethlin:unsafe-precondition-cleanup, r=RalfJungMatthias Krüger-38/+34
assert_unsafe_precondition cleanup I moved the polymorphic `is_nonoverlapping` into the `Cell` function that uses it and renamed `intrinsics::is_nonoverlapping_mono` to just `intrinsics::is_nonoverlapping`. We now also have some docs for `intrinsics::debug_assertions`. r? RalfJung
2024-02-11Rollup merge of #120880 - RalfJung:vtable-fnptr-partialeq, r=cuviperMatthias Krüger-0/+16
add note on comparing vtables / function pointers Fixes https://github.com/rust-lang/rust/issues/99388 Fixes https://github.com/rust-lang/rust/issues/117047
2024-02-11add comparison warning to RawWakerVTable as wellRalf Jung-0/+6
2024-02-11Auto merge of #120903 - matthiaskrgr:rollup-tmsuzth, r=matthiaskrgrbors-3/+51
Rollup of 9 pull requests Successful merges: - #119213 (simd intrinsics: add simd_shuffle_generic and other missing intrinsics) - #120272 (Suppress suggestions in derive macro) - #120773 (large_assignments: Allow moves into functions) - #120874 (Take empty `where` bounds into account when suggesting predicates) - #120882 (interpret/write_discriminant: when encoding niched variant, ensure the stored value matches) - #120883 (interpret: rename ReadExternStatic → ExternStatic) - #120890 (Adapt `llvm-has-rust-patches` validation to take `llvm-config` into account.) - #120895 (don't skip coercions for types with errors) - #120896 (Print kind of coroutine closure) r? `@ghost` `@rustbot` modify labels: rollup
2024-02-11Cleanup around the new assert_unsafe_preconditionBen Kimock-38/+34
Make the polymorphic is_nonoverlapping private Fix assert_unsafe_precondition doc typos Add docs for intrinsics::debug_assertions
2024-02-11Rollup merge of #120307 - djc:duration-constructors, r=Mark-SimulacrumMatthias Krüger-0/+120
core: add Duration constructors Add more `Duration` constructors. Tracking issue: #120301. These match similar convenience constructors available on both `chrono::Duration` and `time::Duration`. What's the best ordering for these with respect to the existing constructors?
2024-02-11Rollup merge of #119449 - Nilstrieb:library-clippy, r=cuviperMatthias Krüger-0/+12
Fix `clippy::correctness` in the library needs https://github.com/rust-lang/backtrace-rs/pull/579 to be complete for https://github.com/rust-lang/compiler-team/issues/709
2024-02-11Rollup merge of #119242 - BenWiederhake:dev-from-nanos, r=joshtriplettMatthias Krüger-0/+5
Suggest less bug-prone construction of Duration in docs std::time::Duration has a well-known quirk: Duration::as_nanos() returns u128 [1], but Duration::from_nanos() takes u64 [2]. So these methods cannot easily roundtrip [3]. It is not possible to simply accept u128 in from_nanos [4], because it requires breaking other API [5]. It seems to me that callers have basically only two options: 1. `Duration::from_nanos(d.as_nanos() as u64)`, which is the "obvious" and buggy approach. 2. `Duration::new(d.as_secs(), d.subsecs_nanos())`, which only becomes apparent after reading and digesting the entire Duration struct documentation. I suggest that the documentation of `from_nanos` is changed to make option 2 more easily discoverable. There are two major usecases for this: - "Weird math" operations that should not be supported directly by `Duration`, like squaring. - "Disconnected roundtrips", where the u128 value is passed through various other stack frames, and perhaps reconstructed into a Duration on a different machine. In both cases, it seems like a good idea to not tempt people into thinking "Eh, u64 is good enough, what could possibly go wrong!". That's why I want to add a note that points out the similarly-easy and *safe* way to reconstruct a Duration. [1] https://doc.rust-lang.org/stable/std/time/struct.Duration.html#method.as_nanos [2] https://doc.rust-lang.org/stable/std/time/struct.Duration.html#method.from_nanos [3] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fa6bab2b6b72f20c14b5243610ea1dde [4] https://github.com/rust-lang/rust/issues/103332 [5] https://github.com/rust-lang/rust/issues/51107#issuecomment-392353166
2024-02-11Rollup merge of #118307 - scottmcm:tuple-eq-simpler, r=joshtriplettMatthias Krüger-13/+1
Remove an unneeded helper from the tuple library code Thanks to https://github.com/rust-lang/rust/pull/107022, this is just what `==` does, so we don't need the helper here anymore.
2024-02-10Additional doc links and explanation of `Wake`.Kevin Reid-3/+17
This is intended to clarify: * That `Wake` exists and can be used instead of `RawWaker`. * How to construct a `Waker` when you are looking at `Wake` (which was previously only documented in the example).
2024-02-11Rollup merge of #119213 - RalfJung:simd_shuffle, r=workingjubileeMatthias Krüger-3/+51
simd intrinsics: add simd_shuffle_generic and other missing intrinsics Also tweak the simd_shuffle docs a bit. r? `@calebzulawski`
2024-02-10add note on comparing vtables / function pointersRalf Jung-0/+10
2024-02-10Rollup merge of #120823 - LegionMammal978:clarify-atomic-align, r=RalfJungMatthias Krüger-1/+10
Clarify that atomic and regular integers can differ in alignment The documentation for atomic integers says that they have the "same in-memory representation" as their underlying integers. This might be misconstrued as implying that they have the same layout. Therefore, clarify that atomic integers' alignment is equal to their size.
2024-02-10Rollup merge of #120764 - Alfriadox:master, r=m-ou-seMatthias Krüger-1/+14
Add documentation on `str::starts_with` Add documentation about a current footgun of `str::starts_with`
2024-02-10various docs tweaksRalf Jung-7/+7
2024-02-10simd_scatter: mention left-to-right orderRalf Jung-0/+3
2024-02-10add more missing simd intrinsicsRalf Jung-0/+32
2024-02-10simd intrinsics: add simd_shuffle_genericRalf Jung-3/+16
2024-02-10Stabilize slice_split_at_uncheckedDaniel Sedlak-7/+2
2024-02-10Auto merge of #120712 - compiler-errors:async-closures-harmonize, r=oli-obkbors-14/+37
Harmonize `AsyncFn` implementations, make async closures conditionally impl `Fn*` traits This PR implements several changes to the built-in and libcore-provided implementations of `Fn*` and `AsyncFn*` to address two problems: 1. async closures do not implement the `Fn*` family traits, leading to breakage: https://crater-reports.s3.amazonaws.com/pr-120361/index.html 2. *references* to async closures do not implement `AsyncFn*`, as a consequence of the existing blanket impls of the shape `AsyncFn for F where F: Fn, F::Output: Future`. In order to fix (1.), we implement `Fn` traits appropriately for async closures. It turns out that async closures can: * always implement `FnOnce`, meaning that they're drop-in compatible with `FnOnce`-bound combinators like `Option::map`. * conditionally implement `Fn`/`FnMut` if they have no captures, which means that existing usages of async closures should *probably* work without breakage (crater checking this: https://github.com/rust-lang/rust/pull/120712#issuecomment-1930587805). In order to fix (2.), we make all of the built-in callables implement `AsyncFn*` via built-in impls, and instead adjust the blanket impls for `AsyncFn*` provided by libcore to match the blanket impls for `Fn*`.
2024-02-09Change wordingVenus Xeon-Blonde-7/+6
2024-02-09Auto merge of #120852 - matthiaskrgr:rollup-01pr8gj, r=matthiaskrgrbors-7/+16
Rollup of 11 pull requests Successful merges: - #120351 (Implement SystemTime for UEFI) - #120354 (improve normalization of `Pointee::Metadata`) - #120776 (Move path implementations into `sys`) - #120790 (better error message on download CI LLVM failure) - #120806 (Clippy subtree update) - #120815 (Improve `Option::inspect` docs) - #120822 (Emit more specific diagnostics when enums fail to cast with `as`) - #120827 (Print image input file and checksum in CI only) - #120836 (hide impls if trait bound is proven from env) - #120844 (Build DebugInfo for async closures) - #120851 (Remove duplicate release note) r? `@ghost` `@rustbot` modify labels: rollup
2024-02-09Rollup merge of #120815 - camsteffen:inspect-docs, r=m-ou-seMatthias Krüger-7/+16
Improve `Option::inspect` docs * Refer to the function as "a function" instead of "the provided closure" since it is not necessarily a closure. * State that the original Option/Result is returned. * Adjust the example for `Option::inspect` to use chaining.
2024-02-09Auto merge of #120676 - Mark-Simulacrum:bootstrap-bump, r=clubby789bors-168/+40
Bump bootstrap compiler to just-built 1.77 beta https://forge.rust-lang.org/release/process.html#master-bootstrap-update-t-2-day-tuesday
2024-02-09Bump Unicode to version 15.1.0, regenerate tablesMarcondiro-6/+6
2024-02-09Improve Option::inspect docsCameron Steffen-7/+16
2024-02-09Auto merge of #120843 - matthiaskrgr:rollup-med37z5, r=matthiaskrgrbors-10/+13
Rollup of 8 pull requests Successful merges: - #113671 (Make privacy visitor use types more (instead of HIR)) - #120308 (core/time: avoid divisions in Duration::new) - #120693 (Invert diagnostic lints.) - #120704 (A drive-by rewrite of `give_region_a_name()`) - #120809 (Use `transmute_unchecked` in `NonZero::new`.) - #120817 (Fix more `ty::Error` ICEs in MIR passes) - #120828 (Fix `ErrorGuaranteed` unsoundness with stash/steal.) - #120831 (Startup objects disappearing from sysroot) r? `@ghost` `@rustbot` modify labels: rollup
2024-02-09Rollup merge of #120809 - reitermarkus:generic-nonzero-constructors, r=NilstriebMatthias Krüger-3/+1
Use `transmute_unchecked` in `NonZero::new`. Tracking issue: https://github.com/rust-lang/rust/issues/120257 See https://github.com/rust-lang/rust/pull/120521#discussion_r1482615129.
2024-02-09Rollup merge of #120308 - utkarshgupta137:duration-opt, r=m-ou-seMatthias Krüger-7/+12
core/time: avoid divisions in Duration::new In our (decently large) code base, we use `SystemTime::UNIX_EPOCH.elapsed()` in a lot of places & often in a loop or in the hot path. On [Unix](https://github.com/rust-lang/rust/blob/1.75.0/library/std/src/sys/unix/time.rs#L153-L162) at least, it seems we do calculations before hand to ensure that nanos is within the valid range, yet `Duration::new()` still checks it again, using 2 divisions. It seems like adding a branch can make this function 33% faster on ARM64 in the cases where nanos is already in the valid range & seems to have no effect in the other case. Benchmarks: M1 Pro (14-inch base model): ``` duration/current/checked time: [1.5945 ns 1.6167 ns 1.6407 ns] Found 5 outliers among 100 measurements (5.00%) 2 (2.00%) high mild 3 (3.00%) high severe duration/current/unchecked time: [1.5941 ns 1.6051 ns 1.6179 ns] Found 2 outliers among 100 measurements (2.00%) 1 (1.00%) high mild 1 (1.00%) high severe duration/branched/checked time: [1.1997 ns 1.2048 ns 1.2104 ns] Found 8 outliers among 100 measurements (8.00%) 4 (4.00%) high mild 4 (4.00%) high severe duration/branched/unchecked time: [1.5881 ns 1.5957 ns 1.6039 ns] Found 6 outliers among 100 measurements (6.00%) 3 (3.00%) high mild 3 (3.00%) high severe ``` EC2 c7gd.16xlarge (Graviton 3): ``` duration/current/checked time: [2.7996 ns 2.8000 ns 2.8003 ns] Found 5 outliers among 100 measurements (5.00%) 2 (2.00%) low severe 3 (3.00%) low mild duration/current/unchecked time: [2.9922 ns 2.9925 ns 2.9928 ns] Found 7 outliers among 100 measurements (7.00%) 4 (4.00%) low severe 1 (1.00%) low mild 2 (2.00%) high mild duration/branched/checked time: [2.0830 ns 2.0843 ns 2.0857 ns] Found 3 outliers among 100 measurements (3.00%) 1 (1.00%) low severe 1 (1.00%) low mild 1 (1.00%) high mild duration/branched/unchecked time: [2.9879 ns 2.9886 ns 2.9893 ns] Found 5 outliers among 100 measurements (5.00%) 3 (3.00%) low severe 2 (2.00%) low mild ``` EC2 r7iz.16xlarge (Intel Xeon Scalable-based (Sapphire Rapids)): ``` duration/current/checked time: [980.60 ps 980.79 ps 980.99 ps] Found 10 outliers among 100 measurements (10.00%) 4 (4.00%) low severe 2 (2.00%) low mild 3 (3.00%) high mild 1 (1.00%) high severe duration/current/unchecked time: [979.53 ps 979.74 ps 979.96 ps] Found 6 outliers among 100 measurements (6.00%) 2 (2.00%) low severe 1 (1.00%) low mild 2 (2.00%) high mild 1 (1.00%) high severe duration/branched/checked time: [938.72 ps 938.96 ps 939.22 ps] Found 4 outliers among 100 measurements (4.00%) 1 (1.00%) low mild 1 (1.00%) high mild 2 (2.00%) high severe duration/branched/unchecked time: [1.0103 ns 1.0110 ns 1.0118 ns] Found 10 outliers among 100 measurements (10.00%) 2 (2.00%) low mild 7 (7.00%) high mild 1 (1.00%) high severe ``` Bench code (ran using stable 1.75.0 & criterion latest 0.5.1): I couldn't find any benches for `Duration` in this repo, so I just copied the relevant types & recreated it. ```rust use criterion::{black_box, criterion_group, criterion_main, Criterion}; pub fn duration_bench(c: &mut Criterion) { const NANOS_PER_SEC: u32 = 1_000_000_000; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] struct Nanoseconds(u32); impl Default for Nanoseconds { #[inline] fn default() -> Self { // SAFETY: 0 is within the valid range unsafe { Nanoseconds(0) } } } #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] pub struct Duration { secs: u64, nanos: Nanoseconds, // Always 0 <= nanos < NANOS_PER_SEC } impl Duration { #[inline] pub const fn new_current(secs: u64, nanos: u32) -> Duration { let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { Some(secs) => secs, None => panic!("overflow in Duration::new"), }; let nanos = nanos % NANOS_PER_SEC; // SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range Duration { secs, nanos: unsafe { Nanoseconds(nanos) } } } #[inline] pub const fn new_branched(secs: u64, nanos: u32) -> Duration { if nanos < NANOS_PER_SEC { // SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range Duration { secs, nanos: unsafe { Nanoseconds(nanos) } } } else { let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) { Some(secs) => secs, None => panic!("overflow in Duration::new"), }; let nanos = nanos % NANOS_PER_SEC; // SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range Duration { secs, nanos: unsafe { Nanoseconds(nanos) } } } } } let mut group = c.benchmark_group("duration/current"); group.bench_function("checked", |b| { b.iter(|| black_box(Duration::new_current(black_box(1_000_000_000), black_box(1_000_000)))); }); group.bench_function("unchecked", |b| { b.iter(|| { black_box(Duration::new_current(black_box(1_000_000_000), black_box(2_000_000_000))) }); }); drop(group); let mut group = c.benchmark_group("duration/branched"); group.bench_function("checked", |b| { b.iter(|| { black_box(Duration::new_branched(black_box(1_000_000_000), black_box(1_000_000))) }); }); group.bench_function("unchecked", |b| { b.iter(|| { black_box(Duration::new_branched(black_box(1_000_000_000), black_box(2_000_000_000))) }); }); } criterion_group!(duration_benches, duration_bench); criterion_main!(duration_benches); ```
2024-02-08Clarify that atomic and regular integers can differ in alignmentLegionMammal978-1/+10
The documentation for atomic integers says that they have the "same in-memory representation" as their underlying integers. This might be misconstrued as implying that they have the same layout. Therefore, clarify that atomic integers' alignment is equal to their size.
2024-02-08Add and use Unique::as_non_null_ptrBen Kimock-4/+9
2024-02-08Make `NonZero::get` generic.Markus Reiter-20/+21
2024-02-08Use `transmute_unchecked` in `NonZero::new`.Markus Reiter-3/+1
2024-02-08Reduce use of NonNull::new_unchecked in library/Ben Kimock-14/+13
2024-02-08Remove a now-obviated debug_assert!Ben Kimock-1/+0
2024-02-08Rewrite assert_unsafe_precondition around the new intrinsicBen Kimock-59/+147
2024-02-08Add a new debug_assertions intrinsicBen Kimock-0/+11