Skip to content

Safely access data in PyUntypedArray #477

Open
@flying-sheep

Description

@flying-sheep

PyUntypedArrayMethods::as_array_ptr doesn’t describe under which circumstances the returned pointer is convertible into a reference.

Since the requirements are rather hairy, it would be great to have a way to access the data safely without going through unsafe. Would it e.g. be possible to have this?

trait PyUntypedArrayMethods<'py> {
    ...
    fn bytes<'a>(&'a self) -> &'a [u8] {
        let ptr = self.as_array_ptr();
        let n_bytes = self.len() * self.dtype().itemsize();
        unsafe {
            let p = (*ptr).data.cast();
            slice::from_raw_parts(p, n_bytes)
        }
    }
}

Because we currently implement this ourselves, but without any of the aforementioned safety information, we can’t really be sure if it’s safe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions