Open
Description
Would it be possible to add a trait to downcast &[Vector<T>]
to &[[T; N]]
and &[T]
?
I work with buffers of vectors, and I need to pass them to other libraries, that are using &[T]
for compatibility. For now I only copy it element by element, but I think that copy could be avoided with a simple reference cast.
For example, something like that
impl Into<&[T]> for &[Vector3<T>] {
fn into(self) -> &[T] {
unsafe { std::slice::from_raw_parts(self.as_ptr() as *const [T], self.len() * 3) }
}
}
and maybe also the inverse
impl Into<&[Vector<T>]> for &[T] {
fn into(self) -> &[Vector<T>] {
assert_eq!(self.len() % 3, 0);
unsafe { std::slice::from_raw_parts(self.as_ptr() as *const [Vector3<T>], self.len() / 3) }
}
}
I think it's possible because the Vector
struct is marked repr(C)
and its fields are all of the same type (so there is no alignment concerns). Plus all slices are contiguous data storages
This kind of cast would get it much more simple to get from a buffer type to an other. (slices, vec, ndarray, with grouped components or not).
Metadata
Metadata
Assignees
Labels
No labels