about summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2022-03-01Temporarily make `CStr` not a link in the `c_char` docsJosh Triplett-2/+1
When CStr moves to core with an alias in std, this can link to `crate::ffi::CStr`. However, linking in the reverse direction (from core to std) requires a relative path, and that path can't work from both core::ffi and std::os::raw (different number of `../` traversals required).
2022-03-01Provide C FFI types via core::ffi, not just in stdJosh Triplett-15/+231
The ability to interoperate with C code via FFI is not limited to crates using std; this allows using these types without std. The existing types in `std::os::raw` become type aliases for the ones in `core::ffi`. This uses type aliases rather than re-exports, to allow the std types to remain stable while the core types are unstable. This also moves the currently unstable `NonZero_` variants and `c_size_t`/`c_ssize_t`/`c_ptrdiff_t` types to `core::ffi`, while leaving them unstable.
2022-03-01Add a copy of cfg_if to core's internal_macros.rsJosh Triplett-0/+93
core can't depend on external crates the way std can. Rather than revert usage of cfg_if, add a copy of it to core. This does not export our copy, even unstably; such a change could occur in a later commit.
2022-03-01Rollup merge of #94384 - cuviper:atomic-slice, r=dtolnayDylan DPC-0/+94
Add Atomic*::from_mut_slice Tracking issue #76314 for `from_mut` has a question about the possibility of `from_mut_slice`, and I found a real case for it. A user in the forum had a parallelism problem that could be solved by open-indexing updates to a vector of atomics, but they didn't want to affect the other code using that vector. Using `from_mut_slice`, they could borrow that data as atomics just long enough for their parallel loop. ref: https://users.rust-lang.org/t/sharing-vector-with-rayon-par-iter-correctly/72022
2022-02-28Rollup merge of #89793 - ibraheemdev:from_ptr_range, r=m-ou-seMatthias Krüger-0/+114
Add `slice::{from_ptr_range, from_mut_ptr_range} ` Adds `slice::{from_ptr_range, from_mut_ptr_range}` as counterparts to `slice::{as_ptr_range, as_mut_ptr_range}`.
2022-02-27add `slice::{from_ptr_range, from_mut_ptr_range}`Ibraheem Ahmed-0/+114
2022-02-26For MIRI, cfg out the swap logic from 94212Scott McMurray-13/+37
2022-02-26Auto merge of #94385 - matthiaskrgr:rollup-4pwegqk, r=matthiaskrgrbors-6/+6
Rollup of 5 pull requests Successful merges: - #93603 (Populate liveness facts when calling `get_body_with_borrowck_facts` without `-Z polonius`) - #93870 (Fix switch on discriminant detection in a presence of coverage counters) - #94355 (Add one more case to avoid ICE) - #94363 (Remove needless borrows from core::fmt) - #94377 (`check_used` should only look at actual `used` attributes) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2022-02-26Rollup merge of #94363 - aDotInTheVoid:fmt-needless-borrows, r=scottmcmMatthias Krüger-6/+6
Remove needless borrows from core::fmt
2022-02-25Auto merge of #94342 - ibraheemdev:swap-regression, r=Dylan-DPCbors-5/+11
Revert implementation of `slice::swap` Due to the perf regressions noticed here, possible due to inlining? https://github.com/rust-lang/rust/pull/88540#issuecomment-944344343 r? `@kennytm`
2022-02-25Add Atomic*::from_mut_sliceJosh Stone-0/+94
2022-02-25Remove needless borrows from core::fmtNixon Enraght-Moony-6/+6
2022-02-25Re-add track_caller to panic_no_unwind in bootstrapMark Rousskov-0/+3
This function was updated in a recent PR (92911) to be called without the caller information passed in, but the function signature itself was not altered with cfg_attr at the time.
2022-02-25Switch bootstrap cfgsMark Rousskov-23/+1
2022-02-24revert implementation of `slice::swap`Ibraheem Ahmed-5/+11
2022-02-24Rollup merge of #94212 - scottmcm:swapper, r=dtolnayDylan DPC-93/+84
Stop manually SIMDing in `swap_nonoverlapping` Like I previously did for `reverse` (#90821), this leaves it to LLVM to pick how to vectorize it, since it can know better the chunk size to use, compared to the "32 bytes always" approach we currently have. A variety of codegen tests are included to confirm that the various cases are still being vectorized. It does still need logic to type-erase in some cases, though, as while LLVM is now smart enough to vectorize over slices of things like `[u8; 4]`, it fails to do so over slices of `[u8; 3]`. As a bonus, this change also means one no longer gets the spurious `memcpy`(s?) at the end up swapping a slice of `__m256`s: <https://rust.godbolt.org/z/joofr4v8Y> <details> <summary>ASM for this example</summary> ## Before (from godbolt) note the `push`/`pop`s and `memcpy` ```x86 swap_m256_slice: push r15 push r14 push r13 push r12 push rbx sub rsp, 32 cmp rsi, rcx jne .LBB0_6 mov r14, rsi shl r14, 5 je .LBB0_6 mov r15, rdx mov rbx, rdi xor eax, eax .LBB0_3: mov rcx, rax vmovaps ymm0, ymmword ptr [rbx + rax] vmovaps ymm1, ymmword ptr [r15 + rax] vmovaps ymmword ptr [rbx + rax], ymm1 vmovaps ymmword ptr [r15 + rax], ymm0 add rax, 32 add rcx, 64 cmp rcx, r14 jbe .LBB0_3 sub r14, rax jbe .LBB0_6 add rbx, rax add r15, rax mov r12, rsp mov r13, qword ptr [rip + memcpy@GOTPCREL] mov rdi, r12 mov rsi, rbx mov rdx, r14 vzeroupper call r13 mov rdi, rbx mov rsi, r15 mov rdx, r14 call r13 mov rdi, r15 mov rsi, r12 mov rdx, r14 call r13 .LBB0_6: add rsp, 32 pop rbx pop r12 pop r13 pop r14 pop r15 vzeroupper ret ``` ## After (from my machine) Note no `rsp` manipulation, sorry for different ASM syntax ```x86 swap_m256_slice: cmpq %r9, %rdx jne .LBB1_6 testq %rdx, %rdx je .LBB1_6 cmpq $1, %rdx jne .LBB1_7 xorl %r10d, %r10d jmp .LBB1_4 .LBB1_7: movq %rdx, %r9 andq $-2, %r9 movl $32, %eax xorl %r10d, %r10d .p2align 4, 0x90 .LBB1_8: vmovaps -32(%rcx,%rax), %ymm0 vmovaps -32(%r8,%rax), %ymm1 vmovaps %ymm1, -32(%rcx,%rax) vmovaps %ymm0, -32(%r8,%rax) vmovaps (%rcx,%rax), %ymm0 vmovaps (%r8,%rax), %ymm1 vmovaps %ymm1, (%rcx,%rax) vmovaps %ymm0, (%r8,%rax) addq $2, %r10 addq $64, %rax cmpq %r10, %r9 jne .LBB1_8 .LBB1_4: testb $1, %dl je .LBB1_6 shlq $5, %r10 vmovaps (%rcx,%r10), %ymm0 vmovaps (%r8,%r10), %ymm1 vmovaps %ymm1, (%rcx,%r10) vmovaps %ymm0, (%r8,%r10) .LBB1_6: vzeroupper retq ``` </details> This does all its copying operations as either the original type or as `MaybeUninit`s, so as far as I know there should be no potential abstract machine issues with reading padding bytes as integers. <details> <summary>Perf is essentially unchanged</summary> Though perhaps with more target features this would help more, if it could pick bigger chunks ## Before ``` running 10 tests test slice::swap_with_slice_4x_usize_30 ... bench: 894 ns/iter (+/- 11) test slice::swap_with_slice_4x_usize_3000 ... bench: 99,476 ns/iter (+/- 2,784) test slice::swap_with_slice_5x_usize_30 ... bench: 1,257 ns/iter (+/- 7) test slice::swap_with_slice_5x_usize_3000 ... bench: 139,922 ns/iter (+/- 959) test slice::swap_with_slice_rgb_30 ... bench: 328 ns/iter (+/- 27) test slice::swap_with_slice_rgb_3000 ... bench: 16,215 ns/iter (+/- 176) test slice::swap_with_slice_u8_30 ... bench: 312 ns/iter (+/- 9) test slice::swap_with_slice_u8_3000 ... bench: 5,401 ns/iter (+/- 123) test slice::swap_with_slice_usize_30 ... bench: 368 ns/iter (+/- 3) test slice::swap_with_slice_usize_3000 ... bench: 28,472 ns/iter (+/- 3,913) ``` ## After ``` running 10 tests test slice::swap_with_slice_4x_usize_30 ... bench: 868 ns/iter (+/- 36) test slice::swap_with_slice_4x_usize_3000 ... bench: 99,642 ns/iter (+/- 1,507) test slice::swap_with_slice_5x_usize_30 ... bench: 1,194 ns/iter (+/- 11) test slice::swap_with_slice_5x_usize_3000 ... bench: 139,761 ns/iter (+/- 5,018) test slice::swap_with_slice_rgb_30 ... bench: 324 ns/iter (+/- 6) test slice::swap_with_slice_rgb_3000 ... bench: 15,962 ns/iter (+/- 287) test slice::swap_with_slice_u8_30 ... bench: 281 ns/iter (+/- 5) test slice::swap_with_slice_u8_3000 ... bench: 5,324 ns/iter (+/- 40) test slice::swap_with_slice_usize_30 ... bench: 275 ns/iter (+/- 5) test slice::swap_with_slice_usize_3000 ... bench: 28,277 ns/iter (+/- 277) ``` </detail>
2022-02-24Rollup merge of #94300 - WaffleLapkin:patch-4, r=scottmcmMatthias Krüger-1/+1
Fix a typo in documentation of `array::IntoIter::new_unchecked` 🌸
2022-02-24Rollup merge of #94283 - hellow554:stable_flow_control, r=Dylan-DPCMatthias Krüger-2/+0
remove feature gate in control_flow examples Stabilization was done in https://github.com/rust-lang/rust/pull/91091, but the two examples weren't updated accordingly. Probably too late to put it into stable, but it should be in the next release :)
2022-02-23Fix a typo in documentation of `array::IntoIter::new_unchecked`Waffle Maybe-1/+1
2022-02-23Rollup merge of #94240 - compiler-errors:pathbuf-display, r=lcnrMatthias Krüger-1/+1
Suggest calling .display() on `PathBuf` too Fixes #94210
2022-02-23Rollup merge of #94128 - mqy:master, r=Dylan-DPCMatthias Krüger-3/+3
rustdoc: several minor fixes ``@rustbot`` label A-docs
2022-02-23remove feature gate in control_flow examplesMarcel Hellwig-2/+0
2022-02-21Suggest calling .display() on PathBuf tooMichael Goulet-1/+1
2022-02-21Rollup merge of #94186 - ehuss:pin-stable-1.61, r=m-ou-seMatthias Krüger-2/+2
Update pin_static_ref stabilization version. #93580 slipped into 1.61 cc `@m-ou-se`
2022-02-21Rollup merge of #91192 - r00ster91:futuredocs, r=GuillaumeGomezMatthias Krüger-2/+10
Some improvements to the async docs The goal here is to make the docs overall a little bit more comprehensive and add more links between the things. One thing that's not working yet is the links to the keywords. Somehow I couldn't get them to work. r? ````@GuillaumeGomez```` do you know how I could get the keyword links to work?
2022-02-21Stop manually SIMDing in swap_nonoverlappingScott McMurray-93/+84
Like I previously did for `reverse`, this leaves it to LLVM to pick how to vectorize it, since it can know better the chunk size to use, compared to the "32 bytes always" approach we currently have. It does still need logic to type-erase where appropriate, though, as while LLVM is now smart enough to vectorize over slices of things like `[u8; 4]`, it fails to do so over slices of `[u8; 3]`. As a bonus, this also means one no longer gets the spurious `memcpy`(s?) at the end up swapping a slice of `__m256`s: <https://rust.godbolt.org/z/joofr4v8Y>
2022-02-20Update pin_static_ref stabilization version.Eric Huss-2/+2
2022-02-20Auto merge of #94174 - matthiaskrgr:rollup-snyrlhy, r=matthiaskrgrbors-5/+94
Rollup of 14 pull requests Successful merges: - #93580 (Stabilize pin_static_ref.) - #93639 (Release notes for 1.59) - #93686 (core: Implement ASCII trim functions on byte slices) - #94002 (rustdoc: Avoid duplicating macros in sidebar) - #94019 (removing architecture requirements for RustyHermit) - #94023 (adapt static-nobundle test to use llvm-nm) - #94091 (Fix rustdoc const computed value) - #94093 (Fix pretty printing of enums without variants) - #94097 (Add module-level docs for `rustc_middle::query`) - #94112 (Optimize char_try_from_u32) - #94113 (document rustc_middle::mir::Field) - #94122 (Fix miniz_oxide types showing up in std docs) - #94142 (rustc_typeck: adopt let else in more places) - #94146 (Adopt let else in more places) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2022-02-20Rollup merge of #94112 - digama0:patch-3, r=scottmcmMatthias Krüger-3/+14
Optimize char_try_from_u32 The optimization was proposed by ```````@falk-hueffner``````` in https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Micro-optimizing.20char.3A.3Afrom_u32/near/272146171, and I simplified it a bit and added an explanation of why the optimization is correct. The generated code is 2 instructions shorter and uses 2 registers instead of 4 on x86.
2022-02-20Rollup merge of #93686 - dbrgn:trim-on-byte-slices, r=joshtriplettMatthias Krüger-0/+78
core: Implement ASCII trim functions on byte slices Hi ````````@rust-lang/libs!```````` This is a feature that I wished for when implementing serial protocols with microcontrollers. Often these protocols may contain leading or trailing whitespace, which needs to be removed. Because oftentimes drivers will operate on the byte level, decoding to unicode and checking for unicode whitespace is unnecessary overhead. This PR adds three new methods to byte slices: - `trim_ascii_start` - `trim_ascii_end` - `trim_ascii` I did not find any pre-existing discussions about this, which surprises me a bit. Maybe I'm missing something, and this functionality is already possible through other means? There's https://github.com/rust-lang/rfcs/issues/2547 ("Trim methods on slices"), but that has a different purpose. As per the [std dev guide](https://std-dev-guide.rust-lang.org/feature-lifecycle/new-unstable-features.html), this is a proposed implementation without any issue / RFC. If this is the wrong process, please let me know. However, I thought discussing code is easier than discussing a mere idea, and hacking on the stdlib was fun. Tracking issue: https://github.com/rust-lang/rust/issues/94035
2022-02-20Rollup merge of #93580 - m-ou-se:stabilize-pin-static-ref, r=scottmcmMatthias Krüger-2/+2
Stabilize pin_static_ref. FCP finished here: https://github.com/rust-lang/rust/issues/78186#issuecomment-1024987221 Closes #78186
2022-02-19Auto merge of #92911 - nbdd0121:unwind, r=Amanieubors-2/+1
Guard against unwinding in cleanup code Currently the only safe guard we have against double unwind is the panic count (which is local to Rust). When double unwinds indeed happen (e.g. C++ exception + Rust panic, or two C++ exceptions), then the second unwind actually goes through and the first unwind is leaked. This can cause UB. cc rust-lang/project-ffi-unwind#6 E.g. given the following C++ code: ```c++ extern "C" void foo() { throw "A"; } extern "C" void execute(void (*fn)()) { try { fn(); } catch(...) { } } ``` This program is well-defined to terminate: ```c++ struct dtor { ~dtor() noexcept(false) { foo(); } }; void a() { dtor a; dtor b; } int main() { execute(a); return 0; } ``` But this Rust code doesn't catch the double unwind: ```rust extern "C-unwind" { fn foo(); fn execute(f: unsafe extern "C-unwind" fn()); } struct Dtor; impl Drop for Dtor { fn drop(&mut self) { unsafe { foo(); } } } extern "C-unwind" fn a() { let _a = Dtor; let _b = Dtor; } fn main() { unsafe { execute(a) }; } ``` To address this issue, this PR adds an unwind edge to an abort block, so that the Rust example aborts. This is similar to how clang guards against double unwind (except clang calls terminate per C++ spec and we abort). The cost should be very small; it's an additional trap instruction (well, two for now, since we use TrapUnreachable, but that's a different issue) for each function with landing pads; if LLVM gains support to encode "abort/terminate" info directly in LSDA like GCC does, then it'll be free. It's an additional basic block though so compile time may be worse, so I'd like a perf run. r? `@ghost` `@rustbot` label: F-c_unwind
2022-02-19Some improvements to the async docsr00ster91-2/+10
2022-02-19rustdoc: several minor fixesmqy-3/+3
2022-02-18Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahcMatthias Krüger-77/+76
Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator` Following amendments in https://github.com/rust-lang/rfcs/pull/3208/. cc #79024 cc ``@yoshuawuyts`` ``@joshtriplett``
2022-02-17fixMario Carneiro-2/+0
2022-02-17Optimize char_try_from_u32Mario Carneiro-1/+14
The optimization was proposed by @falk-hueffner in https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Micro-optimizing.20char.3A.3Afrom_u32/near/272146171, and I simplified it a bit and added an explanation of why the optimization is correct.
2022-02-17Rollup merge of #94041 - a-lafrance:try-collect, r=scottmcmMatthias Krüger-0/+82
Add a `try_collect()` helper method to `Iterator` Implement `Iterator::try_collect()` as a helper around `Iterator::collect()` as discussed [here](https://internals.rust-lang.org/t/idea-fallible-iterator-mapping-with-try-map/15715/5?u=a.lafrance). First time contributor so definitely open to any feedback about my implementation! Specifically wondering if I should open a tracking issue for the unstable feature I introduced. As the main participant in the internals discussion: r? `@scottmcm`
2022-02-17core: Implement trim functions on byte slicesDanilo Bargen-0/+78
Co-authored-by: Jubilee Young <workingjubilee@gmail.com>
2022-02-17Rollup merge of #89869 - kpreid:from-doc, r=yaahcMatthias Krüger-1/+20
Add documentation to more `From::from` implementations. For users looking at documentation through IDE popups, this gives them relevant information rather than the generic trait documentation wording “Performs the conversion”. For users reading the documentation for a specific type for any reason, this informs them when the conversion may allocate or copy significant memory versus when it is always a move or cheap copy. Notes on specific cases: * The new documentation for `From<T> for T` explains that it is not a conversion at all. * Also documented `impl<T, U> Into<U> for T where U: From<T>`, the other central blanket implementation of conversion. * The new documentation for construction of maps and sets from arrays of keys mentions the handling of duplicates. Future work could be to do this for *all* code paths that convert an iterable to a map or set. * I did not add documentation to conversions of a specific error type to a more general error type. * I did not add documentation to unstable code. This change was prepared by searching for the text "From<... for" and so may have missed some cases that for whatever reason did not match. I also looked for `Into` impls but did not find any worth documenting by the above criteria.
2022-02-17Auto merge of #94040 - Mark-Simulacrum:destabilize-load-store, r=Amanieubors-1/+1
Destabilize cfg(target_has_atomic_load_store = ...) This was not intended to be stabilized yet. This keeps the cfg_target_has_atomic feature gate name since compiler-builtins otherwise depends on it and I'd rather not try to manage a bump across a crates.io published repository given the time-sensitivity here (we need to land this quickly to avoid a beta backport). Closes https://github.com/rust-lang/rust/issues/32976 r? `@Amanieu`
2022-02-16Add a `try_collect()` helper method to `Iterator`Arthur Lafrance-0/+82
Tweaked `try_collect()` to accept more `Try` types Updated feature attribute for tracking issue
2022-02-16Rollup merge of #93962 - joboet:branchless_slice_ord, r=Mark-SimulacrumMatthias Krüger-12/+11
Make [u8]::cmp implementation branchless The current implementation generates rather ugly assembly code, branching when the common parts are equal. By performing the comparison of the lengths upfront using a subtraction, the assembly gets much prettier: https://godbolt.org/z/4e5fnEKGd. This will probably not impact speed too much, as the expensive part is in most cases the `memcmp`, but it sure looks better (I'm porting a sorting algorithm currently, and that branch just bothered me).
2022-02-16Destabilize cfg(target_has_atomic_load_store = ...)Mark Rousskov-1/+1
This was not intended to be stabilized yet.
2022-02-14Make [u8]::cmp implementation branchlessjoboet-12/+11
2022-02-14Add a comment to justify why the `pointer` field is `pub`.Daniel Henry-Mantilla-0/+5
Addresses https://github.com/rust-lang/rust/pull/93176/files#r795258110.
2022-02-14Replace `def_site`-&-privacy implementation with a stability-based one.Daniel Henry-Mantilla-5/+6
Since `decl_macro`s and/or `Span::def_site()` is deemed quite unstable, no public-facing macro that relies on it can hope to be, itself, stabilized. We circumvent the issue by no longer relying on field privacy for safety and, instead, relying on an unstable feature-gate to act as the gate keeper for non users of the macro (thanks to `allow_internal_unstable`). This is technically not correct (since a `nightly` user could technically enable the feature and cause unsoundness with it); or, in other words, this makes the feature-gate used to gate the access to the field be (technically unsound, and in practice) `unsafe`. Hence it having `unsafe` in its name. Back to the macro, we go back to `macro_rules!` / `mixed_site()`-span rules thanks to declaring the `decl_macro` as `semitransparent`, which is a hack to basically have `pub macro_rules!` Co-Authored-By: Mara Bos <m-ou.se@m-ou.se>
2022-02-14Improve documentation.Daniel Henry-Mantilla-6/+5
Co-Authored-By: Mara Bos <m-ou.se@m-ou.se>
2022-02-14Add a stack-`pin!`-ning macro to the `pin` module.Daniel Henry-Mantilla-0/+242
Add a type annotation to improve error messages with type mismatches Add a link to the temporary-lifetime-extension section of the reference
2022-02-13Rollup merge of #93930 - name1e5s:chore/docs, r=Mark-SimulacrumMatthias Krüger-2/+2
add link to format_args! when mention it in docs close #93904