It appears to me that, for all T
and n
:

[T; n]
andT
have the same alignment 
[T; n]
isn
times as large asT
I checked for i32
, bool
, f64
, even ((), ())
.
But can I rely on this for evil purposes for unsafe code?
use ::std::slice;
fn flatten_3d<T>(x: &[[T; 3]]) > &[T] {
unsafe { slice::from_raw_parts(x.as_ptr() as *const _, x.len() * 3) }
}
fn unflatten_3d<T>(x: &[T]) > &[[T; 3]] {
assert_eq!(x.len() % 3, 0);
unsafe { slice::from_raw_parts(x.as_ptr() as *const _, x.len() / 3) }
}
fn main() {
assert_eq!(unflatten_3d(&[0,1,2,3,4,5]), &[[0,1,2],[3,4,5]]);
assert_eq!(flatten_3d(&[[0,1,2],[3,4,5]]), &[0,1,2,3,4,5]);
}
To elaborate:
I'm trying to find a type suitable for use in public APIs to represent a list of mathematical 3vectors. I will likely need to multiply a matrix against them so I'll probably be using ndarrays at least interally at some point, but they feel a bit too heavy to use in my public interface. On the other hand, &[f64]
(which I know is trivial to wrap as an ndarray) lacks key information from the type.
So I would really like to be able to use &[[f64; 3]]
and Vec<[f64; 3]>
in my interface; but I need to know that I can convert between these and flattened forms or ndarrays with O(1) cost.