Skip to content

Commit c454a5e

Browse files
committed
Replace Element::DATA_TYPE with IS_COPY
1 parent cf25192 commit c454a5e

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
use std::{iter::ExactSizeIterator, marker::PhantomData};
1616

1717
use crate::convert::{ArrayExt, IntoPyArray, NpyIndex, ToNpyDims, ToPyArray};
18-
use crate::dtype::{DataType, Element};
18+
use crate::dtype::Element;
1919
use crate::error::{DimensionalityError, FromVecError, NotContiguousError, TypeError};
2020
use crate::slice_container::PySliceContainer;
2121

@@ -852,7 +852,7 @@ impl<T: Element> PyArray<T, Ix1> {
852852
pub fn from_slice<'py>(py: Python<'py>, slice: &[T]) -> &'py Self {
853853
unsafe {
854854
let array = PyArray::new(py, [slice.len()], false);
855-
if T::DATA_TYPE != DataType::Object {
855+
if T::IS_COPY {
856856
array.copy_ptr(slice.as_ptr(), slice.len());
857857
} else {
858858
let data_ptr = array.data();

src/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{mem, os::raw::c_int};
77

88
use crate::{
99
npyffi::{self, npy_intp},
10-
DataType, Element, PyArray,
10+
Element, PyArray,
1111
};
1212

1313
/// Conversion trait from some rust types to `PyArray`.
@@ -123,7 +123,7 @@ where
123123
fn to_pyarray<'py>(&self, py: Python<'py>) -> &'py PyArray<Self::Item, Self::Dim> {
124124
let len = self.len();
125125
match self.order() {
126-
Some(order) if A::DATA_TYPE != DataType::Object => {
126+
Some(order) if A::IS_COPY => {
127127
// if the array is contiguous, copy it by `copy_ptr`.
128128
let strides = self.npy_strides();
129129
unsafe {

src/dtype.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl DataType {
257257
/// pub struct Wrapper(pub Py<CustomElement>);
258258
///
259259
/// unsafe impl Element for Wrapper {
260-
/// const DATA_TYPE: DataType = DataType::Object;
260+
/// const IS_COPY: bool = false;
261261
///
262262
/// fn get_dtype(py: Python) -> &PyArrayDescr {
263263
/// PyArrayDescr::object(py)
@@ -273,17 +273,23 @@ impl DataType {
273273
/// });
274274
/// ```
275275
pub unsafe trait Element: Clone + Send {
276-
/// `DataType` corresponding to this type.
277-
const DATA_TYPE: DataType;
276+
/// Flag that indicates whether this type is trivially copyable.
277+
///
278+
/// It should be set to true for all trivially copyable types (like scalar types
279+
/// and record/array types only containing trivially copyable fields and elements).
280+
///
281+
/// This flag should *always* be set to `false` for object types or record types
282+
/// that contain object-type fields.
283+
const IS_COPY: bool;
278284

279-
/// Create `dtype`.
285+
/// Returns the associated array descriptor ("dtype") for the given type.
280286
fn get_dtype(py: Python) -> &PyArrayDescr;
281287
}
282288

283289
macro_rules! impl_num_element {
284290
($ty:ty, $data_type:expr) => {
285291
unsafe impl Element for $ty {
286-
const DATA_TYPE: DataType = $data_type;
292+
const IS_COPY: bool = true;
287293

288294
fn get_dtype(py: Python) -> &PyArrayDescr {
289295
PyArrayDescr::from_npy_type(py, $data_type.into_npy_type())
@@ -315,7 +321,7 @@ cfg_if! {
315321
}
316322

317323
unsafe impl Element for PyObject {
318-
const DATA_TYPE: DataType = DataType::Object;
324+
const IS_COPY: bool = false;
319325

320326
fn get_dtype(py: Python) -> &PyArrayDescr {
321327
PyArrayDescr::object(py)

0 commit comments

Comments
 (0)