-
Notifications
You must be signed in to change notification settings - Fork 18
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
Returning split up arrays by value #5
Comments
On first thought (and I haven't been using rust in a while), I would think that you could do this with minimal overhead using array_ref! and a copy. Something like let my_copy = *array_ref![data,5,2]; ought to create a copy of a portion of the array on the stack. Do you have a concern that this might be inefficient? Or is there something more expressive that you are looking for? |
I donno if efficiency matters too much. If you're returning the array by value, then it's probably not such a big array. Agreed, thanks!. It's much cleaner to write the following instead of a
I kinda liked that the |
I still feel |
There are situations where you want to split an array and return it by value on the stack. If you trust LLVM to remove a duplicate copy, then the
clone_into_array
function described here works.There is a nice unsafe solution that works by building the type you want to return as a tuple and unsafely treating it as a single array :
I'm curious how one should make this safer?
An obstacle is that often one must apply
mem::transmute
to a reference, but one wants to check that the referenced types have the same size, not just that two pointers have the same size.We could use two wrappers for
mem::transmute
that check the type sizes in debug mode, like so :I think this becomes
transmute_ptr<'l,A,B>(v: &l' A) -> &l' B
and similarly for the_mut
variant, so they reborrowv
. As a result, you might need to wrap them inside another scope, well until rust-lang/rfcs#811 or similar.In principle, one might give the compiler more ammunition to analyze the borrow by using macros, like :
Any thoughts on the best way to do this sort of thing?
I asked about this on stackexchange, but actually this crate seems like the better place, as maybe there is something worth adding.
The text was updated successfully, but these errors were encountered: