pub struct PyUntypedBuffer(/* private fields */);Expand description
Allows access to the underlying buffer used by a python object such as bytes, bytearray or array.array.
Implementations§
Source§impl PyUntypedBuffer
impl PyUntypedBuffer
Sourcepub fn get(obj: &Bound<'_, PyAny>) -> PyResult<Self>
pub fn get(obj: &Bound<'_, PyAny>) -> PyResult<Self>
Gets the underlying buffer from the specified python object.
Sourcepub fn into_typed<T: Element>(self) -> PyResult<PyBuffer<T>>
pub fn into_typed<T: Element>(self) -> PyResult<PyBuffer<T>>
Returns a [PyBuffer] instance if the buffer can be interpreted as containing elements of type T.
Sourcepub fn as_typed<T: Element>(&self) -> PyResult<&PyBuffer<T>>
pub fn as_typed<T: Element>(&self) -> PyResult<&PyBuffer<T>>
Non-owning equivalent of into_typed().
Sourcepub fn release(self, _py: Python<'_>)
pub fn release(self, _py: Python<'_>)
Releases the buffer object, freeing the reference to the Python object which owns the buffer.
This will automatically be called on drop.
Sourcepub fn buf_ptr(&self) -> *mut c_void
pub fn buf_ptr(&self) -> *mut c_void
Gets the pointer to the start of the buffer memory.
Warning: the buffer memory can be mutated by other code (including other Python functions, if the GIL is released, or other extension modules even if the GIL is held). You must either access memory atomically, or ensure there are no data races yourself. See this blog post for more details.
Sourcepub fn get_ptr(&self, indices: &[usize]) -> *mut c_void
pub fn get_ptr(&self, indices: &[usize]) -> *mut c_void
Gets a pointer to the specified item.
If indices.len() < self.dimensions(), returns the start address of the sub-array at the specified dimension.
Sourcepub fn item_size(&self) -> usize
pub fn item_size(&self) -> usize
Gets the size of a single element, in bytes. Important exception: when requesting an unformatted buffer, item_size still has the value
Sourcepub fn item_count(&self) -> usize
pub fn item_count(&self) -> usize
Gets the total number of items.
Sourcepub fn len_bytes(&self) -> usize
pub fn len_bytes(&self) -> usize
item_size() * item_count().
For contiguous arrays, this is the length of the underlying memory block.
For non-contiguous arrays, it is the length that the logical structure would have if it were copied to a contiguous representation.
Sourcepub fn dimensions(&self) -> usize
pub fn dimensions(&self) -> usize
Gets the number of dimensions.
May be 0 to indicate a single scalar value.
Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Returns an array of length dimensions. shape()[i] is the length of the array in dimension number i.
May return None for single-dimensional arrays or scalar values (dimensions() <= 1);
You can call item_count() to get the length of the single dimension.
Despite Python using an array of signed integers, the values are guaranteed to be non-negative. However, dimensions of length 0 are possible and might need special attention.
Sourcepub fn strides(&self) -> &[isize]
pub fn strides(&self) -> &[isize]
Returns an array that holds, for each dimension, the number of bytes to skip to get to the next element in the dimension.
Stride values can be any integer. For regular arrays, strides are usually positive,
but a consumer MUST be able to handle the case strides[n] <= 0.
Sourcepub fn suboffsets(&self) -> Option<&[isize]>
pub fn suboffsets(&self) -> Option<&[isize]>
An array of length ndim.
If suboffsets[n] >= 0, the values stored along the nth dimension are pointers and the suboffset value dictates how many bytes to add to each pointer after de-referencing.
A suboffset value that is negative indicates that no de-referencing should occur (striding in a contiguous memory block).
If all suboffsets are negative (i.e. no de-referencing is needed), then this field must be NULL (the default value).
Sourcepub fn format(&self) -> &CStr
pub fn format(&self) -> &CStr
A string in struct module style syntax describing the contents of a single item.
Sourcepub fn is_c_contiguous(&self) -> bool
pub fn is_c_contiguous(&self) -> bool
Gets whether the buffer is contiguous in C-style order (last index varies fastest when visiting items in order of memory address).
Sourcepub fn is_fortran_contiguous(&self) -> bool
pub fn is_fortran_contiguous(&self) -> bool
Gets whether the buffer is contiguous in Fortran-style order (first index varies fastest when visiting items in order of memory address).
Trait Implementations§
Source§impl Debug for PyUntypedBuffer
impl Debug for PyUntypedBuffer
Source§impl Drop for PyUntypedBuffer
impl Drop for PyUntypedBuffer
impl Send for PyUntypedBuffer
impl Sync for PyUntypedBuffer
Auto Trait Implementations§
impl Freeze for PyUntypedBuffer
impl RefUnwindSafe for PyUntypedBuffer
impl Unpin for PyUntypedBuffer
impl UnwindSafe for PyUntypedBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more