@@ -5,7 +5,7 @@ use pyo3::{
5
5
ffi,
6
6
prelude:: * ,
7
7
sync:: GILOnceCell ,
8
- types:: { PyCapsule , PyType } ,
8
+ types:: { DerefToPyAny , PyCapsule , PyType } ,
9
9
PyTypeInfo ,
10
10
} ;
11
11
@@ -15,10 +15,10 @@ use crate::npyffi::npy_bitgen;
15
15
///!
16
16
///! [bg]: https://numpy.org/doc/stable//reference/random/bit_generators/generated/numpy.random.BitGenerator.html
17
17
#[ repr( transparent) ]
18
- pub struct BitGenerator ( PyAny ) ;
18
+ pub struct PyBitGenerator ( PyAny ) ;
19
19
20
- unsafe impl PyTypeInfo for BitGenerator {
21
- const NAME : & ' static str = "BitGenerator " ;
20
+ unsafe impl PyTypeInfo for PyBitGenerator {
21
+ const NAME : & ' static str = "PyBitGenerator " ;
22
22
const MODULE : Option < & ' static str > = Some ( "numpy.random" ) ;
23
23
24
24
fn type_object_raw < ' py > ( py : Python < ' py > ) -> * mut ffi:: PyTypeObject {
@@ -38,18 +38,17 @@ unsafe impl PyTypeInfo for BitGenerator {
38
38
}
39
39
}
40
40
41
+ impl DerefToPyAny for PyBitGenerator { }
42
+
41
43
/// Methods for [`BitGenerator`]
42
44
pub trait BitGeneratorMethods < ' py > {
43
45
/// Returns a new [`BitGen`]
44
46
fn bit_gen ( & self ) -> PyResult < BitGen < ' py > > ;
45
47
}
46
48
47
- impl < ' py > BitGeneratorMethods < ' py > for Bound < ' py , BitGenerator > {
49
+ impl < ' py > BitGeneratorMethods < ' py > for Bound < ' py , PyBitGenerator > {
48
50
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 > ( ) ?;
53
52
assert_eq ! ( capsule. name( ) ?, Some ( c"BitGenerator" ) ) ;
54
53
let ptr = capsule. pointer ( ) as * mut npy_bitgen ;
55
54
// SAFETY: the lifetime of `ptr` is derived from the lifetime of `self`
@@ -59,9 +58,9 @@ impl<'py> BitGeneratorMethods<'py> for Bound<'py, BitGenerator> {
59
58
}
60
59
}
61
60
62
- impl < ' py > TryFrom < & Bound < ' py , BitGenerator > > for BitGen < ' py > {
61
+ impl < ' py > TryFrom < & Bound < ' py , PyBitGenerator > > for BitGen < ' py > {
63
62
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 > {
65
64
value. bit_gen ( )
66
65
}
67
66
}
@@ -105,12 +104,12 @@ impl rand::RngCore for BitGen<'_> {
105
104
mod tests {
106
105
use super :: * ;
107
106
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 > > {
109
108
let default_rng = py. import ( "numpy.random" ) ?. getattr ( "default_rng" ) ?;
110
109
let bit_generator = default_rng
111
110
. call0 ( ) ?
112
111
. getattr ( "bit_generator" ) ?
113
- . downcast_into :: < BitGenerator > ( ) ?;
112
+ . downcast_into :: < PyBitGenerator > ( ) ?;
114
113
Ok ( bit_generator)
115
114
}
116
115
0 commit comments