Stream: wasmtime

Topic: moving elements into slices


view this post on Zulip Dan Gohman (May 21 2020 at 20:53):

if I have a Vec<Val> that I can consume, can I move the elements into a &mut [Val]?

view this post on Zulip fitzgen (he/him) (May 21 2020 at 20:55):

like you have dst: &mut [Val] and src: Vec<Val>?

if so, then {copy,clone}_from_slice should do the trick

view this post on Zulip Alex Crichton (May 21 2020 at 20:55):

@Dan Gohman for (slot, val) in slots.iter_mut().zip(values) { *slot = val; } I think?

view this post on Zulip Alex Crichton (May 21 2020 at 20:56):

oh, do what fitzgen says

view this post on Zulip Dan Gohman (May 21 2020 at 20:57):

ah, I guess I was hoping to avoid copy/clone since the old copy is going away anyway, but I can do that

view this post on Zulip Chris Fallin (May 21 2020 at 20:57):

if you want to avoid the clone and Val's type has a cheap default (e.g. it's a nested Vec or something) you might also be able to use mem::replace

view this post on Zulip Dan Gohman (May 21 2020 at 20:57):

I tried Alex's thing, changing it to values.into_iter(), but it doesn't like that: cannot move out of *result which is behind a shared reference

view this post on Zulip Chris Fallin (May 21 2020 at 20:58):

in the worst case you could always do some hackery with unsafe, a memcpy, and mem::forget

view this post on Zulip fitzgen (he/him) (May 21 2020 at 20:58):

@Dan Gohman can you share the snippet with a tiny bit more context?

view this post on Zulip Chris Fallin (May 21 2020 at 20:58):

though the actual Rust people here will please toss me overboard if that has caveats (Pin would break I think?)

view this post on Zulip Dan Gohman (May 21 2020 at 21:01):

ok, heh, tried to make a reduced case of this and it compiled without error. let me see what's different...

view this post on Zulip fitzgen (he/him) (May 21 2020 at 21:01):

are you destructuring out of a type that has a Drop implementation, by chance?

view this post on Zulip Dan Gohman (May 21 2020 at 21:02):

cannot move out of *command_result which is behind a shared referenc

view this post on Zulip Dan Gohman (May 21 2020 at 21:03):

oops

view this post on Zulip Dan Gohman (May 21 2020 at 21:03):

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e4c8cd5c2bddfdee4538c48615b6a111

view this post on Zulip Dan Gohman (May 21 2020 at 21:03):

It looks like I can do it if I have a Vec, but I actually have a Box<[T]>

view this post on Zulip Alex Crichton (May 21 2020 at 21:04):

@Dan Gohman Vec::from(ins)

view this post on Zulip Alex Crichton (May 21 2020 at 21:04):

in that you can create a Vec from a Box<[T]> w/ a zero-cost cast

view this post on Zulip fitzgen (he/him) (May 21 2020 at 21:05):

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6aec638adb3b577ec34ece67d4d4fc5e

view this post on Zulip Dan Gohman (May 21 2020 at 21:06):

oh, cool

view this post on Zulip Dan Gohman (May 21 2020 at 21:06):

That works!

view this post on Zulip fitzgen (he/him) (May 21 2020 at 21:07):

I think what was happening is that Box<[T]> doesn't implement IntoIter, and it just derefs to the slice's IntoIter; would need specialization to special case Box<[T]> over Box<T> for choosing an IntoIter, which I'm not sure std ever does at the public level (although definitely internally uses specialization)

maybe you can clarify, @Alex Crichton

view this post on Zulip Alex Crichton (May 21 2020 at 21:08):

that sounds right yeah


Last updated: Dec 23 2024 at 13:07 UTC