Skip to content

Commit 8e25d8c

Browse files
JRRudy1davidhewitt
authored andcommitted
Added PyClone impls for the types in the strings module.
The impls were provided explicitly instead of using the `impl_py_clone` macro since making the macro flexible enough to handle the `const` param adds more complexity than it's worth.
1 parent ab71b1e commit 8e25d8c

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/strings.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use pyo3::{
1717
};
1818
use rustc_hash::FxHashMap;
1919

20-
use crate::dtype::{Element, PyArrayDescr, PyArrayDescrMethods};
20+
use crate::dtype::{Element, PyArrayDescr, PyArrayDescrMethods, PyClone};
2121
use crate::npyffi::PyDataType_SET_ELSIZE;
2222
use crate::npyffi::NPY_TYPES;
2323

@@ -84,6 +84,29 @@ unsafe impl<const N: usize> Element for PyFixedString<N> {
8484
}
8585
}
8686

87+
impl<const N: usize> PyClone for PyFixedString<N> {
88+
#[inline]
89+
fn py_clone(&self, _py: Python<'_>) -> Self {
90+
self.clone()
91+
}
92+
93+
#[inline]
94+
fn vec_from_slice(_py: Python<'_>, slc: &[Self]) -> Vec<Self> {
95+
slc.to_owned()
96+
}
97+
98+
#[inline]
99+
fn array_from_view<D>(
100+
_py: Python<'_>,
101+
view: ::ndarray::ArrayView<'_, Self, D>
102+
) -> ::ndarray::Array<Self, D>
103+
where
104+
D: ::ndarray::Dimension
105+
{
106+
view.to_owned()
107+
}
108+
}
109+
87110
/// A newtype wrapper around [`[PyUCS4; N]`][Py_UCS4] to handle [`str_` scalars][numpy-str] while satisfying coherence.
88111
///
89112
/// Note that when creating arrays of Unicode strings without an explicit `dtype`,
@@ -145,6 +168,29 @@ impl<const N: usize> From<[Py_UCS4; N]> for PyFixedUnicode<N> {
145168
}
146169
}
147170

171+
impl<const N: usize> PyClone for PyFixedUnicode<N> {
172+
#[inline]
173+
fn py_clone(&self, _py: Python<'_>) -> Self {
174+
self.clone()
175+
}
176+
177+
#[inline]
178+
fn vec_from_slice(_py: Python<'_>, slc: &[Self]) -> Vec<Self> {
179+
slc.to_owned()
180+
}
181+
182+
#[inline]
183+
fn array_from_view<D>(
184+
_py: Python<'_>,
185+
view: ::ndarray::ArrayView<'_, Self, D>
186+
) -> ::ndarray::Array<Self, D>
187+
where
188+
D: ::ndarray::Dimension
189+
{
190+
view.to_owned()
191+
}
192+
}
193+
148194
unsafe impl<const N: usize> Element for PyFixedUnicode<N> {
149195
const IS_COPY: bool = true;
150196

0 commit comments

Comments
 (0)