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

Way to create a Rc<[T]> from a Vec<T> #1979

Closed
arielb1 opened this issue Apr 20, 2017 · 10 comments
Closed

Way to create a Rc<[T]> from a Vec<T> #1979

arielb1 opened this issue Apr 20, 2017 · 10 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@arielb1
Copy link
Contributor

arielb1 commented Apr 20, 2017

It should be possible to create an Rc<[T]> from a Vec<T>, but that is not possible right now without heavy use of transmute (which I'm sure some people are doing).

@eddyb
Copy link
Member

eddyb commented Apr 20, 2017

See #1138

@Diggsey
Copy link
Contributor

Diggsey commented Apr 20, 2017

I hope people are not using transmute, because you can't safely transmute one to the other without resizing the allocation to make room for the reference counts.

@le-jzr
Copy link

le-jzr commented Apr 20, 2017

We can already convert Vec<T> to Box<[T]>, so this would be easily solved with rust-lang/rust#29953

@le-jzr
Copy link

le-jzr commented Apr 20, 2017

Although I'm curious, why would anyone use unsafe code, or even transmute, instead of just using Rc<Vec<T>>? This is not a rhetorical question.

@est31
Copy link
Member

est31 commented Apr 21, 2017

This comment would also be relevant: rust-lang/rust#29953 (comment)

Vec<T>←→ Box<[T]> works because the allocated memory is of the same size, with Rc<T> its different, and would require re-allocation and most likely also copying (some system allocators allow you to try to change the size of your allocation), which would make this non-O(1).

@burdges
Copy link

burdges commented Apr 21, 2017

You could create new RcVec<T> and ArcVec<T> types with zero reallocation conversions to/from Box<Rc<[T]>> and Box<Arc<[T]>>.

@eddyb
Copy link
Member

eddyb commented Apr 21, 2017

@burdges An universal way to do that is #1138.

@le-jzr
Copy link

le-jzr commented Apr 21, 2017

I suspect the usual use case would be that the code would build the array in Vec<T> and then convert it to Rc<[T]> for the consumer. Building it would be O(n) regardless, and the memcpy will likely matter much less than the work expended on creating it. As long as it's documented as such, I don't see a problem with it.

@le-jzr
Copy link

le-jzr commented Apr 21, 2017

Does any code actually do this conversion at the moment? Even if allocator magic as proposed by @eddyb is accepted, it will take a lot of time to land, but experiments like RcVec<T> can be realized right this afternoon as a 3rd party crate.

@withoutboats withoutboats added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label May 15, 2017
@Mark-Simulacrum
Copy link
Member

This has been accepted in RFC 1845 -- https://github.com/rust-lang/rfcs/blob/master/text/1845-shared-from-slice.md, tracking issue rust-lang/rust#40475.

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 RFC.
Projects
None yet
Development

No branches or pull requests

8 participants