Skip to content

Commit f291cf2

Browse files
JRRudy1davidhewitt
authored andcommitted
Updated methods in the array module to call PyClone methods instead of Clone methods.
This change is trivial since the GIL is already held everywhere that the clones occur, with the minor technicality that the GIL is not guaranteed to be held in the definition of the `PyArrayMethods` trait, but only in the actual impl block for `Bound<PyArray`. As such, several method implementations had to be moved to the impl block instead of being default implementations in the trait definition, but this is not a breaking change since the trait is `Sealed` and we control the only impl.
1 parent 23c6a13 commit f291cf2

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/array.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,10 +1686,7 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
16861686
where
16871687
T: Element,
16881688
D: Dimension,
1689-
Idx: NpyIndex<Dim = D>,
1690-
{
1691-
unsafe { self.get(index) }.cloned()
1692-
}
1689+
Idx: NpyIndex<Dim = D>;
16931690

16941691
/// Turn an array with fixed dimensionality into one with dynamic dimensionality.
16951692
fn to_dyn(&self) -> &Bound<'py, PyArray<T, IxDyn>>
@@ -1720,10 +1717,7 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
17201717
fn to_vec(&self) -> Result<Vec<T>, NotContiguousError>
17211718
where
17221719
T: Element,
1723-
D: Dimension,
1724-
{
1725-
unsafe { self.as_slice() }.map(ToOwned::to_owned)
1726-
}
1720+
D: Dimension;
17271721

17281722
/// Get an immutable borrow of the NumPy array
17291723
fn try_readonly(&self) -> Result<PyReadonlyArray<'py, T, D>, BorrowError>
@@ -1827,10 +1821,7 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
18271821
fn to_owned_array(&self) -> Array<T, D>
18281822
where
18291823
T: Element,
1830-
D: Dimension,
1831-
{
1832-
unsafe { self.as_array() }.to_owned()
1833-
}
1824+
D: Dimension;
18341825

18351826
/// Copies `self` into `other`, performing a data type conversion if necessary.
18361827
///
@@ -2206,10 +2197,29 @@ impl<'py, T, D> PyArrayMethods<'py, T, D> for Bound<'py, PyArray<T, D>> {
22062197
Some(&mut *ptr)
22072198
}
22082199

2200+
fn get_owned<Idx>(&self, index: Idx) -> Option<T>
2201+
where
2202+
T: Element,
2203+
D: Dimension,
2204+
Idx: NpyIndex<Dim = D>,
2205+
{
2206+
let element = unsafe { self.get(index) };
2207+
element.map(|elem| elem.py_clone(self.py()))
2208+
}
2209+
22092210
fn to_dyn(&self) -> &Bound<'py, PyArray<T, IxDyn>> {
22102211
unsafe { self.downcast_unchecked() }
22112212
}
22122213

2214+
fn to_vec(&self) -> Result<Vec<T>, NotContiguousError>
2215+
where
2216+
T: Element,
2217+
D: Dimension,
2218+
{
2219+
let slice = unsafe { self.as_slice() };
2220+
slice.map(|slc| T::vec_from_slice(self.py(), slc))
2221+
}
2222+
22132223
fn try_readonly(&self) -> Result<PyReadonlyArray<'py, T, D>, BorrowError>
22142224
where
22152225
T: Element,
@@ -2260,6 +2270,15 @@ impl<'py, T, D> PyArrayMethods<'py, T, D> for Bound<'py, PyArray<T, D>> {
22602270
as_view(self, |shape, ptr| unsafe {
22612271
RawArrayViewMut::from_shape_ptr(shape, ptr)
22622272
})
2273+
}
2274+
2275+
fn to_owned_array(&self) -> Array<T, D>
2276+
where
2277+
T: Element,
2278+
D: Dimension,
2279+
{
2280+
let view = unsafe { self.as_array() };
2281+
T::array_from_view(self.py(), view)
22632282
}
22642283

22652284
fn copy_to<U: Element>(&self, other: &Bound<'py, PyArray<U, D>>) -> PyResult<()>

0 commit comments

Comments
 (0)