Skip to content

Commit f61f4e2

Browse files
committed
Use a uniform approach to sealing traits
1 parent ea88df3 commit f61f4e2

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

src/array_like.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ use pyo3::{
1010
};
1111

1212
use crate::array::PyArrayMethods;
13-
use crate::sealed::Sealed;
1413
use crate::{get_array_module, Element, IntoPyArray, PyArray, PyReadonlyArray};
1514

1615
pub trait Coerce: Sealed {
1716
const VAL: bool;
1817
}
1918

19+
mod sealed {
20+
pub trait Sealed {}
21+
}
22+
23+
use sealed::Sealed;
24+
2025
/// Marker type to indicate that the element type received via [`PyArrayLike`] must match the specified type exactly.
2126
#[derive(Debug)]
2227
pub struct TypeMustMatch;

src/convert.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::array::{PyArray, PyArrayMethods};
99
use crate::dtype::Element;
1010
use crate::error::MAX_DIMENSIONALITY_ERR;
1111
use crate::npyffi::{self, npy_intp};
12-
use crate::sealed::Sealed;
1312
use crate::slice_container::PySliceContainer;
1413

1514
/// Conversion trait from owning Rust types into [`PyArray`].
@@ -273,6 +272,12 @@ pub trait ToNpyDims: Dimension + Sealed {
273272
}
274273
}
275274

275+
mod sealed {
276+
pub trait Sealed {}
277+
}
278+
279+
use sealed::Sealed;
280+
276281
impl<D> ToNpyDims for D where D: Dimension {}
277282

278283
/// Trait implemented by types that can be used to index an array.

src/dtype.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl PyArrayDescr {
364364

365365
/// Implementation of functionality for [`PyArrayDescr`].
366366
#[doc(alias = "PyArrayDescr")]
367-
pub trait PyArrayDescrMethods<'py>: sealed::Sealed {
367+
pub trait PyArrayDescrMethods<'py>: Sealed {
368368
/// Returns `self` as `*mut PyArray_Descr`.
369369
fn as_dtype_ptr(&self) -> *mut PyArray_Descr;
370370

@@ -553,6 +553,12 @@ pub trait PyArrayDescrMethods<'py>: sealed::Sealed {
553553
fn get_field(&self, name: &str) -> PyResult<(Bound<'py, PyArrayDescr>, usize)>;
554554
}
555555

556+
mod sealed {
557+
pub trait Sealed {}
558+
}
559+
560+
use sealed::Sealed;
561+
556562
impl<'py> PyArrayDescrMethods<'py> for Bound<'py, PyArrayDescr> {
557563
fn as_dtype_ptr(&self) -> *mut PyArray_Descr {
558564
self.as_ptr() as _
@@ -632,13 +638,7 @@ impl<'py> PyArrayDescrMethods<'py> for Bound<'py, PyArrayDescr> {
632638
}
633639
}
634640

635-
mod sealed {
636-
use super::PyArrayDescr;
637-
638-
pub trait Sealed {}
639-
640-
impl Sealed for pyo3::Bound<'_, PyArrayDescr> {}
641-
}
641+
impl Sealed for Bound<'_, PyArrayDescr> {}
642642

643643
/// Represents that a type can be an element of `PyArray`.
644644
///

src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ mod doctest {
130130
doc_comment!(include_str!("../README.md"), readme);
131131
}
132132

133-
mod sealed {
134-
pub trait Sealed {}
135-
}
136-
137133
#[cold]
138134
#[inline(always)]
139135
fn cold() {}

src/untyped_array.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl PyUntypedArray {
258258

259259
/// Implementation of functionality for [`PyUntypedArray`].
260260
#[doc(alias = "PyUntypedArray")]
261-
pub trait PyUntypedArrayMethods<'py>: sealed::Sealed {
261+
pub trait PyUntypedArrayMethods<'py>: Sealed {
262262
/// Returns a raw pointer to the underlying [`PyArrayObject`][npyffi::PyArrayObject].
263263
fn as_array_ptr(&self) -> *mut npyffi::PyArrayObject;
264264

@@ -423,6 +423,12 @@ pub trait PyUntypedArrayMethods<'py>: sealed::Sealed {
423423
}
424424
}
425425

426+
mod sealed {
427+
pub trait Sealed {}
428+
}
429+
430+
use sealed::Sealed;
431+
426432
fn check_flags(obj: &npyffi::PyArrayObject, flags: i32) -> bool {
427433
obj.flags & flags != 0
428434
}
@@ -441,6 +447,8 @@ impl<'py> PyUntypedArrayMethods<'py> for Bound<'py, PyUntypedArray> {
441447
}
442448
}
443449

450+
impl Sealed for Bound<'_, PyUntypedArray> {}
451+
444452
// We won't be able to provide a `Deref` impl from `Bound<'_, PyArray<T, D>>` to
445453
// `Bound<'_, PyUntypedArray>`, so this seems to be the next best thing to do
446454
impl<'py, T, D> PyUntypedArrayMethods<'py> for Bound<'py, PyArray<T, D>> {
@@ -455,11 +463,4 @@ impl<'py, T, D> PyUntypedArrayMethods<'py> for Bound<'py, PyArray<T, D>> {
455463
}
456464
}
457465

458-
mod sealed {
459-
use super::{PyArray, PyUntypedArray};
460-
461-
pub trait Sealed {}
462-
463-
impl Sealed for pyo3::Bound<'_, PyUntypedArray> {}
464-
impl<T, D> Sealed for pyo3::Bound<'_, PyArray<T, D>> {}
465-
}
466+
impl<T, D> Sealed for Bound<'_, PyArray<T, D>> {}

0 commit comments

Comments
 (0)