1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
extern crate atomic_refcell;
#[cfg(feature = "serde")]
extern crate serde;
use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
#[derive(Debug)]
struct Foo {
u: u32,
}
#[derive(Debug)]
struct Bar {
f: Foo,
}
impl Default for Bar {
fn default() -> Self {
Bar { f: Foo { u: 42 } }
}
}
// FIXME(bholley): Add tests to exercise this in concurrent scenarios.
#[test]
fn immutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow();
let _second = a.borrow();
}
#[test]
fn try_immutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.try_borrow().unwrap();
let _second = a.try_borrow().unwrap();
}
#[test]
fn mutable() {
let a = AtomicRefCell::new(Bar::default());
let _ = a.borrow_mut();
}
#[test]
fn try_mutable() {
let a = AtomicRefCell::new(Bar::default());
let _ = a.try_borrow_mut().unwrap();
}
#[test]
fn get_mut() {
let mut a = AtomicRefCell::new(Bar::default());
let _ = a.get_mut();
}
#[test]
fn interleaved() {
let a = AtomicRefCell::new(Bar::default());
{
let _ = a.borrow_mut();
}
{
let _first = a.borrow();
let _second = a.borrow();
}
{
let _ = a.borrow_mut();
}
}
#[test]
fn try_interleaved() {
let a = AtomicRefCell::new(Bar::default());
{
let _ = a.try_borrow_mut().unwrap();
}
{
let _first = a.try_borrow().unwrap();
let _second = a.try_borrow().unwrap();
let _ = a.try_borrow_mut().unwrap_err();
}
{
let _first = a.try_borrow_mut().unwrap();
let _ = a.try_borrow().unwrap_err();
}
}
// For Miri to catch issues when calling a function.
//
// See how this scenerio affects std::cell::RefCell implementation:
// https://github.com/rust-lang/rust/issues/63787
//
// Also see relevant unsafe code guidelines issue:
// https://github.com/rust-lang/unsafe-code-guidelines/issues/125
#[test]
fn drop_and_borrow_in_fn_call() {
fn drop_and_borrow(cell: &AtomicRefCell<Bar>, borrow: AtomicRef<'_, Bar>) {
drop(borrow);
*cell.borrow_mut() = Bar::default();
}
let a = AtomicRefCell::new(Bar::default());
let borrow = a.borrow();
drop_and_borrow(&a, borrow);
}
#[test]
#[should_panic(expected = "already immutably borrowed")]
fn immutable_then_mutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow();
let _second = a.borrow_mut();
}
#[test]
fn immutable_then_try_mutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow();
let _second = a.try_borrow_mut().unwrap_err();
}
#[test]
#[should_panic(expected = "already mutably borrowed")]
fn mutable_then_immutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow_mut();
let _second = a.borrow();
}
#[test]
fn mutable_then_try_immutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow_mut();
let _second = a.try_borrow().unwrap_err();
}
#[test]
#[should_panic(expected = "already mutably borrowed")]
fn double_mutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow_mut();
let _second = a.borrow_mut();
}
#[test]
fn mutable_then_try_mutable() {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow_mut();
let _second = a.try_borrow_mut().unwrap_err();
}
#[test]
fn map() {
let a = AtomicRefCell::new(Bar::default());
let b = a.borrow();
assert_eq!(b.f.u, 42);
let c = AtomicRef::map(b, |x| &x.f);
assert_eq!(c.u, 42);
let d = AtomicRef::map(c, |x| &x.u);
assert_eq!(*d, 42);
}
#[test]
fn map_mut() {
let a = AtomicRefCell::new(Bar::default());
let mut b = a.borrow_mut();
assert_eq!(b.f.u, 42);
b.f.u = 43;
let mut c = AtomicRefMut::map(b, |x| &mut x.f);
assert_eq!(c.u, 43);
c.u = 44;
let mut d = AtomicRefMut::map(c, |x| &mut x.u);
assert_eq!(*d, 44);
*d = 45;
assert_eq!(*d, 45);
}
#[test]
fn debug_fmt() {
let a = AtomicRefCell::new(Foo { u: 42 });
assert_eq!(format!("{:?}", a), "AtomicRefCell { value: Foo { u: 42 } }");
}
#[test]
fn debug_fmt_borrowed() {
let a = AtomicRefCell::new(Foo { u: 42 });
let _b = a.borrow();
assert_eq!(format!("{:?}", a), "AtomicRefCell { value: Foo { u: 42 } }");
}
#[test]
fn debug_fmt_borrowed_mut() {
let a = AtomicRefCell::new(Foo { u: 42 });
let _b = a.borrow_mut();
assert_eq!(format!("{:?}", a), "AtomicRefCell { value: <borrowed> }");
}
#[test]
#[cfg(feature = "serde")]
fn serde() {
let value = 10;
let cell = AtomicRefCell::new(value);
let serialized = serde_json::to_string(&cell).unwrap();
let deserialized = serde_json::from_str::<AtomicRefCell<usize>>(&serialized).unwrap();
assert_eq!(*deserialized.borrow(), value);
}
|