You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/dtype.rs
+6-32Lines changed: 6 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -292,7 +292,7 @@ impl PyArrayDescr {
292
292
293
293
/// Represents that a type can be an element of `PyArray`.
294
294
///
295
-
/// Currently, only integer/float/complex types are supported.
295
+
/// Currently, only integer/float/complex/object types are supported.
296
296
/// If you come up with a nice implementation for some other types, we're happy to receive your PR :)
297
297
/// You may refer to the [numpy document](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types)
298
298
/// for all types that numpy supports.
@@ -310,38 +310,12 @@ impl PyArrayDescr {
310
310
///
311
311
/// # Custom element types
312
312
///
313
-
/// You can implement this trait to manage arrays of custom element types, but they still need to be stored
314
-
/// on Python's heap using PyO3's [Py](pyo3::Py) type.
313
+
/// Note that we cannot safely store `Py<T>` where `T: PyClass`, because the type information would be
314
+
/// eliminated in the resulting NumPy array.
315
+
/// In other words, objects are always treated as `Py<PyAny>` (a.k.a. `PyObject`) by Python code,
316
+
/// and only `Py<PyAny>` can be stored in a type safe manner.
315
317
///
316
-
/// ```
317
-
/// use numpy::{ndarray::Array2, Element, PyArray, PyArrayDescr, ToPyArray};
318
-
/// use pyo3::{pyclass, Py, Python};
319
-
///
320
-
/// #[pyclass]
321
-
/// pub struct CustomElement;
322
-
///
323
-
/// // The transparent wrapper is necessary as one cannot implement
324
-
/// // a foreign trait (`Element`) on a foreign type (`Py`) directly.
325
-
/// #[derive(Clone)]
326
-
/// #[repr(transparent)]
327
-
/// pub struct Wrapper(pub Py<CustomElement>);
328
-
///
329
-
/// unsafe impl Element for Wrapper {
330
-
/// const IS_COPY: bool = false;
331
-
///
332
-
/// fn get_dtype(py: Python) -> &PyArrayDescr {
333
-
/// PyArrayDescr::object(py)
334
-
/// }
335
-
/// }
336
-
///
337
-
/// Python::with_gil(|py| {
338
-
/// let array = Array2::<Wrapper>::from_shape_fn((2, 3), |(_i, _j)| {
339
-
/// Wrapper(Py::new(py, CustomElement).unwrap())
340
-
/// });
341
-
///
342
-
/// let _array: &PyArray<Wrapper, _> = array.to_pyarray(py);
343
-
/// });
344
-
/// ```
318
+
/// You can however create `ndarray::Array<Py<T>, D>` and turn that into a NumPy array safely and efficiently using [`from_owned_object_array`][crate::PyArray::from_owned_object_array].
345
319
pubunsafetraitElement:Clone + Send{
346
320
/// Flag that indicates whether this type is trivially copyable.
0 commit comments