Skip to content
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

liballoc: implement From for Box, Rc, Arc #29580

Merged
merged 3 commits into from Nov 16, 2015

Conversation

alexbool
Copy link
Contributor

@alexbool alexbool commented Nov 4, 2015

Sometimes when writing generic code you want to abstract over
owning/pointer type so that calling code isn't restricted by one
concrete owning/pointer type. This commit makes possible such code:

fn i_will_work_with_arc<T: Into<Arc<MyTy>>>(t: T) {
    let the_arc = t.into();
    // Do something
}

i_will_work_with_arc(MyTy::new());

i_will_work_with_arc(Box::new(MyTy::new()));

let arc_that_i_already_have = Arc::new(MyTy::new());
i_will_work_with_arc(arc_that_i_already_have);

Please note that this patch doesn't work with DSTs.
Also to mention, I made those impls stable, and I don't know whether they should be actually stable from the beginning. Please tell me if this should be feature-gated.

Sometimes when writing generic code you want to abstract over
owning/pointer type so that calling code isn't restricted by one
concrete owning/pointer type. This commit makes possible such code:
```
fn i_will_work_with_arc<T: Into<Arc<MyTy>>>(t: T) {
    let the_arc = t.into();
    // Do something
}

i_will_work_with_arc(MyTy::new());

i_will_work_with_arc(Box::new(MyTy::new()));

let arc_that_i_already_have = Arc::new(MyTy::new());
i_will_work_with_arc(arc_that_i_already_have);
```

Please note that this patch doesn't work with DSTs.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@alexcrichton
Copy link
Member

Thanks for the PR! Unfortunately there's now way to have an impl of a trait be unstable, and as these are for some particularly "core types" it's a weighty addition! As a result I'm going to tag this with T-libs so this comes up during the normal triage for the library subteam.

I would personally want to eschew the From<Box<T>> for Arc<T> impls for now, but the others seem relatively OK to me.

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Nov 4, 2015
@alexbool
Copy link
Contributor Author

alexbool commented Nov 4, 2015

It was also suprising for me to find out that there is no 'legitimate' way of conversion unsized data from one smart pointer to another (or maybe I just did not find it?), because from the naive point of view this is kind of simplest operation (move the pointer and that's it). Even for sized types, if I understood the code correctly, in impl From<Box<T>> for Arc<T> it is copying the data itself (not the pointer) to the new location. The consequence is that library authors stick with things like Arc<Box<TraitObject>> which is one more (unnecessary) indirection level. Is it 'OK by design' or maybe there are plans to fix it?

@bluss
Copy link
Member

bluss commented Nov 4, 2015

@alexbool There is no free way to move from Box<T> to Rc<T>; both point to a single allocation, but the size of it differs between Box and Rc (both for regular T and for unsized T). Arc<T> and Rc<T> use the same size allocation, but I don't think that's something we want to cement by exposing it. (And maybe there are quirks I'm missing that mean they are not the same size).

@alexcrichton
Copy link
Member

To add to what @bluss said, I think situations like Arc<Box<Trait>> come up because Arc<Trait> was not supported awhile back (but it now is). Transitioning from A<Trait> to B<Trait> is generally not possible (as @bluss mentioned) to do so easily, unfortunately.

@alexbool
Copy link
Contributor Author

alexbool commented Nov 5, 2015

Ah yeah, I see that (A)rc is basically a pointer to a struct containing refcounts AND plain data. In my naive world it would be rather a tuple (pointer to refcounts, pointer to data) and in that case, I guess, consuming a Box into refrence-counted pointer would be trivial. But at the same time it would lead to some downsides (more values to pass between functions, more mallocs, maybe worse cache locality etc), so it is trade-off. Another question, how is it implemented in C++'s shared_ptr? Thanks @alexcrichton @bluss for comments!

@petrochenkov
Copy link
Contributor

@alexbool

Another question, how is it implemented in C++'s shared_ptr?

C++ supports creation of shared pointers from raw pointers, so shared_ptr should be able to support two allocations and normally keeps two pointers (example) - one for the data and one for the control block. As an optimization, these two allocations can be fused together if the pointer was created with make_shared. Rust doesn't support two allocation mode, so A(Rc) can't adopt stray pointers.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 6, 2015

Rust doesn't support two allocation mode, so A(Rc) can't adopt stray pointers.

You can manually allow the two-allocation mode for specific traits by implementing the trait for Box<Trait>

use std::rc::Rc;

trait A {
    fn blub(&self) { println!("blub") }
}

struct X;

impl A for X {
    fn blub(&self) { println!("X") }
}

impl<T: A + ?Sized> A for Box<T> {
    fn blub(&self) { (**self).blub() }
}

fn main() {
    let x = Box::new(X);
    let y: Box<A> = x;
    let z: Rc<A> = Rc::new(y);
    z.blub();
}

@alexcrichton
Copy link
Member

The libs team discussed this during triage today and we're fine merging this, although could you remove the From<Box<T>> for Rc<T> and for Arc as well? It should be possible to add those later and for now we feel it's a bit niche to have those included.

@alexbool
Copy link
Contributor Author

Done

@alexbool
Copy link
Contributor Author

Oops, there are some errors from make-tidy.
Do I understand this correctly that I should remove #[stable] attributes?

@@ -894,6 +895,13 @@ impl<T: ?Sized + Hash> Hash for Arc<T> {
}
}

#[stable(feature = "rust1", since = "1.6.0")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah these attributes are fine, but they'll need a new feature name as rust1 is already taken. Perhaps something like from_for_ptrs or something like that?

@alexbool
Copy link
Contributor Author

Fixed

@alexcrichton
Copy link
Member

@bors: r+ 67c07d4

@bors
Copy link
Contributor

bors commented Nov 16, 2015

⌛ Testing commit 67c07d4 with merge 99093b7...

bors added a commit that referenced this pull request Nov 16, 2015
Sometimes when writing generic code you want to abstract over
owning/pointer type so that calling code isn't restricted by one
concrete owning/pointer type. This commit makes possible such code:
```rust
fn i_will_work_with_arc<T: Into<Arc<MyTy>>>(t: T) {
    let the_arc = t.into();
    // Do something
}

i_will_work_with_arc(MyTy::new());

i_will_work_with_arc(Box::new(MyTy::new()));

let arc_that_i_already_have = Arc::new(MyTy::new());
i_will_work_with_arc(arc_that_i_already_have);
```

Please note that this patch doesn't work with DSTs.
Also to mention, I made those impls stable, and I don't know whether they should be actually stable from the beginning. Please tell me if this should be feature-gated.
@bors bors merged commit 67c07d4 into rust-lang:master Nov 16, 2015
@bstrie
Copy link
Contributor

bstrie commented Nov 24, 2015

@alexbool I see corresponding tests for Rc and Arc, but not for Box. Is this an oversight?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants