|
1 | 1 | use std::mem::size_of;
|
2 | 2 | use std::os::raw::{c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort};
|
3 | 3 | use std::ptr;
|
4 |
| -use ndarray::{Array, ArrayView, Dimension}; |
5 | 4 |
|
6 | 5 | #[cfg(feature = "half")]
|
7 | 6 | use half::{bf16, f16};
|
@@ -645,55 +644,56 @@ impl<'py> PyArrayDescrMethods<'py> for Bound<'py, PyArrayDescr> {
|
645 | 644 |
|
646 | 645 | impl Sealed for Bound<'_, PyArrayDescr> {}
|
647 | 646 |
|
648 |
| - |
649 | 647 | /// Weaker form of `Clone` for types that can be cloned while the GIL is held.
|
650 |
| -/// |
651 |
| -/// Any type that implements `Clone` can trivially implement `PyClone` by forwarding |
652 |
| -/// to the `Clone::clone` method. However, some types (notably `PyObject`) can only |
| 648 | +/// |
| 649 | +/// Any type that implements `Clone` can trivially implement `PyClone` by forwarding |
| 650 | +/// to the `Clone::clone` method. However, some types (notably `PyObject`) can only |
653 | 651 | /// be safely cloned while the GIL is held, and therefore cannot implement `Clone`.
|
654 | 652 | /// This trait provides a mechanism for performing a clone while the GIL is held, as
|
655 | 653 | /// represented by the [`Python`] token provided as an argument to the [`py_clone`]
|
656 |
| -/// method. All API's in the `numpy` crate require the GIL to be held, so this weaker |
| 654 | +/// method. All API's in the `numpy` crate require the GIL to be held, so this weaker |
657 | 655 | /// alternative to `Clone` is a sufficient prerequisite for implementing the
|
658 | 656 | /// [`Element`] trait.
|
659 |
| -/// |
| 657 | +/// |
660 | 658 | /// # Implementing `PyClone`
|
661 | 659 | /// Implementing this trait is trivial for most types, and simply requires defining
|
662 |
| -/// the `py_clone` method. The `vec_from_slice` and `array_from_view` methods have |
663 |
| -/// default implementations that simply map the `py_clone` method to each item in |
| 660 | +/// the `py_clone` method. The `vec_from_slice` and `array_from_view` methods have |
| 661 | +/// default implementations that simply map the `py_clone` method to each item in |
664 | 662 | /// the collection, but types may want to override these implementations if there
|
665 |
| -/// is a more efficient way to perform the conversion. In particular, `Clone` types |
666 |
| -/// may instead defer to the `ToOwned::to_owned` and `ArrayBase::to_owned` methods |
| 663 | +/// is a more efficient way to perform the conversion. In particular, `Clone` types |
| 664 | +/// may instead defer to the `ToOwned::to_owned` and `ArrayBase::to_owned` methods |
667 | 665 | /// for increased performance.
|
668 |
| -/// |
| 666 | +/// |
669 | 667 | /// [`py_clone`]: Self::py_clone
|
670 | 668 | pub trait PyClone: Sized {
|
671 | 669 | /// Create a clone of the value while the GIL is guaranteed to be held.
|
672 | 670 | fn py_clone(&self, py: Python<'_>) -> Self;
|
673 |
| - |
| 671 | + |
674 | 672 | /// Create an owned copy of the slice while the GIL is guaranteed to be held.
|
675 |
| - /// |
676 |
| - /// Some types may provide implementations of this method that are more efficient |
| 673 | + /// |
| 674 | + /// Some types may provide implementations of this method that are more efficient |
677 | 675 | /// than simply mapping the `py_clone` method to each element in the slice.
|
678 | 676 | #[inline]
|
679 | 677 | fn vec_from_slice(py: Python<'_>, slc: &[Self]) -> Vec<Self> {
|
680 | 678 | slc.iter().map(|elem| elem.py_clone(py)).collect()
|
681 | 679 | }
|
682 |
| - |
| 680 | + |
683 | 681 | /// Create an owned copy of the array while the GIL is guaranteed to be held.
|
684 |
| - /// |
685 |
| - /// Some types may provide implementations of this method that are more efficient |
| 682 | + /// |
| 683 | + /// Some types may provide implementations of this method that are more efficient |
686 | 684 | /// than simply mapping the `py_clone` method to each element in the view.
|
687 | 685 | #[inline]
|
688 |
| - fn array_from_view<D>(py: Python<'_>, view: ArrayView<'_, Self, D>) -> Array<Self, D> |
| 686 | + fn array_from_view<D>( |
| 687 | + py: Python<'_>, |
| 688 | + view: ::ndarray::ArrayView<'_, Self, D>, |
| 689 | + ) -> ::ndarray::Array<Self, D> |
689 | 690 | where
|
690 |
| - D: Dimension |
| 691 | + D: ::ndarray::Dimension, |
691 | 692 | {
|
692 | 693 | view.map(|elem| elem.py_clone(py))
|
693 | 694 | }
|
694 | 695 | }
|
695 | 696 |
|
696 |
| - |
697 | 697 | /// Represents that a type can be an element of `PyArray`.
|
698 | 698 | ///
|
699 | 699 | /// Currently, only integer/float/complex/object types are supported. The [NumPy documentation][enumerated-types]
|
@@ -804,17 +804,17 @@ macro_rules! impl_py_clone {
|
804 | 804 | fn py_clone(&self, _py: ::pyo3::Python<'_>) -> Self {
|
805 | 805 | self.clone()
|
806 | 806 | }
|
807 |
| - |
| 807 | + |
808 | 808 | #[inline]
|
809 | 809 | fn vec_from_slice(_py: ::pyo3::Python<'_>, slc: &[Self]) -> Vec<Self> {
|
810 | 810 | slc.to_owned()
|
811 | 811 | }
|
812 |
| - |
| 812 | + |
813 | 813 | #[inline]
|
814 | 814 | fn array_from_view<D>(
|
815 |
| - _py: ::pyo3::Python<'_>, |
| 815 | + _py: ::pyo3::Python<'_>, |
816 | 816 | view: ::ndarray::ArrayView<'_, Self, D>
|
817 |
| - ) -> ::ndarray::Array<Self, D> |
| 817 | + ) -> ::ndarray::Array<Self, D> |
818 | 818 | where
|
819 | 819 | D: ::ndarray::Dimension
|
820 | 820 | {
|
@@ -930,7 +930,7 @@ mod tests {
|
930 | 930 |
|
931 | 931 | #[test]
|
932 | 932 | fn test_dtype_names() {
|
933 |
| - fn type_name<T: Element>(py: Python) -> Bound<PyString> { |
| 933 | + fn type_name<T: Element>(py: Python<'_>) -> Bound<'_, PyString> { |
934 | 934 | dtype_bound::<T>(py).typeobj().qualname().unwrap()
|
935 | 935 | }
|
936 | 936 | Python::with_gil(|py| {
|
|
0 commit comments