proptest_arbitrary/_std/
cell.rs

1//! Arbitrary implementations for `std::cell`.
2
3use std::cell::{Cell, RefCell, UnsafeCell, BorrowError, BorrowMutError};
4
5wrap_from!([Copy] Cell);
6wrap_from!(RefCell);
7wrap_from!(UnsafeCell);
8
9generator!(BorrowError, || {
10    // False positive:
11    #[cfg_attr(feature = "cargo-clippy", allow(let_and_return))]
12    {
13        let _rc = RefCell::new(());
14        let _bm = _rc.borrow_mut();
15        let _tb = _rc.try_borrow();
16        let ret = match _rc.try_borrow() {
17            Ok(_) => panic!("should never happen!"),
18            Err(e) => e,
19        };
20        ret
21    }
22});
23generator!(BorrowMutError, || {
24    // False positive:
25    #[cfg_attr(feature = "cargo-clippy", allow(let_and_return))]
26    {
27        let _rc = RefCell::new(());
28        let _bm = _rc.borrow_mut();
29        let _tb = _rc.try_borrow();
30        let ret = match _rc.try_borrow_mut() {
31            Ok(_) => panic!("should never happen!"),
32            Err(e) => e,
33        };
34        ret
35    }
36});
37
38#[cfg(test)]
39mod test {
40    no_panic_test!(
41        cell => Cell<u8>,
42        ref_cell => RefCell<u8>,
43        unsafe_cell => UnsafeCell<u8>
44    );
45}