17
17
//! let random_number = Python::with_gil(|py| -> PyResult<_> {
18
18
//! let mut bitgen = default_bit_gen(py)?.lock()?;
19
19
//! // use bitgen without holding the GIL
20
- //! let r = py.allow_threads(|| bitgen.next_uint64 ())? ;
20
+ //! let r = py.allow_threads(|| bitgen.next_u64 ());
21
21
//! // release the lock manually while holding the GIL again
22
22
//! bitgen.release(py)?;
23
23
//! Ok(r)
@@ -175,27 +175,31 @@ impl<'py> PyBitGeneratorGuard {
175
175
pub fn next_u64 ( & mut self ) -> u64 {
176
176
unsafe {
177
177
let bitgen = self . raw_bitgen . as_ptr ( ) ;
178
+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
178
179
( ( * bitgen) . next_uint64 ) ( ( * bitgen) . state )
179
180
}
180
181
}
181
182
/// Returns the next random unsigned 32 bit integer.
182
183
pub fn next_u32 ( & mut self ) -> u32 {
183
184
unsafe {
184
185
let bitgen = self . raw_bitgen . as_ptr ( ) ;
186
+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
185
187
( ( * bitgen) . next_uint32 ) ( ( * bitgen) . state )
186
188
}
187
189
}
188
190
/// Returns the next random double.
189
191
pub fn next_double ( & mut self ) -> libc:: c_double {
190
192
unsafe {
191
193
let bitgen = self . raw_bitgen . as_ptr ( ) ;
194
+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
192
195
( ( * bitgen) . next_double ) ( ( * bitgen) . state )
193
196
}
194
197
}
195
198
/// Returns the next raw value (can be used for testing).
196
199
pub fn next_raw ( & mut self ) -> u64 {
197
200
unsafe {
198
201
let bitgen = self . raw_bitgen . as_ptr ( ) ;
202
+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
199
203
( ( * bitgen) . next_raw ) ( ( * bitgen) . state )
200
204
}
201
205
}
@@ -226,7 +230,6 @@ mod tests {
226
230
Ok ( bit_generator)
227
231
}
228
232
229
- /*
230
233
/// Test the primary use case: acquire the lock, release the GIL, then use the lock
231
234
#[ test]
232
235
fn use_outside_gil ( ) -> PyResult < ( ) > {
@@ -239,9 +242,9 @@ mod tests {
239
242
Ok ( ( ) )
240
243
} )
241
244
}
242
- */
243
245
244
246
/// More complex version of primary use case: use from multiple threads
247
+ #[ cfg( feature = "rand_core" ) ]
245
248
#[ test]
246
249
fn use_parallel ( ) -> PyResult < ( ) > {
247
250
use crate :: array:: { PyArray2 , PyArrayMethods as _} ;
@@ -274,8 +277,8 @@ mod tests {
274
277
} )
275
278
}
276
279
277
- /*
278
280
/// Test that the `rand::Rng` APIs work
281
+ #[ cfg( feature = "rand_core" ) ]
279
282
#[ test]
280
283
fn rand ( ) -> PyResult < ( ) > {
281
284
use rand:: Rng as _;
@@ -301,5 +304,4 @@ mod tests {
301
304
Ok ( ( ) )
302
305
} )
303
306
}
304
- */
305
307
}
0 commit comments