about summary refs log tree commit diff
path: root/library/alloc/src/slice.rs
AgeCommit message (Collapse)AuthorLines
2023-01-28slice: Add a specialization for clone_into when T is CopyNeil Roberts-10/+33
The implementation for the ToOwned::clone_into method on [T] is a copy of the code for vec::clone_from. In 361398009be6 the code for vec::clone_from gained a specialization for when T is Copy. This commit copies that specialization over to the clone_into implementation.
2023-01-20Rollup merge of #104672 - Voultapher:unify-sort-modules, r=thomccMichael Goulet-309/+39
Unify stable and unstable sort implementations in same core module This moves the stable sort implementation to the core::slice::sort module. By virtue of being in core it can't access `Vec`. The two `Vec` used by merge sort, `buf` and `runs`, are modelled as custom types that implement the very limited required `Vec` interface with the help of provided allocation and free functions. This is done to allow future re-use of functions and logic between stable and unstable sort. Such as `insert_head`. This is in preparation of #100856 and #104116. It only moves code, it *doesn't* change any of the sort related logic. This unlocks the ability to share `insert_head`, `insert_tail`, `swap_if_less` `merge` and more. Tagging ````@Mark-Simulacrum```` I hope this allows progress on #100856, by moving `merge_sort` here I hope future changes will be easier to review.
2023-01-04Update rand in the stdlib tests, and remove the getrandom feature from itThom Chiovoloni-0/+3
2022-12-30Update paths in comments.jonathanCogan-1/+1
2022-12-30Replace libstd, libcore, liballoc in docs.jonathanCogan-1/+1
2022-11-20Unify stable and unstable sort implementations in same core moduleLukas Bergdoll-309/+39
This moves the stable sort implementation to the core::slice::sort module. By virtue of being in core it can't access `Vec`. The two `Vec` used by merge sort, `buf` and `runs`, are modelled as custom types that implement the very limited required `Vec` interface with the help of provided allocation and free functions. This is done to allow future re-use of functions and logic between stable and unstable sort. Such as `insert_head`.
2022-11-14rustdoc: Add copy to the description of repeatyancy-1/+1
2022-09-25Auto merge of #102169 - scottmcm:constify-some-conditions, r=thomccbors-4/+2
Make ZST checks in core/alloc more readable There's a bunch of these checks because of special handing for ZSTs in various unsafe implementations of stuff. This lets them be `T::IS_ZST` instead of `mem::size_of::<T>() == 0` every time, making them both more readable and more terse. *Not* proposed for stabilization. Would be `pub(crate)` except `alloc` wants to use it too. (And while it doesn't matter now, if we ever get something like #85836 making it a const can help codegen be simpler.)
2022-09-22Make ZST checks in core/alloc more readableScott McMurray-4/+2
There's a bunch of these checks because of special handing for ZSTs in various unsafe implementations of stuff. This lets them be `T::IS_ZST` instead of `mem::size_of::<T>() == 0` every time, making them both more readable and more terse. *Not* proposed for stabilization at this time. Would be `pub(crate)` except `alloc` wants to use it too. (And while it doesn't matter now, if we ever get something like 85836 making it a const can help codegen be simpler.)
2022-09-14Cleanup closures.raldone01-1/+1
2022-08-22Rollup merge of #93162 - camsteffen:std-prim-docs, r=Mark-SimulacrumDylan DPC-75/+5
Std module docs improvements My primary goal is to create a cleaner separation between primitive types and primitive type helper modules (fixes #92777). I also changed a few header lines in other top-level std modules (seen at https://doc.rust-lang.org/std/) for consistency. Some conventions used/established: * "The \`Box\<T>` type for heap allocation." - if a module mainly provides a single type, name it and summarize its purpose in the module header * "Utilities for the _ primitive type." - this wording is used for the header of helper modules * Documentation for primitive types themselves are removed from helper modules * provided-by-core functionality of primitive types is documented in the primitive type instead of the helper module (such as the "Iteration" section in the slice docs) I wonder if some content in `std::ptr` should be in `pointer` but I did not address this.
2022-08-21Replace most uses of `pointer::offset` with `add` and `sub`Maybe Waffle-3/+3
2022-08-20Improve primitive/std docs separation and headersCameron Steffen-75/+5
2022-07-16Borrow Vec<T, A> as [T]yanchith-2/+2
2022-05-30Add reexport of slice::from{,_mut}_ptr_range to alloc & stdMaybe Waffle-0/+2
At first I was confused why `std::slice::from_ptr_range` didn't work :D
2022-05-11Rename `unsigned_offset_from` to `sub_ptr`Scott McMurray-1/+1
2022-05-11Add `unsigned_offset_from` on pointersScott McMurray-1/+1
Like we have `add`/`sub` which are the `usize` version of `offset`, this adds the `usize` equivalent of `offset_from`. Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`. As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives. That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change. This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE. It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order.
2022-05-09Auto merge of #95960 - jhpratt:remove-rustc_deprecated, r=compiler-errorsbors-1/+1
Remove `#[rustc_deprecated]` This removes `#[rustc_deprecated]` and introduces diagnostics to help users to the right direction (that being `#[deprecated]`). All uses of `#[rustc_deprecated]` have been converted. CI is expected to fail initially; this requires #95958, which includes converting `stdarch`. I plan on following up in a short while (maybe a bootstrap cycle?) removing the diagnostics, as they're only intended to be short-term.
2022-04-14library: Move `CStr` to libcore, and `CString` to liballocVadim Petrochenkov-1/+1
2022-04-14Remove use of `#[rustc_deprecated]`Jacob Pratt-1/+1
2022-04-05trivial cfg(bootstrap) changesPietro Albini-15/+13
2022-03-30Auto merge of #94963 - lcnr:inherent-impls-std, r=oli-obk,m-ou-sebors-2/+15
allow arbitrary inherent impls for builtin types in core Part of https://github.com/rust-lang/compiler-team/issues/487. Slightly adjusted after some talks with `@m-ou-se` about the requirements of `t-libs-api`. This adds a crate attribute `#![rustc_coherence_is_core]` which allows arbitrary impls for builtin types in core. For other library crates impls for builtin types should be avoided if possible. We do have to allow the existing stable impls however. To prevent us from accidentally adding more of these in the future, there is a second attribute `#[rustc_allow_incoherent_impl]` which has to be added to **all impl items**. This only supports impls for builtin types but can easily be extended to additional types in a future PR. This implementation does not check for overlaps in these impls. Perfectly checking that requires us to check the coherence of these incoherent impls in every crate, as two distinct dependencies may add overlapping methods. It should be easy enough to detect if it goes wrong and the attribute is only intended for use inside of std. The first two commits are mostly unrelated cleanups.
2022-03-30remove now unnecessary lang itemslcnr-2/+2
2022-03-30rework implementation for inherent impls for builtin typeslcnr-0/+13
2022-03-29cleanup some of the less terrifying library codeAria Beingessner-2/+2
2022-03-29Make the stdlib largely conform to strict provenance.Aria Beingessner-1/+1
Some things like the unwinders and system APIs are not fully conformant, this only covers a lot of low-hanging fruit.
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-1/+1
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-02-12Stabilise inherent_ascii_escape (FCP in #77174)ltdk-1/+1
2022-01-19Rollup merge of #89621 - digama0:patch-2, r=yaahcMatthias Krüger-1/+4
doc: guarantee call order for sort_by_cached_key `slice::sort_by_cached_key` takes a caching function `f: impl FnMut(&T) -> K`, which means that the order that calls to the caching function are made is user-visible. This adds a clause to the documentation to promise the current behavior, which is that `f` is called on all elements of the slice from left to right, unless the slice has len < 2 in which case `f` is not called. For example, this can be used to ensure that the following code is a correct way to involve the index of the element in the sort key: ```rust let mut index = 0; slice.sort_by_cached_key(|x| (my_key(index, x), index += 1).0); ```
2022-01-04Clarify that ordering is unspecifiedMario Carneiro-0/+2
2022-01-04Update wordingMario Carneiro-3/+3
2021-12-18Derive src pointers in sort drop guards from &TBen Kimock-3/+3
The src pointers in CopyOnDrop and InsertionHole used to be *mut T, and were derived via automatic conversion from &mut T. According to Stacked Borrows 2.1, this means that those pointers become invalidated by interior mutation in the comparison function. But there's no need for mutability in this code path. Thus, we can change the drop guards to use *const and derive those from &T.
2021-11-05Re-export `core::slice::EscapeAscii`mbartlett21-0/+2
2021-11-05Re-export `core::slice::SplitInclusive[Mut]`mbartlett21-0/+2
2021-10-09Add #[must_use] to string/char transformation methodsJohn Kugelman-0/+4
These methods could be misconstrued as modifying their arguments instead of returning new values. Where possible I made the note recommend a method that does mutate in place.
2021-10-07doc: guarantee call order for sort_by_cached_keyMario Carneiro-1/+2
`slice::sort_by_cached_key` takes a caching function `f: impl FnMut(&T) -> K`, which means that the order that calls to the caching function are made is user-visible. This adds a clause to the documentation to promise the current behavior, which is that `f` is called on all elements of the slice from left to right, unless the slice has len < 2 in which case `f` is not called.
2021-06-23Use HTTPS links where possibleSmitty-1/+1
2021-05-05alloc: Add unstable Cfg feature `no-global_oom_handling`John Ericson-2/+30
For certain sorts of systems, programming, it's deemed essential that all allocation failures be explicitly handled where they occur. For example, see Linus Torvald's opinion in [1]. Merely not calling global panic handlers, or always `try_reserving` first (for vectors), is not deemed good enough, because the mere presence of the global OOM handlers is burdens static analysis. One option for these projects to use rust would just be to skip `alloc`, rolling their own allocation abstractions. But this would, in my opinion be a real shame. `alloc` has a few `try_*` methods already, and we could easily have more. Features like custom allocator support also demonstrate and existing to support diverse use-cases with the same abstractions. A natural way to add such a feature flag would a Cargo feature, but there are currently uncertainties around how std library crate's Cargo features may or not be stable, so to avoid any risk of stabilizing by mistake we are going with a more low-level "raw cfg" token, which cannot be interacted with via Cargo alone. Note also that since there is no notion of "default cfg tokens" outside of Cargo features, we have to invert the condition from `global_oom_handling` to to `not(no_global_oom_handling)`. This breaks the monotonicity that would be important for a Cargo feature (i.e. turning on more features should never break compatibility), but it doesn't matter for raw cfg tokens which are not intended to be "constraint solved" by Cargo or anything else. To support this use-case we create a new feature, "global-oom-handling", on by default, and put the global OOM handler infra and everything else it that depends on it behind it. By default, nothing is changed, but users concerned about global handling can make sure it is disabled, and be confident that all OOM handling is local and explicit. For this first iteration, non-flat collections are outright disabled. `Vec` and `String` don't yet have `try_*` allocation methods, but are kept anyways since they can be oom-safely created "from parts", and we hope to add those `try_` methods in the future. [1]: https://lore.kernel.org/lkml/CAHk-=wh_sNLoz84AUUzuqXEsYH35u=8HV3vK-jbRbJ_B-JjGrg@mail.gmail.com/
2021-04-13Remove slice diagnostic itemCameron Steffen-1/+0
2021-03-27adjust documentation links for slice ascii case functions to use newer ↵Violet-2/+2
rustdoc link format
2021-03-27update links to make_ascii_lowercase for slice to point to methods on the ↵Violet-2/+2
same type, rather than on u8
2021-03-09convert slice doc link to intra-doc linksFrançois Mockers-5/+5
2021-02-25Convert primitives to use intra-doc linksJoshua Nelson-11/+11
2021-02-23Rollup merge of #82128 - anall:feature/add_diagnostic_items, r=davidtwcoDylan DPC-0/+1
add diagnostic items for OsString/PathBuf/Owned as well as to_vec on slice This is adding diagnostic items to be used by rust-lang/rust-clippy#6730, but my understanding is the clippy-side change does need to be done over there since I am adding a new clippy feature. Add diagnostic items to the following types: OsString (os_string_type) PathBuf (path_buf_type) Owned (to_owned_trait) As well as the to_vec method on slice/[T]
2021-02-15requested/proposed changesAndrea Nall-1/+1
2021-02-15add diagnostic itemsAndrea Nall-0/+1
Add diagnostic items to the following types: OsString (os_string_type) PathBuf (path_buf_type) Owned (to_owned_trait) As well as the to_vec method on slice/[T]
2021-02-12Rename `Range::ensure_subset_of` to `slice::range`dylni-0/+2
2020-12-31Replace the tracking issue for the slice_group_by featureClément Renault-1/+1
2020-12-10Fix the fmt issuesClément Renault-2/+2
2020-12-10Use none as the issue instead of 0Clément Renault-1/+1