Open
Description
See the discussion here:
https://internals.rust-lang.org/t/more-about-slice-array-conversions/12251
I suggest to add two related functions (mutable and not mutable versions) that allows to replace code similar to this:
row.get_mut(8*x .. 8*x + 8).unwrap().try_into().unwrap()
In a shorter, simpler and less bug-prone way, and with only one unwrap (in this specific case there's no need to specify the length as const generic value because it's inferred by the result. In other cases you specify it with a turbofish):
row.array_from_mut(8 * x).unwrap()
The two slice methods could be like (currently they can't be const fn):
pub fn array_from<const LEN: usize>(&self, pos: usize) -> Option<&[T; LEN]> {
if let Some(piece) = self.get(pos .. pos + LEN) {
let temp_ptr: *const [T] = piece;
unsafe {
Some( std::mem::transmute(temp_ptr as *const [T; LEN]) )
}
} else {
None
}
}
pub fn array_from_mut<const LEN: usize>(&mut self, pos: usize) -> Option<&mut [T; LEN]> {
if let Some(piece) = self.get_mut(pos .. pos + LEN) {
let temp_ptr: *mut [T] = piece;
unsafe {
Some( std::mem::transmute(temp_ptr as *mut [T; LEN]) )
}
} else {
None
}
}