Skip to content

Commit 975b5ee

Browse files
authored
Merge pull request #213 from PyO3/element-is-unsafe
Mark `Element` unsafe
2 parents 9d2a4b1 + c581425 commit 975b5ee

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/dtype.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,19 @@ impl DataType {
169169
}
170170

171171
/// Represents that a type can be an element of `PyArray`.
172-
pub trait Element: Clone + Send {
172+
///
173+
/// Currently, only integer/float/complex types are supported.
174+
/// If you come up with a nice implementation for some other types, we're happy to receive your PR :)
175+
/// You may refer to the [numpy document](https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types)
176+
/// for all types that numpy supports.
177+
///
178+
/// # Safety
179+
///
180+
/// A type `T` that implements this trait should be safe when managed in numpy array,
181+
/// thus implementing this trait is marked unsafe.
182+
/// For example, we don't support `PyObject` because of [an odd segfault](https://github.com/PyO3/rust-numpy/pull/143),
183+
/// although numpy itself supports it.
184+
pub unsafe trait Element: Clone + Send {
173185
/// `DataType` corresponding to this type.
174186
const DATA_TYPE: DataType;
175187

@@ -191,7 +203,7 @@ pub trait Element: Clone + Send {
191203

192204
macro_rules! impl_num_element {
193205
($t:ty, $npy_dat_t:ident $(,$npy_types: ident)+) => {
194-
impl Element for $t {
206+
unsafe impl Element for $t {
195207
const DATA_TYPE: DataType = DataType::$npy_dat_t;
196208
fn is_same_type(dtype: &PyArrayDescr) -> bool {
197209
$(dtype.get_typenum() == NPY_TYPES::$npy_types as i32 ||)+ false

0 commit comments

Comments
 (0)