about summary refs log tree commit diff
path: root/library/alloc/src/string.rs
AgeCommit message (Collapse)AuthorLines
2021-10-11Add #[must_use] to as_type conversionsJohn Kugelman-0/+5
2021-10-11Rollup merge of #89753 - jkugelman:must-use-from_value-conversions, ↵Guillaume Gomez-0/+1
r=joshtriplett Add #[must_use] to from_value conversions I added two methods to the list myself. Clippy did not flag them because they take `mut` args, but neither modifies their argument. ```rust core::str const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str; std::ffi::CString unsafe fn from_raw(ptr: *mut c_char) -> CString; ``` I put a custom note on `from_raw`: ```rust #[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"] pub unsafe fn from_raw(ptr: *mut c_char) -> CString { ``` Parent issue: #89692 r? ``@joshtriplett``
2021-10-11Rollup merge of #89726 - jkugelman:must-use-alloc-constructors, r=joshtriplettGuillaume Gomez-0/+2
Add #[must_use] to alloc constructors Added `#[must_use]`. to the various forms of `new`, `pin`, and `with_capacity` in the `alloc` crate. No extra explanations given as I couldn't think of anything useful to add. I figure this deserves extra scrutiny compared to the other PRs I've done so far. In particular: * The 4 `pin`/`pin_in` methods I touched. Are there legitimate use cases for pinning and not using the result? Pinning's a difficult concept I'm not very comfortable with. * `Box`'s constructors. Do people ever create boxes just for the side effects... allocating or zeroing out memory? Parent issue: #89692 r? ``@joshtriplett``
2021-10-10Add #[must_use] to conversions that move selfJohn Kugelman-0/+4
2021-10-10Add #[must_use] to from_value conversionsJohn Kugelman-0/+1
2021-10-10Add #[must_use] to alloc constructorsJohn Kugelman-0/+2
2021-10-04Stabilize try_reserveKornel-4/+2
2021-10-02Make diangostic item names consistentCameron Steffen-1/+1
2021-09-25Apply 16 commits (squashed)Frank Steffahn-23/+25
---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::fmt ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::{rc, sync} ---------- Fix spacing for links inside code blocks, and improve link tooltips in alloc::string ---------- Fix spacing for links inside code blocks in alloc::vec ---------- Fix spacing for links inside code blocks in core::option ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in core::result ---------- Fix spacing for links inside code blocks in core::{iter::{self, iterator}, stream::stream, poll} ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{fs, path} ---------- Fix spacing for links inside code blocks in std::{collections, time} ---------- Fix spacing for links inside code blocks in and make formatting of `&str`-like types consistent in std::ffi::{c_str, os_str} ---------- Fix spacing for links inside code blocks, and improve link tooltips in std::ffi ---------- Fix spacing for links inside code blocks, and improve a few link tooltips in std::{io::{self, buffered::{bufreader, bufwriter}, cursor, util}, net::{self, addr}} ---------- Fix typo in link to `into` for `OsString` docs ---------- Remove tooltips that will probably become redundant in the future ---------- Apply suggestions from code review Replacing `…std/primitive.reference.html` paths with just `reference` Co-authored-by: Joshua Nelson <github@jyn.dev> ---------- Also replace `…std/primitive.reference.html` paths with just `reference` in `core::pin`
2021-08-17Constified `Default` implementationsDeadbeef-1/+2
The libs-api team agrees to allow const_trait_impl to appear in the standard library as long as stable code cannot be broken (they are properly gated) this means if the compiler teams thinks it's okay, then it's okay. My priority on constifying would be: 1. Non-generic impls (e.g. Default) or generic impls with no bounds 2. Generic functions with bounds (that use const impls) 3. Generic impls with bounds 4. Impls for traits with associated types For people opening constification PRs: please cc me and/or oli-obk.
2021-08-08Auto merge of #86879 - YohDeadfall:stabilize-vec-shrink-to, r=dtolnaybors-2/+1
Stabilize Vec<T>::shrink_to This PR stabilizes `shrink_to` feature and closes the corresponding issue. The second point was addressed already, and no `panic!` should occur. Closes #56431.
2021-08-08Bump shrink_to stabilization to Rust 1.56David Tolnay-1/+1
2021-08-05alloc: Use intra doc links for the reserve functionest31-1/+3
The sentence exists to highlight the existence of a performance footgun of repeated calls of the reserve_exact function.
2021-07-30Rollup merge of #87574 - cuviper:retain-examples, r=joshtriplettYuki Okushi-3/+4
Update the examples in `String` and `VecDeque::retain` The examples added in #60396 used a "clever" post-increment hack, unrelated to the actual point of the examples. That hack was found [confusing] in the users forum, and #81811 already changed the `Vec` example to use a more direct iterator. This commit changes `String` and `VecDeque` in the same way for consistency. [confusing]: https://users.rust-lang.org/t/help-understand-strange-expression/62858
2021-07-29Fix may not to appropriate might not or must notAli Malik-3/+3
2021-07-28Update the examples in `String` and `VecDeque::retain`Josh Stone-3/+4
The examples added in #60396 used a "clever" post-increment hack, unrelated to the actual point of the examples. That hack was found [confusing] in the users forum, and #81811 already changed the `Vec` example to use a more direct iterator. This commit changes `String` and `VecDeque` in the same way for consistency. [confusing]: https://users.rust-lang.org/t/help-understand-strange-expression/62858
2021-07-06Stabilize Vec<T>::shrink_toYoh Deadfall-2/+1
2021-07-06Rollup merge of #86852 - Amanieu:remove_doc_aliases, r=joshtriplettYuki Okushi-3/+0
Remove some doc aliases As per the new doc alias policy in https://github.com/rust-lang/std-dev-guide/pull/25, this removes some controversial doc aliases: - `malloc`, `alloc`, `realloc`, etc. - `length` (alias for `len`) - `delete` (alias for `remove` in collections and also file/directory deletion) r? `@joshtriplett`
2021-07-04Stabilize `string_drain_as_str`Yuki Okushi-16/+14
2021-06-30Remove "length" doc aliasesAmanieu d'Antras-1/+0
2021-06-30Remove alloc/malloc/calloc/realloc doc aliasesAmanieu d'Antras-2/+0
2021-06-18Use `copy_nonoverlapping` to copy `bytes` in `String::insert_bytes`Paolo Barbolini-1/+1
2021-06-09Rollup merge of #85715 - fee1-dead:document-string, r=JohnTitorYuki Okushi-7/+43
Document `From` impls in string.rs
2021-06-06String::remove_matches O(n^2) -> O(n)Tamir Duberstein-15/+38
Copy only non-matching bytes.
2021-06-06Use iter::from_fn in String::remove_matchesTamir Duberstein-9/+3
2021-05-29Add `String::extend_from_within`Waffle-0/+36
This patch adds `String::extend_from_within` function under the `string_extend_from_within` feature gate similar to the `Vec::extend_from_within` function.
2021-05-26Document `From` impls in string.rsDeadbeef-7/+43
2021-05-17Optimize default ToString implMark Rousskov-2/+3
This avoids a zero-length write_str call, which boils down to a zero-length memmove and ultimately costs quite a few instructions on some workloads. This is approximately a 0.33% instruction count win on diesel-check.
2021-05-05alloc: Add unstable Cfg feature `no-global_oom_handling`John Ericson-5/+75
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-05-03Fix stability attributes of byte-to-string specializationLingMan-2/+2
2021-05-02Auto merge of #82576 - gilescope:to_string, r=Amanieubors-0/+41
i8 and u8::to_string() specialisation (far less asm). Take 2. Around 1/6th of the assembly to without specialisation. https://godbolt.org/z/bzz8Mq (partially fixes #73533 )
2021-03-22Rollup merge of #82554 - SkiFire13:fix-string-retain-unsoundness, r=m-ou-seDylan DPC-15/+22
Fix invalid slice access in String::retain As noted in #78499, the previous fix was technically still unsound because it accessed elements of a slice outside its bounds (even though they were still inside the same allocation). This PR addresses that concern by switching to a dropguard approach.
2021-03-21Auto merge of #83053 - oli-obk:const_stab_version, r=m-ou-sebors-1/+1
Fix const stability `since` versions. fixes #82085 r? `@m-ou-se`
2021-03-19Auto merge of #71780 - jcotton42:string_remove_matches, r=joshtriplettbors-0/+56
Implement String::remove_matches Closes #50206. I lifted the function help from `@frewsxcv's` original PR (#50015), hope they don't mind. I'm also wondering whether it would be useful for `remove_matches` to collect up the removed substrings into a `Vec` and return them, right now they're just overwritten by the copy and lost.
2021-03-15Fix const stability `since` versions.Oli Scherer-1/+1
2021-03-08Closer similarities.Giles Cope-26/+16
2021-03-08Update library/alloc/src/string.rsSquirrel-6/+3
Co-authored-by: LingMan <LingMan@users.noreply.github.com>
2021-03-07Add documentation for string->Cow conversionsMichael Howell-0/+33
Mostly, it's just to reassure everyone that these functions don't allocate. Part of #51430
2021-03-07vec![0;4] is a fast path.Giles Cope-22/+20
After much tweaking found a way to get similar asm size as the u8 to_string implementation.
2021-03-05Implement String::remove_matchesJosh Cotton-0/+56
2021-03-04less uB in i8Giles Cope-2/+6
2021-03-04Alternative LUT rather than dividing.Giles Cope-1/+34
2021-02-27u8::to_string() specialisation (far less asm).Giles Cope-0/+19
2021-02-26Fix invalid slice access in String::retainGiacomo Stevanato-15/+22
2021-02-25Convert primitives to use intra-doc linksJoshua Nelson-1/+1
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-16a few more diagnostic itemsAndrea Nall-0/+1
2021-02-12Rename `Range::ensure_subset_of` to `slice::range`dylni-2/+3
2021-02-12Fix possible soundness issue in `ensure_subset_of`dylni-1/+1
2021-02-12Improve design of `assert_len`dylni-2/+2