bytemuck's try_cast_vec for Vec<u8>->Vec<u16> or any different alignment will fail, regardless of whether the allocation is aligned. It is undefined behaviour in rust to deallocate with a different alignment than the allocation. So this code will always take the second path. That could be changed to
Ok(TypedArray::UInt16(
try_cast_slice(data[..])
.map(|s| s.to_vec()) // this copy already always happens
.unwrap_or_else(
|(_, data)| {
// Fallback to manual conversion when not aligned
data.chunks_exact(2)
.map(|b| u16::from_ne_bytes([b[0], b[1]]))
.collect()
},
)))
which will go the first path if the allocation is aligned.
Alternatively, this copy could be avoided with some notes in #203
bytemuck's
try_cast_vecforVec<u8>->Vec<u16>or any different alignment will fail, regardless of whether the allocation is aligned. It is undefined behaviour in rust to deallocate with a different alignment than the allocation. So this code will always take the second path. That could be changed towhich will go the first path if the allocation is aligned.
Alternatively, this copy could be avoided with some notes in #203