@@ -138,6 +138,8 @@ pub struct PyBitGeneratorGuard<'py> {
138
138
lock : Bound < ' py , PyAny > ,
139
139
}
140
140
141
+ // SAFETY: we can’t have public APIs that access the Python objects,
142
+ // only the `raw_bitgen` pointer.
141
143
unsafe impl Send for PyBitGeneratorGuard < ' _ > { }
142
144
143
145
impl Drop for PyBitGeneratorGuard < ' _ > {
@@ -150,8 +152,10 @@ impl Drop for PyBitGeneratorGuard<'_> {
150
152
// SAFETY: We hold the `BitGenerator.lock`,
151
153
// so nothing apart from us is allowed to change its state.
152
154
impl PyBitGeneratorGuard < ' _ > {
153
- /// Drop the lock, allowing access to.
154
- pub fn try_drop ( self ) -> PyResult < ( ) > {
155
+ /// Drop the lock manually before `Drop::drop` tries to do it (used for testing).
156
+ /// SAFETY: Can’t be used inside of a
157
+ #[ allow( dead_code) ]
158
+ unsafe fn try_drop ( self ) -> PyResult < ( ) > {
155
159
self . lock . call_method0 ( "release" ) ?;
156
160
Ok ( ( ) )
157
161
}
@@ -219,7 +223,7 @@ mod tests {
219
223
py. allow_threads ( || {
220
224
let _ = bitgen. next_raw ( ) ;
221
225
} ) ;
222
- assert ! ( bitgen. try_drop( ) . is_ok( ) ) ;
226
+ assert ! ( unsafe { bitgen. try_drop( ) } . is_ok( ) ) ;
223
227
Ok ( ( ) )
224
228
} )
225
229
}
@@ -236,7 +240,7 @@ mod tests {
236
240
assert ! ( bitgen. random_ratio( 1 , 1 ) ) ;
237
241
assert ! ( !bitgen. random_ratio( 0 , 1 ) ) ;
238
242
} ) ;
239
- assert ! ( bitgen. try_drop( ) . is_ok( ) ) ;
243
+ assert ! ( unsafe { bitgen. try_drop( ) } . is_ok( ) ) ;
240
244
Ok ( ( ) )
241
245
} )
242
246
}
@@ -247,7 +251,7 @@ mod tests {
247
251
let generator = get_bit_generator ( py) ?;
248
252
let bitgen = generator. lock ( ) ?;
249
253
assert ! ( generator. lock( ) . is_err( ) ) ;
250
- assert ! ( bitgen. try_drop( ) . is_ok( ) ) ;
254
+ assert ! ( unsafe { bitgen. try_drop( ) } . is_ok( ) ) ;
251
255
Ok ( ( ) )
252
256
} )
253
257
}
0 commit comments