MCPcopy Index your code
hub / github.com/PyO3/rust-numpy / as_slice

Method as_slice

src/array.rs:749–764  ·  view source on GitHub ↗

Returns an immutable view of the internal data as a slice. # Safety Calling this method is undefined behaviour if the underlying array is aliased mutably by other instances of `PyArray` or concurrently modified by Python or other native code. Please consider the safe alternative [`PyReadonlyArray::as_slice`].

(&self)

Source from the content-addressed store, hash-verified

747 ///
748 /// Please consider the safe alternative [`PyReadonlyArray::as_slice`].
749 unsafe fn as_slice(&self) -> Result<&[T], AsSliceError>
750 where
751 T: Element,
752 D: Dimension,
753 {
754 let len = self.len();
755 if len == 0 {
756 // We can still produce a slice over zero objects regardless of whether
757 // the underlying pointer is aligned or not.
758 Ok(&[])
759 } else if self.is_aligned() && self.is_contiguous() {
760 Ok(slice::from_raw_parts(self.data(), len))
761 } else {
762 Err(AsSliceError)
763 }
764 }
765
766 /// Returns a mutable view of the internal data as a slice.
767 ///

Callers 1

to_vecMethod · 0.45

Implementers 1

array.rssrc/array.rs

Calls 4

lenMethod · 0.80
is_alignedMethod · 0.80
is_contiguousMethod · 0.80
dataMethod · 0.80

Tested by

no test coverage detected