Skip to main content

PyBufferView

Struct PyBufferView 

Source
pub struct PyBufferView<T, Flags: PyBufferFlagsType = PyBufferFlags<true, true, true>>(/* private fields */);
Expand description

A typed form of PyUntypedBufferView. Not constructible directly — use PyBufferView::with() or PyBufferView::with_flags().

Implementations§

Source§

impl<T: Element> PyBufferView<T>

Source

pub fn with<R>( obj: &Bound<'_, PyAny>, f: impl FnOnce(&PyBufferView<T>) -> R, ) -> PyResult<R>

Acquire a typed buffer view with PyBufferFlags::full_ro() flags, validating that the buffer format is compatible with T.

Source

pub fn with_flags<const FORMAT: bool, const SHAPE: bool, const STRIDE: bool, const WRITABLE: bool, const CONTIGUITY: u8, R>( obj: &Bound<'_, PyAny>, flags: PyBufferFlags<FORMAT, SHAPE, STRIDE, WRITABLE, CONTIGUITY>, f: impl FnOnce(&PyBufferView<T, PyBufferFlags<true, SHAPE, STRIDE, WRITABLE, CONTIGUITY>>) -> R, ) -> PyResult<R>

Acquire a typed buffer view with the given flags.

ffi::PyBUF_FORMAT is implicitly added for type validation.

Source§

impl<T: Element, Flags: PyBufferFlagsType> PyBufferView<T, Flags>

Source

pub fn as_slice<'a>(&'a self, _py: Python<'a>) -> Option<&'a [ReadOnlyCell<T>]>

Gets the buffer memory as a slice.

Returns None if the buffer is not C-contiguous.

The returned slice uses type ReadOnlyCell<T> because it’s theoretically possible for any call into the Python runtime to modify the values in the slice.

Source

pub fn as_mut_slice<'a>(&'a self, _py: Python<'a>) -> Option<&'a [Cell<T>]>

Gets the buffer memory as a mutable slice.

Returns None if the buffer is read-only or not C-contiguous.

The returned slice uses type Cell<T> because it’s theoretically possible for any call into the Python runtime to modify the values in the slice.

Source§

impl<T: Element, const FORMAT: bool, const SHAPE: bool, const STRIDE: bool, const WRITABLE: bool> PyBufferView<T, PyBufferFlags<FORMAT, SHAPE, STRIDE, WRITABLE, CONTIGUITY_C>>

Source

pub fn as_contiguous_slice<'a>( &'a self, _py: Python<'a>, ) -> &'a [ReadOnlyCell<T>]

Gets the buffer memory as a slice. The buffer is guaranteed C-contiguous.

Source§

impl<T: Element, const FORMAT: bool, const SHAPE: bool, const STRIDE: bool> PyBufferView<T, PyBufferFlags<FORMAT, SHAPE, STRIDE, true, CONTIGUITY_C>>

Source

pub fn as_contiguous_mut_slice<'a>(&'a self, _py: Python<'a>) -> &'a [Cell<T>]

Gets the buffer memory as a mutable slice. The buffer is guaranteed C-contiguous and writable.

Source§

impl<T: Element, const FORMAT: bool, const SHAPE: bool, const STRIDE: bool, const WRITABLE: bool> PyBufferView<T, PyBufferFlags<FORMAT, SHAPE, STRIDE, WRITABLE, CONTIGUITY_F>>

Source

pub fn as_fortran_contiguous_slice<'a>( &'a self, _py: Python<'a>, ) -> &'a [ReadOnlyCell<T>]

Gets the buffer memory as a slice. The buffer is guaranteed Fortran-contiguous.

Source§

impl<T: Element, const FORMAT: bool, const SHAPE: bool, const STRIDE: bool> PyBufferView<T, PyBufferFlags<FORMAT, SHAPE, STRIDE, true, CONTIGUITY_F>>

Source

pub fn as_fortran_contiguous_mut_slice<'a>( &'a self, _py: Python<'a>, ) -> &'a [Cell<T>]

Gets the buffer memory as a mutable slice. The buffer is guaranteed Fortran-contiguous and writable.

Methods from Deref<Target = PyUntypedBufferView<Flags>>§

Source

pub fn buf_ptr(&self) -> *mut c_void

Gets the pointer to the start of the buffer memory.

Source

pub fn obj<'py>(&self, py: Python<'py>) -> Option<&Bound<'py, PyAny>>

Returns the Python object that owns the buffer data.

Source

pub fn readonly(&self) -> bool

Gets whether the underlying buffer is read-only.

Source

pub fn item_size(&self) -> usize

Gets the size of a single element, in bytes.

Source

pub fn item_count(&self) -> usize

Gets the total number of items.

Source

pub fn len_bytes(&self) -> usize

item_size() * item_count(). For contiguous arrays, this is the length of the underlying memory block.

Source

pub fn dimensions(&self) -> usize

Gets the number of dimensions.

May be 0 to indicate a single scalar value.

Source

pub fn suboffsets(&self) -> Option<&[isize]>

Returns the suboffsets array.

May return None even with PyBUF_INDIRECT if the exporter sets suboffsets to NULL.

Source

pub fn is_c_contiguous(&self) -> bool

Gets whether the buffer is contiguous in C-style order.

Source

pub fn is_fortran_contiguous(&self) -> bool

Gets whether the buffer is contiguous in Fortran-style order.

Trait Implementations§

Source§

impl<T, Flags: PyBufferFlagsType> Debug for PyBufferView<T, Flags>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, Flags: PyBufferFlagsType> Deref for PyBufferView<T, Flags>

Source§

type Target = PyUntypedBufferView<Flags>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T, Flags> Freeze for PyBufferView<T, Flags>

§

impl<T, Flags> RefUnwindSafe for PyBufferView<T, Flags>
where Flags: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, Flags = PyBufferFlags<true, true, true>> !Send for PyBufferView<T, Flags>

§

impl<T, Flags = PyBufferFlags<true, true, true>> !Sync for PyBufferView<T, Flags>

§

impl<T, Flags> Unpin for PyBufferView<T, Flags>
where Flags: Unpin, T: Unpin,

§

impl<T, Flags> UnsafeUnpin for PyBufferView<T, Flags>

§

impl<T, Flags> UnwindSafe for PyBufferView<T, Flags>
where Flags: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.