Closed
Description
// SAFETY: Packed arrays are stored contiguously in memory, so we can use
// pointer arithmetic instead of going through `$operator_index_const` for
// every index.
If PackedArrays are stored contiguously in memory, it should be possible to return a slice instead of copying all data to the vec. It seams like a guite significant overhead especially when working with Image
.
Unfortunately, currently it is impossible to implement as an extension trait because PackedArray::ptr
and PackedArray::ptr_mut
are private.
Proposed implementation is quite simple:
pub fn as_slice(&self) -> &[$Element] {
let len = self.len();
let ptr = self.ptr(0);
unsafe { std::slice::from_raw_parts(ptr, len) }
}
pub fn as_mut_slice(&self) -> &mut [$Element] {
let len = self.len();
let ptr = self.ptr_mut(0);
unsafe { std::slice::from_raw_parts_mut(ptr, len) }
}