bytemuck icon indicating copy to clipboard operation
bytemuck copied to clipboard

Good Idea: safe way to transmute `Vec<Option<&Foo>>` to `Vec<&Foo>`

Open Lokathor opened this issue 1 year ago • 2 comments

https://www.reddit.com/r/rust/comments/1ez6f2c/any_safe_way_to_transmute_vecoptionfoo_to_vecfoo/


So yeah, what we want is to be able to convert from Option<&i32> to &i32, particularly within a vec/slice/container.

There's lots of types with guaranteed option representation, so we probably want to use some sort of trait.

Lokathor avatar Aug 23 '24 07:08 Lokathor

Isn't this only possible going from &[&Foo] to &[Option<&Foo>] and not the other way around? Or do you mean that in the owned Vec case, we should check all values are Some before doing the transmute?

fu5ha avatar Jun 09 '25 17:06 fu5ha

The latter case, we'd do a check then convert if it's valid.

Lokathor avatar Jun 09 '25 17:06 Lokathor