I have a newtype:
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Foo(pub u32);
and a vector of them:
let v: Vec<Foo> = Vec::new();
I need a read-only reference to v
as Vec<u32>
.
How can I do this without wastefully making a new vector?
And the reverse?
My current workaround involves creating a new vector:
foo_vec.iter().map(|x| x.0).collect()
u32_vec.iter().map(|x| Foo(*x)).collect()
I assume that the compiler isn't clever enough to optimise these away, even with #[repr(transparent)]
.