about summary refs log tree commit diff
path: root/library/alloc/src/boxed.rs
AgeCommit message (Collapse)AuthorLines
2024-08-27library: Stabilize new_uninit for Box, Rc, and ArcJubilee Young-27/+16
A partial stabilization that only affects: - AllocType<T>::new_uninit - AllocType<T>::assume_init - AllocType<[T]>::new_uninit_slice - AllocType<[T]>::assume_init where "AllocType" is Box, Rc, or Arc
2024-08-25Rollup merge of #129416 - workingjubilee:partial-move-from-stabilization, ↵Matthias Krüger-3/+6
r=dtolnay library: Move unstable API of new_uninit to new features - `new_zeroed` variants move to `new_zeroed_alloc` - the `write` fn moves to `box_uninit_write` The remainder will be stabilized in upcoming patches, as it was decided to only stabilize `uninit*` and `assume_init`.
2024-08-23library: Move unstable API of new_uninit to new featuresJubilee Young-3/+6
- `new_zeroed` variants move to `new_zeroed_alloc` - the `write` fn moves to `box_uninit_write` The remainder will be stabilized in upcoming patches, as it was decided to only stabilize `uninit*` and `assume_init`.
2024-08-14add Box::as_ptr and Box::as_mut_ptr methodsRalf Jung-0/+89
2024-08-07Rollup merge of #125048 - dingxiangfei2009:stable-deref, r=amanieuMatthias Krüger-1/+4
PinCoerceUnsized trait into core cc ``@Darksonn`` ``@wedsonaf`` ``@ojeda`` This is a PR to introduce a `PinCoerceUnsized` trait in order to make trait impls generated by the proc-macro `#[derive(SmartPointer)]`, proposed by [RFC](https://github.com/rust-lang/rfcs/blob/e17e19ac7ad1c8ccad55d4babfaee1aa107d1da5/text/3621-derive-smart-pointer.md#pincoerceunsized-1), sound. There you may find explanation, justification and discussion about the alternatives. Note that we do not seek stabilization of this `PinCoerceUnsized` trait in the near future. The stabilisation of this trait does not block the eventual stabilization process of the `#[derive(SmartPointer)]` macro. Ideally, use of `DerefPure` is more preferrable except this will actually constitute a breaking change. `PinCoerceUnsized` emerges as a solution to the said soundness hole while avoiding the breaking change. More details on the `DerefPure` option have been described in this [section](https://github.com/rust-lang/rfcs/blob/e17e19ac7ad1c8ccad55d4babfaee1aa107d1da5/text/3621-derive-smart-pointer.md#derefpure) of the RFC linked above. Earlier discussion can be found in this [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/Pin.20and.20soundness.20of.20unsizing.20coercions) and [rust-for-linux thread](https://rust-lang.zulipchat.com/#narrow/stream/425075-rust-for-linux/topic/.23.5Bderive.28SmartPointer.29.5D.20and.20pin.20unsoundness.20rfc.233621). try-job: dist-various-2
2024-08-03Rollup merge of #127586 - zachs18:more-must-use, r=cuviperMatthias Krüger-0/+2
Add `#[must_use]` to some `into_raw*` functions. cc #121287 r? ``@cuviper`` Adds `#[must_use = "losing the pointer will leak memory"]`[^1] to `Box::into_raw(_with_allocator)`, `Vec::into_raw_parts(_with_alloc)`, `String::into_raw_parts`[^2], and `rc::{Rc, Weak}::into_raw_with_allocator` (Rc's normal `into_raw` and all of `Arc`'s `into_raw*`s are already `must_use`). Adds `#[must_use = "losing the raw <resource name may leak resources"]` to `IntoRawFd::into_raw_fd`, `IntoRawSocket::into_raw_socket`, and `IntoRawHandle::into_raw_handle`. [^1]: "*will* leak memory" may be too-strong wording (since `Box`/`Vec`/`String`/`rc::Weak` might not have a backing allocation), but I left it as-is for simplicity and consistency. [^2]: `String::into_raw_parts`'s `must_use` message is changed from the previous (possibly misleading) "`self` will be dropped if the result is not used".
2024-07-31PinCoerceUnsized trait into coreXiangfei Ding-1/+4
2024-07-29Reformat `use` declarations.Nicholas Nethercote-10/+7
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-26Fix doc nitsJohn Arundel-6/+8
Many tiny changes to stdlib doc comments to make them consistent (for example "Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph breaks, backticks for monospace style, and other minor nits. https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
2024-07-22Rollup merge of #127415 - AljoschaMeyer:master, r=dtolnay许杰友 Jieyou Xu (Joe)-2/+81
Add missing try_new_uninit_slice_in and try_new_zeroed_slice_in The methods for fallible slice allocation in a given allocator were missing from `Box`, which was an oversight according to https://github.com/rust-lang/wg-allocators/issues/130 This PR adds them as `try_new_uninit_slice_in` and `try_new_zeroed_slice_in`. I simply copy-pasted the implementations of `try_new_uninit_slice` and `try_new_zeroed_slice` and adusted doc comment, typings, and the allocator it uses internally. Also adds missing punctuation to the doc comments of `try_new_uninit_slice` and `try_new_zeroed_slice`. Related issue is https://github.com/rust-lang/rust/issues/32838 (Allocator traits and std::heap) *I think*. Also relevant is https://github.com/rust-lang/rust/issues/63291, but I did not add the corresponding `#[unstable]` proc macro, since `try_new_uninit_slice` and `try_new_zeroed_slice` are also not annotated with it.
2024-07-22Use given allocator instad of GlobalAljoscha Meyer-2/+2
2024-07-13Rollup merge of #127446 - zachs18:miri-stdlib-leaks-core-alloc, ↵Jubilee-0/+6
r=Mark-Simulacrum Remove memory leaks in doctests in `core`, `alloc`, and `std` cc `@RalfJung` https://github.com/rust-lang/rust/issues/126067 https://github.com/rust-lang/miri/issues/3670 Should be no actual *documentation* changes[^1], all added/modified lines in the doctests are hidden with `#`, This PR splits the existing memory leaks in doctests in `core`, `alloc`, and `std` into two general categories: 1. "Non-focused" memory leaks that are incidental to the thing being documented, and/or are easy to remove, i.e. they are only there because preventing the leak would make the doctest less clear and/or concise. - These doctests simply have a comment like `# // Prevent leaks for Miri.` above the added line that removes the memory leak. - [^2]Some of these would perhaps be better as part of the public documentation part of the doctest, to clarify that a memory leak can happen if it is not otherwise mentioned explicitly in the documentation (specifically the ones in `(A)Rc::increment_strong_count(_in)`). 2. "Focused" memory leaks that are intentional and documented, and/or are possibly fragile to remove. - These doctests have a `# // FIXME` comment above the line that removes the memory leak, with a note that once `-Zmiri-disable-leak-check` can be applied at test granularity, these tests should be "un-unleakified" and have `-Zmiri-disable-leak-check` enabled. - Some of these are possibly fragile (e.g. unleaking the result of `Vec::leak`) and thus should definitely not be made part of the documentation. This should be all of the leaks currently in `core` and `alloc`. I only found one leak in `std`, and it was in the first category (excluding the modules `@RalfJung` mentioned in https://github.com/rust-lang/rust/issues/126067 , and reducing the number of iterations of [one test](https://github.com/rust-lang/rust/blob/master/library/std/src/sync/once_lock.rs#L49-L94) from 1000 to 10) [^1]: assuming [^2] is not added [^2]: backlink
2024-07-10Clarify/add `must_use` messages for more `into_raw*` functions of `alloc` types.Zachary S-0/+2
2024-07-06Mitigate focused memory leaks in `alloc` doctests for Miri.Zachary S-0/+6
If/when `-Zmiri-disable-leak-check` is able to be used at test-granularity, it should applied to these tests instead of unleaking.
2024-07-06Fix them doc examples some moreAljoscha Meyer-2/+2
Apologies for the many attempts, my dev loop for this consists of editing on github, committing, and then waiting for the CI failure log to yell at me.
2024-07-06Fix doc examplesAljoscha Meyer-2/+2
2024-07-06Run formatter on alloc/src/boxed.rsAljoscha Meyer-4/+10
2024-07-06Add missing try_new_uninit_slice_in and try_new_zeroed_slice_inAljoscha Meyer-2/+75
The methods for fallible slice allocation in a given allocator were missing, which was an oversight according to https://github.com/rust-lang/wg-allocators/issues/130 This PR adds them as `try_new_uninit_slice_in` and `try_new_zeroed_slice_in`. Also adds missing punctuation to the doc comments of ` try_new_uninit_slice` and `try_new_zeroed_slice`
2024-07-04Add more checks for pointers with vtable metaMaybe Waffle-3/+3
The rules for casting `*mut X<dyn A>` -> `*mut Y<dyn B>` are as follows: - If `B` has a principal - `A` must have exactly the same principal (including generics) - Auto traits of `B` must be a subset of autotraits in `A` Note that `X<_>` and `Y<_>` can be identity, or arbitrary structs with last field being the dyn type. The lifetime of the trait object itself (`dyn ... + 'a`) is not checked. This prevents a few soundness issues with `#![feature(arbitrary_self_types)]` and trait upcasting. Namely, these checks make sure that vtable is always valid for the pointee.
2024-06-22Replace `WriteCloneIntoRaw` with `CloneToUninit`.Kevin Reid-2/+4
2024-06-11remove cfg(bootstrap)Pietro Albini-2/+1
2024-06-11replace version placeholderPietro Albini-12/+12
2024-05-20Add the impls for Box<[T]>: IntoIteratorMichael Goulet-0/+86
Co-authored-by: ltdk <usr@ltdk.xyz>
2024-05-05alloc: implement FromIterator for Box<str>Caleb Sander-0/+48
Box<[T]> implements FromIterator<T> using Vec<T> + into_boxed_slice(). Add analogous FromIterator implementations for Box<str> matching the current implementations for String. Remove the Global allocator requirement for FromIterator<Box<str>> too.
2024-04-17Rollup merge of #122201 - coolreader18:doc-clone_from, r=dtolnayMatthias Krüger-4/+22
Document overrides of `clone_from()` in core/std As mentioned in https://github.com/rust-lang/rust/pull/96979#discussion_r1379502413 Specifically, when an override doesn't just forward to an inner type, document the behavior and that it's preferred over simply assigning a clone of source. Also, change instances where the second parameter is "other" to "source". I reused some of the wording over and over for similar impls, but I'm not sure that the wording is actually *good*. Would appreciate feedback about that. Also, now some of these seem to provide pretty specific guarantees about behavior (e.g. will reuse the exact same allocation iff the len is the same), but I was basing it off of the docs for [`Box::clone_from`](https://doc.rust-lang.org/1.75.0/std/boxed/struct.Box.html#method.clone_from-1) - I'm not sure if providing those strong guarantees is actually good or not.
2024-04-16Box::into_raw: make Miri understand that this is a box-to-raw castRalf Jung-2/+6
2024-03-25Require DerefPure for patternsMichael Goulet-1/+4
2024-03-19Only split by-ref/by-move futures for async closuresMichael Goulet-5/+3
2024-03-18add_retag: ensure box-to-raw-ptr casts are preserved for MiriRalf Jung-11/+6
2024-03-09miri: do not apply aliasing restrictions to Box with custom allocatorRalf Jung-10/+14
2024-03-08Document overrides of `clone_from()`Noa-4/+22
Specifically, when an override doesn't just forward to an inner type, document the behavior and that it's preferred over simply assigning a clone of source. Also, change instances where the second parameter is "other" to "source".
2024-03-05only set noalias on Box with the global allocatorRalf Jung-0/+3
2024-02-22Auto merge of #118634 - Jules-Bertholet:box-allocator-static, r=Amanieubors-9/+3
Remove useless `'static` bounds on `Box` allocator #79327 added `'static` bounds to the allocator parameter for various `Box` + `Pin` APIs to ensure soundness. But it was a bit overzealous, some of the bounds aren't actually needed.
2024-02-06Harmonize blanket implementations for AsyncFn* traitsMichael Goulet-0/+29
2024-02-05Rollup merge of #113833 - WiktorPrzetacznik:master, r=dtolnayMatthias Krüger-8/+8
`std::error::Error` -> Trait Implementations: lifetimes consistency improvement This cleans up `std::error::Error` trait implementations lifetime inconsistency (`'static` -> `'a`) **Reasoning:** Trait implementations for `std::error::Error`, like: `impl From<&str> for Box<dyn Error + 'static, Global>` `impl<'a> From<&str> for Box<dyn Error + Sync + Send + 'a, Global>` use different lifetime annotations misleadingly implying using different life annotations here is a conscious, nonaccidental decision. [(Related forum discussion here)](https://users.rust-lang.org/t/confusing-std-error-source-code/97011/5?u=wiktor)
2024-01-26Fix outdated comment on Boxbjorn3-2/+1
2024-01-02Adjust library tests for unused_tuple_struct_fields -> dead_codeJake Goulding-0/+1
2023-12-04Remove useless `'static` bounds on `Box` allocatorJules Bertholet-9/+3
#79327 added `'static` bounds to the allocator parameter for various `Box` + `Pin` APIs to ensure soundness. But it was a bit overzealous, some of the bounds aren't actually needed.
2023-11-04docs: clarify explicitly freeing heap allocated memoryalpharush-3/+11
2023-10-31delegate box error provideBugen Zhao-0/+4
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
2023-10-21Update boxed.rsGimbles-1/+1
2023-10-20s/generator/coroutine/Oli Scherer-2/+2
2023-10-20s/Generator/Coroutine/Oli Scherer-5/+5
2023-08-14avoid transmuting Box when we can just cast raw pointers insteadRalf Jung-2/+2
2023-07-18std::error::Error -> Trait Implementations: lifetimes consistency improvementWiktorPrzetacznik-8/+8
2023-07-13Eliminate ZST allocations in `Box` and `Vec`Amanieu d'Antras-15/+29
2023-06-16remove box_free and replace with drop implDrMeepster-1/+9
2023-04-29`cfg`-gate `BoxFromSlice` traitJules Bertholet-0/+1
Co-authored-by: David Tolnay <dtolnay@gmail.com>
2023-04-26Loosen `From<&[T]> for Box<[T]>` bound to T: CloneJules Bertholet-8/+30
2023-04-16fix allocDeadbeef-12/+6