Skip to content

Commit

Permalink
Formatting and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
JRRudy1 authored and davidhewitt committed Oct 6, 2024
1 parent 6d25071 commit 2dc0848
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ impl<'py, T, D> PyArrayMethods<'py, T, D> for Bound<'py, PyArray<T, D>> {
as_view(self, |shape, ptr| unsafe {
RawArrayViewMut::from_shape_ptr(shape, ptr)
})
}
}

fn to_owned_array(&self) -> Array<T, D>
where
Expand Down
52 changes: 26 additions & 26 deletions src/dtype.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::mem::size_of;
use std::os::raw::{c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort};
use std::ptr;
use ndarray::{Array, ArrayView, Dimension};

#[cfg(feature = "half")]
use half::{bf16, f16};
Expand Down Expand Up @@ -645,55 +644,56 @@ impl<'py> PyArrayDescrMethods<'py> for Bound<'py, PyArrayDescr> {

impl Sealed for Bound<'_, PyArrayDescr> {}


/// Weaker form of `Clone` for types that can be cloned while the GIL is held.
///
/// Any type that implements `Clone` can trivially implement `PyClone` by forwarding
/// to the `Clone::clone` method. However, some types (notably `PyObject`) can only
///
/// Any type that implements `Clone` can trivially implement `PyClone` by forwarding
/// to the `Clone::clone` method. However, some types (notably `PyObject`) can only
/// be safely cloned while the GIL is held, and therefore cannot implement `Clone`.
/// This trait provides a mechanism for performing a clone while the GIL is held, as
/// represented by the [`Python`] token provided as an argument to the [`py_clone`]
/// method. All API's in the `numpy` crate require the GIL to be held, so this weaker
/// method. All API's in the `numpy` crate require the GIL to be held, so this weaker
/// alternative to `Clone` is a sufficient prerequisite for implementing the
/// [`Element`] trait.
///
///
/// # Implementing `PyClone`
/// Implementing this trait is trivial for most types, and simply requires defining
/// the `py_clone` method. The `vec_from_slice` and `array_from_view` methods have
/// default implementations that simply map the `py_clone` method to each item in
/// the `py_clone` method. The `vec_from_slice` and `array_from_view` methods have
/// default implementations that simply map the `py_clone` method to each item in
/// the collection, but types may want to override these implementations if there
/// is a more efficient way to perform the conversion. In particular, `Clone` types
/// may instead defer to the `ToOwned::to_owned` and `ArrayBase::to_owned` methods
/// is a more efficient way to perform the conversion. In particular, `Clone` types
/// may instead defer to the `ToOwned::to_owned` and `ArrayBase::to_owned` methods
/// for increased performance.
///
///
/// [`py_clone`]: Self::py_clone
pub trait PyClone: Sized {
/// Create a clone of the value while the GIL is guaranteed to be held.
fn py_clone(&self, py: Python<'_>) -> Self;

/// Create an owned copy of the slice while the GIL is guaranteed to be held.
///
/// Some types may provide implementations of this method that are more efficient
///
/// Some types may provide implementations of this method that are more efficient
/// than simply mapping the `py_clone` method to each element in the slice.
#[inline]
fn vec_from_slice(py: Python<'_>, slc: &[Self]) -> Vec<Self> {
slc.iter().map(|elem| elem.py_clone(py)).collect()
}

/// Create an owned copy of the array while the GIL is guaranteed to be held.
///
/// Some types may provide implementations of this method that are more efficient
///
/// Some types may provide implementations of this method that are more efficient
/// than simply mapping the `py_clone` method to each element in the view.
#[inline]
fn array_from_view<D>(py: Python<'_>, view: ArrayView<'_, Self, D>) -> Array<Self, D>
fn array_from_view<D>(
py: Python<'_>,
view: ::ndarray::ArrayView<'_, Self, D>,
) -> ::ndarray::Array<Self, D>
where
D: Dimension
D: ::ndarray::Dimension,
{
view.map(|elem| elem.py_clone(py))
}
}


/// Represents that a type can be an element of `PyArray`.
///
/// Currently, only integer/float/complex/object types are supported. The [NumPy documentation][enumerated-types]
Expand Down Expand Up @@ -804,17 +804,17 @@ macro_rules! impl_py_clone {
fn py_clone(&self, _py: ::pyo3::Python<'_>) -> Self {
self.clone()
}

#[inline]
fn vec_from_slice(_py: ::pyo3::Python<'_>, slc: &[Self]) -> Vec<Self> {
slc.to_owned()
}

#[inline]
fn array_from_view<D>(
_py: ::pyo3::Python<'_>,
_py: ::pyo3::Python<'_>,
view: ::ndarray::ArrayView<'_, Self, D>
) -> ::ndarray::Array<Self, D>
) -> ::ndarray::Array<Self, D>
where
D: ::ndarray::Dimension
{
Expand Down Expand Up @@ -930,7 +930,7 @@ mod tests {

#[test]
fn test_dtype_names() {
fn type_name<T: Element>(py: Python) -> Bound<PyString> {
fn type_name<T: Element>(py: Python<'_>) -> Bound<'_, PyString> {
dtype_bound::<T>(py).typeobj().qualname().unwrap()
}
Python::with_gil(|py| {
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ as well as the [`PyReadonlyArray::try_as_matrix`] and [`PyReadwriteArray::try_as
//! [c-api]: https://numpy.org/doc/stable/reference/c-api
//! [ndarray]: https://numpy.org/doc/stable/reference/arrays.ndarray.html
#![deny(missing_docs)]
// requiring `Debug` impls is not relevant without gil-refs since `&PyArray` and
// similar aren't constructible and the `pyo3::pyobject_native_type_base` macro
// does not provide a `Debug` impl
// requiring `Debug` impls is not relevant without gil-refs since `&PyArray`
// and similar aren't constructible
#![cfg_attr(feature = "gil-refs", deny(missing_debug_implementations))]

#[cfg(all(target_os = "windows", target_arch = "x86"))]
Expand Down
12 changes: 6 additions & 6 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ unsafe impl<const N: usize> Element for PyFixedString<N> {
impl<const N: usize> PyClone for PyFixedString<N> {
#[inline]
fn py_clone(&self, _py: Python<'_>) -> Self {
self.clone()
*self
}

#[inline]
Expand All @@ -98,10 +98,10 @@ impl<const N: usize> PyClone for PyFixedString<N> {
#[inline]
fn array_from_view<D>(
_py: Python<'_>,
view: ::ndarray::ArrayView<'_, Self, D>
view: ::ndarray::ArrayView<'_, Self, D>,
) -> ::ndarray::Array<Self, D>
where
D: ::ndarray::Dimension
D: ::ndarray::Dimension,
{
view.to_owned()
}
Expand Down Expand Up @@ -171,7 +171,7 @@ impl<const N: usize> From<[Py_UCS4; N]> for PyFixedUnicode<N> {
impl<const N: usize> PyClone for PyFixedUnicode<N> {
#[inline]
fn py_clone(&self, _py: Python<'_>) -> Self {
self.clone()
*self
}

#[inline]
Expand All @@ -182,10 +182,10 @@ impl<const N: usize> PyClone for PyFixedUnicode<N> {
#[inline]
fn array_from_view<D>(
_py: Python<'_>,
view: ::ndarray::ArrayView<'_, Self, D>
view: ::ndarray::ArrayView<'_, Self, D>,
) -> ::ndarray::Array<Self, D>
where
D: ::ndarray::Dimension
D: ::ndarray::Dimension,
{
view.to_owned()
}
Expand Down

0 comments on commit 2dc0848

Please sign in to comment.