Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow slicing to fixed-length arrays #1833
Comments
This comment has been minimized.
This comment has been minimized.
|
So it doesn’t work with the slicing/indexing syntax, but I agree that it would be nice to have some API that does this. However, doing it "right" likely requires type-lever integers. With made-up syntax, it could look like: impl<T> [T] {
pub fn as_array<int N>(&self) -> &[T; N] {
assert_eq!(self.len(), N);
let ptr = self.as_ptr() as *const [T; N];
unsafe { &*ptr }
}
}In the meantime, you can fake it with a macro like the one used in ring.
If the size is fixed for your program that’s even easier. You can use a similar pattern, casting raw pointers: fn to_4x4(array: &[f32; 16]) -> &[[f32; 4]; 4] {
unsafe { &*(array as *const _ as *const _) }
}However this case is more complicated than |
This comment has been minimized.
This comment has been minimized.
|
Ah! Thanks for your solution. Yeah, it would be really handy to have these type-level integers. |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Dec 28, 2016
|
Use the arrayref crate until we get type-level integers or similar. |
torkleyy commentedDec 28, 2016
•
edited
I think it would be nice to do something like that:
Why isn't
yof type[i32; 2]now?I wanted to convert a
[f32; 16]to an[[f32; 4]; 4], however there seems to be no easy way to do that.