Conversion from &[T] to &[VectorN<T, D>]


#1

Hey,

So I’m working with slices of VectorN<T, D> but I’d like to be able to handle more ‘raw’ inputs like slices of T.
Is there any easy way to convert &[T] to &[VectorN<T,D>] ? (without copying the data).
I’ve managed to do a function that does it but it’s safety heavily relies on internal storage details of VectorN which I’m not aware of:

fn raw_slice_to_vector_slice<T, D>(raw: &[T]) -> &[VectorN<T,D>]
where
    D: DimName,
    DefaultAllocator: Allocator<T, D>,
{
    let dim = D::dim();
    let len = raw.len();
    assert_eq!(len % dim, 0);
    let ptr = raw.as_ptr();
    unsafe { std::slice::from_raw_parts(ptr as *const VectorN<T, D>, len / dim) }
}

#2

Hi!

There is no builtin way to perform this kind of conversion.
The code you propose is the right way of doing it! Because this relies so heavily on the internal storage details, it would be safer to add this to nalgebra itself (that way it would be our responsibility to keep this piece of code up-to-date if we ever change the internal layout). There is no reason for this layout to change at any time in the future though.