about summary refs log tree commit diff
path: root/src/liballoc/boxed.rs
AgeCommit message (Collapse)AuthorLines
2020-07-27mv std libs to library/mark-1200/+0
2020-07-05Remove the usage of the LengthAtMost32 traitRoman Proskuryakov-9/+2
2020-07-01Rollup merge of #73678 - Keno:patch-1, r=LukasKalbertodtManish Goregaokar-1/+4
Update Box::from_raw example to generalize better I know very little about rust, so I saw the example here ``` use std::alloc::{alloc, Layout}; unsafe { let ptr = alloc(Layout::new::<i32>()) as *mut i32; *ptr = 5; let x = Box::from_raw(ptr); } ``` and tried to generalize it by writing, ``` let layout = Layout::new::<T>(); let new_obj = unsafe { let ptr = alloc(layout) as *mut T; *ptr = obj; Box::from_raw(ptr) }; ``` for some more complicated `T`, which ended up crashing with SIGSEGV, because it tried to `drop_in_place` the previous object in `ptr` which is of course garbage. I think that changing this example to use `.write` instead would be a good idea to suggest the correct generalization. It is also more consistent with other documentation items in this file, which use `.write`. I also added a comment to explain it, but I'm not too attached to that, and can see it being too verbose in this place.
2020-06-24lints: add `improper_ctypes_definitions`David Wood-0/+2
This commit adds a new lint - `improper_ctypes_definitions` - which functions identically to `improper_ctypes`, but on `extern "C" fn` definitions (as opposed to `improper_ctypes`'s `extern "C" {}` declarations). Signed-off-by: David Wood <david@davidtw.co>
2020-06-23Update Box::from_raw example to generalize betterKeno Fischer-1/+4
I know very little about rust, so I saw this example and tried to generalize it by writing, ``` let layout = Layout::new::<T>(); let new_obj = unsafe { let ptr = alloc(layout) as *mut T; *ptr = obj; Box::from_raw(ptr) }; ``` for some more complicated `T`, which ended up crashing with SIGSEGV, because it tried to `drop_in_place` the previous object in `ptr` which is of course garbage. I also added a comment that explains why `.write` is used, but I think adding that comment is optional and may be too verbose here. I do however think that changing this example is a good idea to suggest the correct generalization. `.write` is also used in most of the rest of the documentation here, even if the example is `i32`, so it would additionally be more consistent.
2020-06-19Rollup merge of #72709 - LeSeulArtichaut:unsafe-liballoc, r=nikomatsakisManish Goregaokar-3/+3
`#[deny(unsafe_op_in_unsafe_fn)]` in liballoc This PR proposes to make use of the new `unsafe_op_in_unsafe_fn` lint, i.e. no longer consider the body of an unsafe function as an unsafe block and require explicit unsafe block to perform unsafe operations. This has been first (partly) suggested by @Mark-Simulacrum in https://github.com/rust-lang/rust/pull/69245#issuecomment-587817065 Tracking issue for the feature: #71668. ~~Blocked on #71862.~~ r? @Mark-Simulacrum cc @nikomatsakis can you confirm that those changes are desirable? Should I restrict it to only BTree for the moment?
2020-06-19`#[deny(unsafe_op_in_unsafe_fn)]` in liballocLeSeulArtichaut-3/+3
2020-06-17Reduce pointer casts in Box::into_boxed_sliceJosh Stone-1/+1
We only need to cast the pointer once to change `Box<T>` to an array `Box<[T; 1]>`, then we can let unsized coercion return `Box<[T]>`.
2020-05-30Rollup merge of #72499 - mendess:master, r=dtolnayRalf Jung-0/+8
Override Box::<[T]>::clone_from Avoid dropping and reallocating when cloning to an existing box if the lengths are the same. It would be nice if this could also be specialized for `Copy` but I don't know how that works since it's not on stable. Will gladly look into it if it's deemed as a good idea. This is my first PR with code, hope I did everything right :smile:
2020-05-23Override Box::<[T]>::clone_frommendess-0/+8
2020-05-20impl From<[T; N]> for Box<[T]>Ivan Tham-0/+19
Based on https://github.com/rust-lang/rust/pull/68692
2020-05-19Auto merge of #71447 - cuviper:unsized_cow, r=dtolnaybors-0/+23
impl From<Cow> for Box, Rc, and Arc These forward `Borrowed`/`Owned` values to existing `From` impls. - `Box<T>` is a fundamental type, so it would be a breaking change to add a blanket impl. Therefore, `From<Cow>` is only implemented for `[T]`, `str`, `CStr`, `OsStr`, and `Path`. - For `Rc<T>` and `Arc<T>`, `From<Cow>` is implemented for everything that implements `From` the borrowed and owned types separately.
2020-04-26Rollup merge of #71421 - elichai:2020-04-boxed-slice, r=sfacklerDylan DPC-0/+10
Add a function to turn Box<T> into Box<[T]> Hi, I think this is very useful, as currently it's not possible in safe rust to do this without re-allocating. an alternative implementation of the same function can be: ```rust pub fn into_boxed_slice<T>(boxed: Box<T>) -> Box<[T]> { unsafe { let slice = slice::from_raw_parts_mut(Box::into_raw(boxed), 1); Box::from_raw(slice) } } ``` The only thing that makes me a little uncomfortable is this line : > The alignment of array types is greater or equal to the alignment of its element type from https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html But then I see: > The alignment of &T, &mut T, *const T and *mut T are the same, and are at least the word size. > The alignment of &[T] is the word size. from https://rust-lang.github.io/unsafe-code-guidelines/layout/pointers.html#representation So I do believe this is valid(FWIW it also passes in miri https://play.rust-lang.org/?gist=c002b99364ee6b29862aeb3565a91c19)
2020-04-26Add a function to turn Box<T> into Box<[T]> (into_boxed_slice)Elichai Turkel-0/+10
2020-04-22impl From<Cow> for boxed slices and stringsJosh Stone-0/+23
These forward `Borrowed`/`Owned` values to existing `Box::from` impls. - `From<Cow<'_, [T]>> for Box<[T]>` - `From<Cow<'_, str>> for Box<str>` - `From<Cow<'_, CStr>> for Box<CStr>` - `From<Cow<'_, OsStr>> for Box<OsStr>` - `From<Cow<'_, Path>> for Box<Path>`
2020-04-16Implement `Box::into_raw` based on `Box::leak`Simon Sapin-10/+17
… instead of the other way around.
2020-04-15Apply suggestions from code reviewSimon Sapin-1/+1
Co-Authored-By: Ralf Jung <post@ralfj.de>
2020-04-15Deprecate `Box::into_raw_non_null`Simon Sapin-12/+21
Per https://github.com/rust-lang/rust/issues/47336#issuecomment-586589016
2020-04-04use ManuallyDrop instead of forget inside collectionsTrevor Spiteri-1/+1
This commit changes some usage of mem::forget into mem::ManuallyDrop in some Vec, VecDeque, BTreeMap and Box methods. Before the commit, the generated IR for some of the methods was longer, and even after optimization, some unwinding artifacts were still present.
2020-03-28Make fields in `MemoryBlock` publicTim Diekmann-2/+2
2020-03-26Fix issues from review and unsoundness of `RawVec::into_box`Tim Diekmann-11/+4
2020-03-26Overhaul of the `AllocRef` trait to match allocator-wg's latest consensTim Diekmann-23/+21
2020-03-15Bump the bootstrap compilerJonas Schievink-24/+0
2020-03-03Rollup merge of #69609 - TimDiekmann:excess, r=AmanieuYuki Okushi-2/+2
Remove `usable_size` APIs This removes the usable size APIs: - remove `usable_size` (obv) - change return type of allocating methods to include the allocated size - remove `_excess` API r? @Amanieu closes rust-lang/wg-allocators#17
2020-03-03Remove `usable_size` APIsTim Diekmann-2/+2
2020-02-28Stabilize `boxed_slice_try_from`Yuki Okushi-1/+1
2020-02-11Preparation for allocator aware `Box`Tim Diekmann-15/+16
2020-02-06Rollup merge of #68524 - jonas-schievink:generator-resume-arguments, r=ZoxcDylan DPC-0/+24
Generator Resume Arguments cc https://github.com/rust-lang/rust/issues/43122 and https://github.com/rust-lang/rust/issues/56974 Blockers: * [x] Fix miscompilation when resume argument is live across a yield point (https://github.com/rust-lang/rust/pull/68524#issuecomment-578459069) * [x] Fix 10% compile time regression in `await-call-tree` benchmarks (https://github.com/rust-lang/rust/pull/68524#issuecomment-578487162) * [x] Fix remaining 1-3% regression (https://github.com/rust-lang/rust/pull/68524#issuecomment-579566255) - resolved (https://github.com/rust-lang/rust/pull/68524#issuecomment-581144901) * [x] Make dropck rules account for resume arguments (https://github.com/rust-lang/rust/pull/68524#issuecomment-578541137) Follow-up work: * Change async/await desugaring to make use of this feature * Rewrite [`box_region.rs`](https://github.com/rust-lang/rust/blob/3d8778d767f0dde6fe2bc9459f21ead8e124d8cb/src/librustc_data_structures/box_region.rs) to use resume arguments (this shows up in profiles too)
2020-02-02Add a resume type parameter to `Generator`Jonas Schievink-0/+24
2020-01-31Fixed issue 68593hman523-1/+2
2020-01-27Rename `Alloc` to `AllocRef`Tim Diekmann-1/+1
2019-12-23Simplify Clone for Box<[T]>Josh Stone-42/+1
The bespoke `BoxBuilder` was basically a very simple `Vec`. Instead, let's clone to a real `Vec`, with all of its specialization for the task, then convert back to `Box<[T]>`.
2019-12-22Format the worldMark Rousskov-23/+13
2019-12-21Require issue = "none" over issue = "0" in unstable attributesRoss MacArthur-4/+4
2019-12-12Rollup merge of #62514 - stephaneyfx:box-ffi, r=nikomatsakisYuki Okushi-0/+53
Clarify `Box<T>` representation and its use in FFI This officializes what was only shown as a code example in [the unsafe code guidelines](https://rust-lang.github.io/unsafe-code-guidelines/layout/function-pointers.html?highlight=box#use) and follows [the discussion](https://github.com/rust-lang/unsafe-code-guidelines/issues/157) in the corresponding repository. It is also related to [the issue](https://github.com/rust-lang/rust/issues/52976) regarding marking `Box<T>` `#[repr(transparent)]`. If the statement this PR adds is incorrect or a more in-depth discussion is warranted, I apologize. Should it be the case, the example in the unsafe code guidelines should be amended and some document should make it clear that it is not sound/supported.
2019-12-11clarify that `Box<T>` should only be used when defined *in Rust*Nicholas Matsakis-7/+16
2019-12-10Fix description based on reviewStephane Raux-1/+1
2019-12-10Remove trailing whitespaceStephane Raux-4/+4
2019-12-09Specify behavior when passed a null pointerStephane Raux-2/+6
2019-12-09Use Niko's wordingStephane Raux-2/+21
2019-11-05alloc: Add new_zeroed() versions like new_uninit().Emilio Cobos Álvarez-0/+27
MaybeUninit has both uninit() and zeroed(), it seems reasonable to have the same surface on Box/Rc/Arc. Needs tests.
2019-11-01Update FFI exampleStephane Raux-4/+4
- Use meaningful names - Clarify comments - Fix C function declaration
2019-10-16Uninitialized boxes: check for zero-size allocation based on Layout::sizeSimon Sapin-4/+4
2019-10-06Fix zero-size uninitialized boxesSimon Sapin-4/+14
Requesting a zero-size allocation is not allowed, return a dangling pointer instead. CC https://github.com/rust-lang/rust/issues/63291#issuecomment-538692745
2019-10-05Hide the `Iterator` specialization behind a traitJonas Schievink-5/+22
2019-10-05Deny specializing items not in the parent implJonas Schievink-0/+5
2019-10-01Remove unneeded `fn main` blocks from docsLzu Tao-33/+21
2019-08-25Update Box representation comment based on reviewsStephane Raux-3/+2
2019-08-17Doc nitsSimon Sapin-4/+4
Co-Authored-By: Ralf Jung <post@ralfj.de>
2019-08-16Add tracking issue numbersSimon Sapin-4/+4