1717//! let random_number = Python::with_gil(|py| -> PyResult<_> {
1818//! let mut bitgen = default_bit_gen(py)?.lock()?;
1919//! // use bitgen without holding the GIL
20- //! let r = py.allow_threads(|| bitgen.next_uint64 ())? ;
20+ //! let r = py.allow_threads(|| bitgen.next_u64 ());
2121//! // release the lock manually while holding the GIL again
2222//! bitgen.release(py)?;
2323//! Ok(r)
@@ -175,27 +175,31 @@ impl<'py> PyBitGeneratorGuard {
175175 pub fn next_u64 ( & mut self ) -> u64 {
176176 unsafe {
177177 let bitgen = self . raw_bitgen . as_ptr ( ) ;
178+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
178179 ( ( * bitgen) . next_uint64 ) ( ( * bitgen) . state )
179180 }
180181 }
181182 /// Returns the next random unsigned 32 bit integer.
182183 pub fn next_u32 ( & mut self ) -> u32 {
183184 unsafe {
184185 let bitgen = self . raw_bitgen . as_ptr ( ) ;
186+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
185187 ( ( * bitgen) . next_uint32 ) ( ( * bitgen) . state )
186188 }
187189 }
188190 /// Returns the next random double.
189191 pub fn next_double ( & mut self ) -> libc:: c_double {
190192 unsafe {
191193 let bitgen = self . raw_bitgen . as_ptr ( ) ;
194+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
192195 ( ( * bitgen) . next_double ) ( ( * bitgen) . state )
193196 }
194197 }
195198 /// Returns the next raw value (can be used for testing).
196199 pub fn next_raw ( & mut self ) -> u64 {
197200 unsafe {
198201 let bitgen = self . raw_bitgen . as_ptr ( ) ;
202+ debug_assert_ne ! ( ( * bitgen) . state, std:: ptr:: null_mut( ) ) ;
199203 ( ( * bitgen) . next_raw ) ( ( * bitgen) . state )
200204 }
201205 }
@@ -226,7 +230,6 @@ mod tests {
226230 Ok ( bit_generator)
227231 }
228232
229- /*
230233 /// Test the primary use case: acquire the lock, release the GIL, then use the lock
231234 #[ test]
232235 fn use_outside_gil ( ) -> PyResult < ( ) > {
@@ -239,9 +242,9 @@ mod tests {
239242 Ok ( ( ) )
240243 } )
241244 }
242- */
243245
244246 /// More complex version of primary use case: use from multiple threads
247+ #[ cfg( feature = "rand_core" ) ]
245248 #[ test]
246249 fn use_parallel ( ) -> PyResult < ( ) > {
247250 use crate :: array:: { PyArray2 , PyArrayMethods as _} ;
@@ -274,8 +277,8 @@ mod tests {
274277 } )
275278 }
276279
277- /*
278280 /// Test that the `rand::Rng` APIs work
281+ #[ cfg( feature = "rand_core" ) ]
279282 #[ test]
280283 fn rand ( ) -> PyResult < ( ) > {
281284 use rand:: Rng as _;
@@ -301,5 +304,4 @@ mod tests {
301304 Ok ( ( ) )
302305 } )
303306 }
304- */
305307}
0 commit comments