Skip to content

Implement PackedArray::as_slice #306

Closed
@MatrixDev

Description

@MatrixDev
// 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) }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: coreCore componentsfeatureAdds functionality to the library

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions