Skip to content

Commit d1909d3

Browse files
committed
rename and deref
1 parent 6c1a89b commit d1909d3

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/random.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use pyo3::{
55
ffi,
66
prelude::*,
77
sync::GILOnceCell,
8-
types::{PyCapsule, PyType},
8+
types::{DerefToPyAny, PyCapsule, PyType},
99
PyTypeInfo,
1010
};
1111

@@ -15,10 +15,10 @@ use crate::npyffi::npy_bitgen;
1515
///!
1616
///! [bg]: https://numpy.org/doc/stable//reference/random/bit_generators/generated/numpy.random.BitGenerator.html
1717
#[repr(transparent)]
18-
pub struct BitGenerator(PyAny);
18+
pub struct PyBitGenerator(PyAny);
1919

20-
unsafe impl PyTypeInfo for BitGenerator {
21-
const NAME: &'static str = "BitGenerator";
20+
unsafe impl PyTypeInfo for PyBitGenerator {
21+
const NAME: &'static str = "PyBitGenerator";
2222
const MODULE: Option<&'static str> = Some("numpy.random");
2323

2424
fn type_object_raw<'py>(py: Python<'py>) -> *mut ffi::PyTypeObject {
@@ -38,18 +38,17 @@ unsafe impl PyTypeInfo for BitGenerator {
3838
}
3939
}
4040

41+
impl DerefToPyAny for PyBitGenerator {}
42+
4143
/// Methods for [`BitGenerator`]
4244
pub trait BitGeneratorMethods<'py> {
4345
/// Returns a new [`BitGen`]
4446
fn bit_gen(&self) -> PyResult<BitGen<'py>>;
4547
}
4648

47-
impl<'py> BitGeneratorMethods<'py> for Bound<'py, BitGenerator> {
49+
impl<'py> BitGeneratorMethods<'py> for Bound<'py, PyBitGenerator> {
4850
fn bit_gen(&self) -> PyResult<BitGen<'py>> {
49-
let capsule = self
50-
.as_any()
51-
.getattr("capsule")?
52-
.downcast_into::<PyCapsule>()?;
51+
let capsule = self.getattr("capsule")?.downcast_into::<PyCapsule>()?;
5352
assert_eq!(capsule.name()?, Some(c"BitGenerator"));
5453
let ptr = capsule.pointer() as *mut npy_bitgen;
5554
// SAFETY: the lifetime of `ptr` is derived from the lifetime of `self`
@@ -59,9 +58,9 @@ impl<'py> BitGeneratorMethods<'py> for Bound<'py, BitGenerator> {
5958
}
6059
}
6160

62-
impl<'py> TryFrom<&Bound<'py, BitGenerator>> for BitGen<'py> {
61+
impl<'py> TryFrom<&Bound<'py, PyBitGenerator>> for BitGen<'py> {
6362
type Error = PyErr;
64-
fn try_from(value: &Bound<'py, BitGenerator>) -> Result<Self, Self::Error> {
63+
fn try_from(value: &Bound<'py, PyBitGenerator>) -> Result<Self, Self::Error> {
6564
value.bit_gen()
6665
}
6766
}
@@ -105,12 +104,12 @@ impl rand::RngCore for BitGen<'_> {
105104
mod tests {
106105
use super::*;
107106

108-
fn get_bit_generator<'py>(py: Python<'py>) -> PyResult<Bound<'py, BitGenerator>> {
107+
fn get_bit_generator<'py>(py: Python<'py>) -> PyResult<Bound<'py, PyBitGenerator>> {
109108
let default_rng = py.import("numpy.random")?.getattr("default_rng")?;
110109
let bit_generator = default_rng
111110
.call0()?
112111
.getattr("bit_generator")?
113-
.downcast_into::<BitGenerator>()?;
112+
.downcast_into::<PyBitGenerator>()?;
114113
Ok(bit_generator)
115114
}
116115

0 commit comments

Comments
 (0)