about summary refs log tree commit diff
path: root/library/alloc/src
AgeCommit message (Collapse)AuthorLines
2021-10-21Clarify undefined behaviour for binary heap, btree and hashsetWilfred Hughes-9/+9
Previously, it wasn't clear whether "This could include" was referring to logic errors, or undefined behaviour. Tweak wording to clarify this sentence does not relate to UB.
2021-10-21Rollup merge of #90099 - SkiFire13:fix-vec-swap-remove, r=dtolnayYuki Okushi-3/+4
Fix MIRI UB in `Vec::swap_remove` Fixes #90055 I find it weird that `Vec::swap_remove` read the last element to the stack just to immediately put it back in the `Vec` in place of the one at index `index`. It seems much more natural to me to just read the element at position `index` and then move the last element in its place. I guess this might also slightly improve codegen.
2021-10-21Rollup merge of #90010 - rusticstuff:vecdeque_with_capacity_in_overflow, ↵Yuki Okushi-1/+1
r=m-ou-se Avoid overflow in `VecDeque::with_capacity_in()`. The overflow only happens if alloc is compiled with overflow checks enabled and the passed capacity is greater or equal 2^(usize::BITS-1). The overflow shadows the expected "capacity overflow" panic leading to a test failure if overflow checks are enabled for std in the CI. Unblocks [CI: Enable overflow checks for test (non-dist) builds #89776](https://github.com/rust-lang/rust/pull/89776). For some reason the overflow is only observable with optimization turned off, but that is a separate issue.
2021-10-20Prevent invalid values from existing in Vec::swap_removeGiacomo Stevanato-3/+4
2021-10-20Stabilize `option_result_unwrap_unchecked`Miguel Ojeda-1/+0
Closes https://github.com/rust-lang/rust/issues/81383. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2021-10-19Reenable feature(nll) in alloc.Mara Bos-0/+1
2021-10-19Remove unused language #![feature]s from alloc.Mara Bos-5/+0
2021-10-19Remove unused library #![feature]s from alloc.Mara Bos-3/+0
2021-10-19Sort and categorize #![feature]s in alloc.Mara Bos-42/+52
2021-10-18Avoid overflow in `VecDeque::with_capacity_in()`.Hans Kratz-1/+1
2021-10-16Rollup merge of #89898 - Amanieu:remove_alloc_prelude, r=joshtriplettMatthias Krüger-30/+0
Remove alloc::prelude As per the libs team decision in #58935. Closes #58935
2021-10-15Auto merge of #89337 - mbrubeck:vec-leak, r=m-ou-sebors-3/+5
Avoid allocations and copying in Vec::leak The [`Vec::leak`] method (#62195) is currently implemented by calling `Vec::into_boxed_slice` and `Box::leak`. This shrinks the vector before leaking it, which potentially causes a reallocation and copies the vector's contents. By avoiding the conversion to `Box`, we can instead leak the vector without any expensive operations, just by returning a slice reference and forgetting the `Vec`. Users who *want* to shrink the vector first can still do so by calling `shrink_to_fit` explicitly. **Note:** This could break code that uses `Box::from_raw` to “un-leak” the slice returned by `Vec::leak`. However, the `Vec::leak` docs explicitly forbid this, so such code is already incorrect. [`Vec::leak`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.leak
2021-10-15Add #[must_use] to remaining alloc functionsJohn Kugelman-21/+76
2021-10-15Auto merge of #88717 - tabokie:vecdeque-fast-append, r=m-ou-sebors-2/+30
Optimize VecDeque::append Optimize `VecDeque::append` to do unsafe copy rather than iterating through each element. On my `Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz`, the benchmark shows 37% improvements: ``` Master: custom-bench vec_deque_append 583164 ns/iter custom-bench vec_deque_append 550040 ns/iter Patched: custom-bench vec_deque_append 349204 ns/iter custom-bench vec_deque_append 368164 ns/iter ``` Additional notes on the context: this is the third attempt to implement a non-trivial version of `VecDeque::append`, the last two are reverted due to unsoundness or regression, see: - https://github.com/rust-lang/rust/pull/52553, reverted in https://github.com/rust-lang/rust/pull/53571 - https://github.com/rust-lang/rust/pull/53564, reverted in https://github.com/rust-lang/rust/pull/54851 Both cases are covered by existing tests. Signed-off-by: tabokie <xy.tao@outlook.com>
2021-10-15Remove alloc::preludeAmanieu d'Antras-30/+0
As per the libs team decision in #58935. Closes #58935
2021-10-14Rollup merge of #89878 - GuillaumeGomez:add-missing-cfg-hide, r=notriddleMatthias Krüger-0/+1
Fix missing remaining compiler specific cfg information Follow-up of #89596. We forgot a few of them: ![Screenshot from 2021-10-14 11-36-44](https://user-images.githubusercontent.com/3050060/137292700-64ebc59f-d9d2-41f2-be3a-fa5bf211523c.png) ![Screenshot from 2021-10-14 11-36-56](https://user-images.githubusercontent.com/3050060/137292703-f63fa4e5-2c56-446b-9f86-3652f03dfe59.png) r? `@notriddle`
2021-10-14Fix missing remaining compiler specific cfg informationGuillaume Gomez-0/+1
2021-10-13Rollup merge of #89814 - jkugelman:must-use-string-transforms-typo, ↵Yuki Okushi-1/+1
r=joshtriplett Fix uppercase/lowercase error Fix https://github.com/rust-lang/rust/pull/89694#discussion_r726829890 r? ````@joshtriplett````
2021-10-12Add #[must_use] to expensive computationsJohn Kugelman-0/+10
The unifying theme for this commit is weak, admittedly. I put together a list of "expensive" functions when I originally proposed this whole effort, but nobody's cared about that criterion. Still, it's a decent way to bite off a not-too-big chunk of work. Given the grab bag nature of this commit, the messages I used vary quite a bit.
2021-10-12Add #[must_use] to Rc::downgradeJohn Kugelman-1/+3
2021-10-12Remove potentially unsound note on reconstructing a leaked Vec.Mara Bos-3/+0
2021-10-12Fix uppercase/lowercase errorJohn Kugelman-1/+1
2021-10-12Rollup merge of #89796 - jkugelman:must-use-non-mutating-verb-methods, ↵the8472-1/+7
r=joshtriplett Add #[must_use] to non-mutating verb methods These are methods that could be misconstrued to mutate their input, similar to #89694. I gave each one a different custom message. I wrote that `upgrade` and `downgrade` don't modify the input pointers. Logically they don't, but technically they do... Parent issue: #89692 r? ```@joshtriplett```
2021-10-12Rollup merge of #89778 - jkugelman:must-use-as_type-conversions, r=joshtriplettthe8472-0/+11
Add #[must_use] to as_type conversions Clippy missed these: ```rust alloc::string::String fn as_mut_str(&mut self) -> &mut str; core::mem::NonNull<T> unsafe fn as_uninit_mut<'a>(&mut self) -> &'a MaybeUninit<T>; str unsafe fn as_bytes_mut(&mut self) -> &mut [u8]; str fn as_mut_ptr(&mut self) -> *mut u8; ``` Parent issue: #89692 r? ````@joshtriplett````
2021-10-12Mention Rust version in Vec::leak docs.Mara Bos-4/+5
2021-10-11Add #[must_use] to non-mutating verb methodsJohn Kugelman-1/+7
2021-10-11Add #[must_use] to as_type conversionsJohn Kugelman-0/+11
2021-10-11Rollup merge of #89753 - jkugelman:must-use-from_value-conversions, ↵Guillaume Gomez-0/+2
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/+38
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-11Auto merge of #89755 - jkugelman:must-use-conversions-that-move-self, ↵bors-10/+23
r=joshtriplett Add #[must_use] to conversions that move self Everything here got the same message. Is the wording okay? ```rust #[must_use = "`self` will be dropped if the result is not used"] ``` I want to draw attention to these methods in particular: ```rust alloc::sync::Arc<MaybeUninit<T>> unsafe fn assume_init(self) -> Arc<T>; alloc::sync::Arc<[MaybeUninit<T>]> unsafe fn assume_init(self) -> Arc<[T]>; core::pin::Pin<&'a mut T> const fn into_ref(self) -> Pin<&'a T>; core::pin::Pin<&'a mut T> const fn get_mut(self) -> &'a mut T; core::pin::Pin<&'a mut T> const unsafe fn get_unchecked_mut(self) -> &'a mut T; core::pin::Pin<&'a mut T> unsafe fn map_unchecked_mut(self, func: F) -> Pin<&'a mut U>; core::pin::Pin<&'a mut Pin<P>> fn as_deref_mut(self) -> Pin<&'a mut P::Target>; ``` Parent issue: #89692 r? `@joshtriplett`
2021-10-10Add #[must_use] to conversions that move selfJohn Kugelman-10/+23
2021-10-10Add #[must_use] to from_value conversionsJohn Kugelman-0/+2
2021-10-10Mark `Arc::from_inner` / `Rc::from_inner` as unsafeSebastian Dröge-32/+45
While it's an internal function, it is easy to create invalid Arc/Rcs to a dangling pointer with it. Fixes https://github.com/rust-lang/rust/issues/89740
2021-10-10Rollup merge of #89718 - jkugelman:must-use-is_condition-tests, r=joshtriplettMatthias Krüger-0/+3
Add #[must_use] to is_condition tests There's nothing insightful to say about these so I didn't write any extra explanations. Parent issue: #89692
2021-10-10Rollup merge of #89705 - nbdd0121:doc, r=GuillaumeGomezMatthias Krüger-0/+1
Cfg hide no_global_oom_handling and no_fp_fmt_parse These are unstable sysroot customisation cfg options that only projects building their own sysroot will use (e.g. Rust-for-linux). Most users shouldn't care. `no_global_oom_handling` can be especially annoying since it's applied on many commonly used alloc crate methods (e.g. `Box::new`, `Vec::push`). r? ```@GuillaumeGomez```
2021-10-10Add #[must_use] to alloc constructorsJohn Kugelman-0/+38
2021-10-09Add #[must_use] to is_condition testsJohn Kugelman-0/+3
There's nothing insightful to say about these so I didn't write any extra explanations.
2021-10-09Cfg hide no_global_oom_handling and no_fp_fmt_parseGary Guo-0/+1
2021-10-09Rollup merge of #89664 - timClicks:51430-document-boxed-conversions, r=m-ou-seGuillaume Gomez-0/+41
Add documentation to boxed conversions Among other changes, documents whether allocations are necessary to complete the type conversion. Part of #51430, supersedes #89199
2021-10-09Remove unnecessary hyphenTim McNamara-1/+1
Co-authored-by: Laurențiu Nicola <lnicola@users.noreply.github.com>
2021-10-09Simplify wordingTim McNamara-4/+4
Co-authored-by: Josh Triplett <josh@joshtriplett.org> Co-authored-by: Laurențiu Nicola <lnicola@users.noreply.github.com>
2021-10-09Add #[must_use] to string/char transformation methodsJohn Kugelman-0/+10
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-08Cfg hide more conditions for allocLoïc BRANSTETT-1/+6
2021-10-08Add documentation to boxed conversionsTim McNamara-0/+41
Among other changes, documents whether allocations are necessary to complete the type conversion. Part of #51430 Co-authored-by: Giacomo Stevanato <giaco.stevanato@gmail.com> Co-authored-by: Joshua Nelson <github@jyn.dev>
2021-10-07Rollup merge of #89596 - GuillaumeGomez:implicit-doc-cfg, r=jyn514Guillaume Gomez-0/+6
Make cfg imply doc(cfg) This is a reopening of #79341, rebased and modified a bit (we made a lot of refactoring in rustdoc's types so they needed to be reflected in this PR as well): * `hidden_cfg` is now in the `Cache` instead of `DocContext` because `cfg` information isn't stored anymore on `clean::Attributes` type but instead computed on-demand, so we need this information in later parts of rustdoc. * I removed the `bool_to_options` feature (which makes the code a bit simpler to read for `SingleExt` trait implementation. * I updated the version for the feature. There is only one thing I couldn't figure out: [this comment](https://github.com/rust-lang/rust/pull/79341#discussion_r561855624) > I think I'll likely scrap the whole `SingleExt` extension trait as the diagnostics for 0 and >1 items should be different. How/why should they differ? EDIT: this part has been solved, the current code was fine, just needed a little simplification. cc `@Nemo157` r? `@jyn514` Original PR description: This is only active when the `doc_cfg` feature is active. The implicit cfg can be overridden via `#[doc(cfg(...))]`, so e.g. to hide a `#[cfg]` you can use something like: ```rust #[cfg(unix)] #[doc(cfg(all()))] pub struct Unix; ``` By adding `#![doc(cfg_hide(foobar))]` to the crate attributes the cfg `#[cfg(foobar)]` (and _only_ that _exact_ cfg) will not be implicitly treated as a `doc(cfg)` to render a message in the documentation.
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-10-06Rollup merge of #89245 - DeveloperC286:iter_mut_fields_to_private, ↵Manish Goregaokar-16/+21
r=joshtriplett refactor: make VecDeque's IterMut fields module-private, not just crate-private Made the fields of VecDeque's IterMut private by creating a IterMut::new(...) function to create a new instance of IterMut and migrating usage to use IterMut::new(...).
2021-10-06Clean up code a bit:Guillaume Gomez-2/+3
* Remove "bool_to_options" feature * Update version for compiler feature * rustfmt
2021-10-05refactor: VecDeques IterMut fields to privateDeveloperC-16/+21
Made the fields of VecDeque's IterMut private by creating a IterMut::new(...) function to create a new instance of IterMut and migrating usage to use IterMut::new(...).
2021-10-05refactor: VecDeques Drain fields to privateDeveloperC286-17/+26