Skip to content

Commit 542ebb9

Browse files
committed
Make the internal PyArray::data function safe
While doing anything with the resulting pointer requires unsafe, producing the pointer should not. (Dereferencing `self.as_array_ptr()` should be safe for the same reasons that dereferencing it in say `PyArray::shape` is, i.e. we ensure that `PyArray<T,D>` is only constructed for NumPy arrays.)
1 parent fff48b8 commit 542ebb9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ impl<T, D> PyArray<T, D> {
340340
}
341341

342342
/// Returns the pointer to the first element of the inner array.
343-
pub(crate) unsafe fn data(&self) -> *mut T {
343+
pub(crate) fn data(&self) -> *mut T {
344344
let ptr = self.as_array_ptr();
345-
(*ptr).data as *mut _
345+
unsafe { (*ptr).data as *mut _ }
346346
}
347347
}
348348

@@ -381,7 +381,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
381381
let strides = self.strides();
382382

383383
let mut new_strides = D::zeros(strides.len());
384-
let mut data_ptr = unsafe { self.data() };
384+
let mut data_ptr = self.data();
385385
let mut inverted_axes = InvertedAxes::new(strides.len());
386386

387387
for i in 0..strides.len() {

0 commit comments

Comments
 (0)