Skip to content

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

Merged
merged 1 commit into from
Jul 28, 2015

Conversation

reem
Copy link
Contributor

@reem reem commented Jul 10, 2015

Closes #25097

@rust-highfive
Copy link
Contributor

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

unsafe { mem::transmute(Slice {
data: data,
len: self.len()
}) }
Copy link
Member

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.

@alexcrichton
Copy link
Member

Implementation looks good to me! Just some stylistic nits here and there.

cc @gankro

@Gankra
Copy link
Contributor

Gankra commented Jul 10, 2015

Uhhhhh

Why not just (&self[..]).to_owned().into_boxed_slice()

@Gankra
Copy link
Contributor

Gankra commented Jul 10, 2015

Ah just read the issue. Sucks.

@Gankra
Copy link
Contributor

Gankra commented Jul 10, 2015

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 into_box/from_box methods that would make this simple. That seems like a cleaner way forward.

@bors
Copy link
Collaborator

bors commented Jul 10, 2015

☔ 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(),
Copy link
Contributor

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).

@reem
Copy link
Contributor Author

reem commented Jul 10, 2015

@gankro shall we let this sit until RawVec lands then? It already needs a rebase because of CString changes.

EDIT: I am open to doing the rebase and landing now too, up to you.

@Gankra
Copy link
Contributor

Gankra commented Jul 10, 2015

Easier to land RawVec first. I hope to post a WIP PR tonight; looks like mostly writing tests now.

@alexcrichton
Copy link
Member

r? @gankro

@rust-highfive rust-highfive assigned Gankra and unassigned alexcrichton Jul 15, 2015
@Gankra
Copy link
Contributor

Gankra commented Jul 15, 2015

This is blocked on #26955 regardless

On Wed, Jul 15, 2015 at 10:15 AM, Tamir Duberstein <[email protected]

wrote:

@alexcrichton https://github.com/alexcrichton merge conflict


Reply to this email directly or view it on GitHub
#26934 (comment).

@Gankra
Copy link
Contributor

Gankra commented Jul 18, 2015

@reem it landed. Should be trivial to implement this on top, now.

@Gankra
Copy link
Contributor

Gankra commented Jul 27, 2015

@reem ping

@reem reem force-pushed the boxed-slice-clone branch from b77f4de to 0c15fca Compare July 27, 2015 05:12
@reem
Copy link
Contributor Author

reem commented Jul 27, 2015

@gankro @alexcrichton pushed an updated version based on RawVec.

let raw = ptr::read(&self.data);
mem::forget(self);
raw.into_box()
}
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@Gankra
Copy link
Contributor

Gankra commented Jul 27, 2015

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]> {
Copy link
Contributor

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")]

Copy link
Contributor Author

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

Copy link
Member

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.

@reem reem force-pushed the boxed-slice-clone branch from 0c15fca to 94a02d2 Compare July 28, 2015 04:24
@reem
Copy link
Contributor Author

reem commented Jul 28, 2015

Added a stability marker and hoisted the helper type into the implementation.

@reem
Copy link
Contributor Author

reem commented Jul 28, 2015

Travis appears to have failed but I don't see why...

@Gankra
Copy link
Contributor

Gankra commented Jul 28, 2015

Pretty failed out. Right now the build powers through to the next thing anyway.

check: feature sanity

error: misformed stability attribute

line 519 of /home/travis/build/rust-lang/rust/src/liballoc/boxed.rs:

#[stable(feature = "box-slice-clone", since = "1.3.0")]

Pretty sure features have to be valid identifiers (underscores).

@reem reem force-pushed the boxed-slice-clone branch from 94a02d2 to e244230 Compare July 28, 2015 08:43
@reem
Copy link
Contributor Author

reem commented Jul 28, 2015

Updated with box_slice_clone.

@Gankra
Copy link
Contributor

Gankra commented Jul 28, 2015

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 28, 2015

📌 Commit e244230 has been approved by Gankro

@bors
Copy link
Collaborator

bors commented Jul 28, 2015

⌛ Testing commit e244230 with merge 07e2ce9...

@bors
Copy link
Collaborator

bors commented Jul 28, 2015

💔 Test failed - auto-mac-64-nopt-t

@alexcrichton
Copy link
Member

@bors: retry

On Tue, Jul 28, 2015 at 10:58 AM, bors [email protected] wrote:

[image: 💔] Test failed - auto-mac-64-nopt-t
http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/5910


Reply to this email directly or view it on GitHub
#26934 (comment).

@bluss bluss added the relnotes Marks issues that should be documented in the release notes of the next release. label Jul 28, 2015
@bluss
Copy link
Member

bluss commented Jul 28, 2015

This PR should maybe say [breaking-change] in the commit log.

Breaking case is that calling .to_owned() on Box<[T]> where T: Clone today produces Vec<T>, I think it would produce Box<[T]> with this PR. I also examined .clone(), but that is invalid on current Box<[T]> (does not produce &[T] as I had feared).

This is a minor breaking change.

@bors
Copy link
Collaborator

bors commented Jul 28, 2015

⌛ Testing commit e244230 with merge ba9224f...

bors added a commit that referenced this pull request Jul 28, 2015
@bors bors merged commit e244230 into rust-lang:master Jul 28, 2015
@Gankra
Copy link
Contributor

Gankra commented Jul 28, 2015

Thanks!

@reem
Copy link
Contributor Author

reem commented Jul 29, 2015

Woo!

@reem reem deleted the boxed-slice-clone branch July 29, 2015 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants