-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement Clone for Box<[T]> where T: Clone #26934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
unsafe { mem::transmute(Slice { | ||
data: data, | ||
len: self.len() | ||
}) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stylistically I think we've been moving to "one unsafe block" instead of a few scattered ones, so I think it's fine to just go ahead and wrap this entire method in an unsafe
block.
Implementation looks good to me! Just some stylistic nits here and there. cc @gankro |
Uhhhhh Why not just |
Ah just read the issue. Sucks. |
So funny story I'm actually in the middle of abstracting basically all of Vec's allocation logic into a seperate type: https://github.com/rust-lang/rust/compare/master...Gankro:raw-vec?expand=1 It would be trivial to add |
☔ The latest upstream changes (presumably #26928) made this pull request unmergeable. Please resolve the merge conflicts. |
impl<T: Clone> Clone for Box<[T]> { | ||
fn clone(&self) -> Self { | ||
let mut alloc = unsafe { | ||
heap::allocate(mem::size_of::<T>() * self.len(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't do this unconditionally; you need to special case mem::size_of::<T> = 0
to not allocate (use alloc::heap::EMPTY).
@gankro shall we let this sit until EDIT: I am open to doing the rebase and landing now too, up to you. |
Easier to land RawVec first. I hope to post a WIP PR tonight; looks like mostly writing tests now. |
r? @gankro |
This is blocked on #26955 regardless On Wed, Jul 15, 2015 at 10:15 AM, Tamir Duberstein <[email protected]
|
@reem it landed. Should be trivial to implement this on top, now. |
@reem ping |
@gankro @alexcrichton pushed an updated version based on RawVec. |
let raw = ptr::read(&self.data); | ||
mem::forget(self); | ||
raw.into_box() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to factor this out? If it's only going to be used in one place, I favour to keep it inline. Especially since it's unsafe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just felt philosophically that it was part of BoxBuilder's interface and it was strange for the Clone impl to be messing around like this with the internals of the type when it's custom built for that use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly now that you mention it, I'd even hoist the type into the function so no one else can use it.
r=me with nits |
@@ -511,3 +512,53 @@ impl<'a,A,R> FnOnce<A> for Box<FnBox<A,Output=R>+Send+'a> { | |||
} | |||
|
|||
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {} | |||
|
|||
impl<T: Clone> Clone for Box<[T]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I guess we add stability markers to these now
#[stable(feature = "boxed_slice_clone", since = "1.3.0")]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impls are auto-stable anyway so there is no point, iirc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We like to track when impls like this were added in case we do start tracking impl stability one day.
Added a stability marker and hoisted the helper type into the implementation. |
Travis appears to have failed but I don't see why... |
Pretty failed out. Right now the build powers through to the next thing anyway.
Pretty sure features have to be valid identifiers (underscores). |
Updated with |
@bors r+ |
📌 Commit e244230 has been approved by |
⌛ Testing commit e244230 with merge 07e2ce9... |
💔 Test failed - auto-mac-64-nopt-t |
@bors: retry On Tue, Jul 28, 2015 at 10:58 AM, bors [email protected] wrote:
|
This PR should maybe say [breaking-change] in the commit log. Breaking case is that calling This is a minor breaking change. |
Thanks! |
Woo! |
Closes #25097