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)
| 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 | /// |