@@ -95,11 +95,11 @@ unsafe impl PyTypeInfo for PyBitGenerator {
95
95
/// Methods for [`PyBitGenerator`].
96
96
pub trait PyBitGeneratorMethods < ' py > {
97
97
/// Acquire a lock on the BitGenerator to allow calling its methods in.
98
- fn lock ( & self ) -> PyResult < PyBitGeneratorGuard < ' py > > ;
98
+ fn lock ( & self ) -> PyResult < PyBitGeneratorGuard > ;
99
99
}
100
100
101
101
impl < ' py > PyBitGeneratorMethods < ' py > for Bound < ' py , PyBitGenerator > {
102
- fn lock ( & self ) -> PyResult < PyBitGeneratorGuard < ' py > > {
102
+ fn lock ( & self ) -> PyResult < PyBitGeneratorGuard > {
103
103
let capsule = self . getattr ( "capsule" ) ?. downcast_into :: < PyCapsule > ( ) ?;
104
104
let lock = self . getattr ( "lock" ) ?;
105
105
if lock. call_method0 ( "locked" ) ?. extract ( ) ? {
@@ -120,48 +120,46 @@ impl<'py> PyBitGeneratorMethods<'py> for Bound<'py, PyBitGenerator> {
120
120
raw_bitgen : non_null,
121
121
_capsule : capsule. unbind ( ) ,
122
122
lock : lock. unbind ( ) ,
123
- py : self . py ( ) ,
124
123
} )
125
124
}
126
125
}
127
126
128
- impl < ' py > TryFrom < & Bound < ' py , PyBitGenerator > > for PyBitGeneratorGuard < ' py > {
127
+ impl < ' py > TryFrom < & Bound < ' py , PyBitGenerator > > for PyBitGeneratorGuard {
129
128
type Error = PyErr ;
130
129
fn try_from ( value : & Bound < ' py , PyBitGenerator > ) -> Result < Self , Self :: Error > {
131
130
value. lock ( )
132
131
}
133
132
}
134
133
135
134
/// [`PyBitGenerator`] lock allowing to access its methods without holding the GIL.
136
- pub struct PyBitGeneratorGuard < ' py > {
135
+ pub struct PyBitGeneratorGuard {
137
136
raw_bitgen : NonNull < npy_bitgen > ,
138
137
/// This field makes sure the `raw_bitgen` inside the capsule doesn’t get deallocated.
139
138
_capsule : Py < PyCapsule > ,
140
139
/// This lock makes sure no other threads try to use the BitGenerator while we do.
141
140
lock : Py < PyAny > ,
142
- /// This should be an unsafe field (https://github.com/rust-lang/rust/issues/132922)
143
- ///
144
- /// SAFETY: only use this in `Drop::drop` (when we are sure the GIL is held).
145
- py : Python < ' py > ,
146
141
}
147
142
148
143
// SAFETY: we can’t have public APIs that access the Python objects,
149
144
// only the `raw_bitgen` pointer.
150
- unsafe impl Send for PyBitGeneratorGuard < ' _ > { }
145
+ unsafe impl Send for PyBitGeneratorGuard { }
151
146
152
- impl Drop for PyBitGeneratorGuard < ' _ > {
147
+ impl Drop for PyBitGeneratorGuard {
153
148
fn drop ( & mut self ) {
154
- // ignore errors. This includes when `try_drop` was called manually
155
- let _ = self . lock . bind ( self . py ) . call_method0 ( "release" ) ;
149
+ // ignore errors. This includes when `try_release` was called manually.
150
+ let _ = Python :: with_gil ( |py| -> PyResult < _ > {
151
+ self . lock . bind ( py) . call_method0 ( "release" ) ?;
152
+ Ok ( ( ) )
153
+ } ) ;
156
154
}
157
155
}
158
156
159
157
// SAFETY: We hold the `BitGenerator.lock`,
160
158
// so nothing apart from us is allowed to change its state.
161
- impl < ' py > PyBitGeneratorGuard < ' py > {
162
- /// Drop the lock manually before `Drop::drop` tries to do it (used for testing) .
159
+ impl < ' py > PyBitGeneratorGuard {
160
+ /// Release the lock, allowing for checking for errors .
163
161
#[ allow( dead_code) ]
164
- fn try_drop ( self , py : Python < ' py > ) -> PyResult < ( ) > {
162
+ pub fn try_release ( self , py : Python < ' py > ) -> PyResult < ( ) > {
165
163
self . lock . bind ( py) . call_method0 ( "release" ) ?;
166
164
Ok ( ( ) )
167
165
}
@@ -197,7 +195,7 @@ impl<'py> PyBitGeneratorGuard<'py> {
197
195
}
198
196
199
197
#[ cfg( feature = "rand" ) ]
200
- impl rand:: RngCore for PyBitGeneratorGuard < ' _ > {
198
+ impl rand:: RngCore for PyBitGeneratorGuard {
201
199
fn next_u32 ( & mut self ) -> u32 {
202
200
self . next_uint32 ( )
203
201
}
@@ -229,7 +227,7 @@ mod tests {
229
227
py. allow_threads ( || {
230
228
let _ = bitgen. next_raw ( ) ;
231
229
} ) ;
232
- assert ! ( bitgen. try_drop ( py) . is_ok( ) ) ;
230
+ assert ! ( bitgen. try_release ( py) . is_ok( ) ) ;
233
231
Ok ( ( ) )
234
232
} )
235
233
}
@@ -278,7 +276,7 @@ mod tests {
278
276
assert ! ( bitgen. random_ratio( 1 , 1 ) ) ;
279
277
assert ! ( !bitgen. random_ratio( 0 , 1 ) ) ;
280
278
} ) ;
281
- assert ! ( bitgen. try_drop ( py) . is_ok( ) ) ;
279
+ assert ! ( bitgen. try_release ( py) . is_ok( ) ) ;
282
280
Ok ( ( ) )
283
281
} )
284
282
}
@@ -289,7 +287,7 @@ mod tests {
289
287
let generator = get_bit_generator ( py) ?;
290
288
let bitgen = generator. lock ( ) ?;
291
289
assert ! ( generator. lock( ) . is_err( ) ) ;
292
- assert ! ( bitgen. try_drop ( py) . is_ok( ) ) ;
290
+ assert ! ( bitgen. try_release ( py) . is_ok( ) ) ;
293
291
Ok ( ( ) )
294
292
} )
295
293
}
0 commit comments