| 1 |
extern crate atomic_refcell; |
| 2 |
|
| 3 |
use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut}; |
| 4 |
|
| 5 |
#[derive(Debug)] |
| 6 |
struct Foo { |
| 7 |
u: u32, |
| 8 |
} |
| 9 |
|
| 10 |
#[derive(Debug)] |
| 11 |
struct Bar { |
| 12 |
f: Foo, |
| 13 |
} |
| 14 |
|
| 15 |
impl Default for Bar { |
| 16 |
fn default() -> Self { |
| 17 |
Bar { f: Foo { u: 42 } } |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// FIXME(bholley): Add tests to exercise this in concurrent scenarios. |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn immutable() { |
| 25 |
let a = AtomicRefCell::new(Bar::default()); |
| 26 |
let _first = a.borrow(); |
| 27 |
let _second = a.borrow(); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn try_immutable() { |
| 32 |
let a = AtomicRefCell::new(Bar::default()); |
| 33 |
let _first = a.try_borrow().unwrap(); |
| 34 |
let _second = a.try_borrow().unwrap(); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn mutable() { |
| 39 |
let a = AtomicRefCell::new(Bar::default()); |
| 40 |
let _ = a.borrow_mut(); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn try_mutable() { |
| 45 |
let a = AtomicRefCell::new(Bar::default()); |
| 46 |
let _ = a.try_borrow_mut().unwrap(); |
| 47 |
} |
| 48 |
|
| 49 |
#[test] |
| 50 |
fn get_mut() { |
| 51 |
let mut a = AtomicRefCell::new(Bar::default()); |
| 52 |
let _ = a.get_mut(); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn interleaved() { |
| 57 |
let a = AtomicRefCell::new(Bar::default()); |
| 58 |
{ |
| 59 |
let _ = a.borrow_mut(); |
| 60 |
} |
| 61 |
{ |
| 62 |
let _first = a.borrow(); |
| 63 |
let _second = a.borrow(); |
| 64 |
} |
| 65 |
{ |
| 66 |
let _ = a.borrow_mut(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn try_interleaved() { |
| 72 |
let a = AtomicRefCell::new(Bar::default()); |
| 73 |
{ |
| 74 |
let _ = a.try_borrow_mut().unwrap(); |
| 75 |
} |
| 76 |
{ |
| 77 |
let _first = a.try_borrow().unwrap(); |
| 78 |
let _second = a.try_borrow().unwrap(); |
| 79 |
let _ = a.try_borrow_mut().unwrap_err(); |
| 80 |
} |
| 81 |
{ |
| 82 |
let _first = a.try_borrow_mut().unwrap(); |
| 83 |
let _ = a.try_borrow().unwrap_err(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
#[should_panic(expected = "already immutably borrowed")] |
| 89 |
fn immutable_then_mutable() { |
| 90 |
let a = AtomicRefCell::new(Bar::default()); |
| 91 |
let _first = a.borrow(); |
| 92 |
let _second = a.borrow_mut(); |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
fn immutable_then_try_mutable() { |
| 97 |
let a = AtomicRefCell::new(Bar::default()); |
| 98 |
let _first = a.borrow(); |
| 99 |
let _second = a.try_borrow_mut().unwrap_err(); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
#[should_panic(expected = "already mutably borrowed")] |
| 104 |
fn mutable_then_immutable() { |
| 105 |
let a = AtomicRefCell::new(Bar::default()); |
| 106 |
let _first = a.borrow_mut(); |
| 107 |
let _second = a.borrow(); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn mutable_then_try_immutable() { |
| 112 |
let a = AtomicRefCell::new(Bar::default()); |
| 113 |
let _first = a.borrow_mut(); |
| 114 |
let _second = a.try_borrow().unwrap_err(); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
#[should_panic(expected = "already mutably borrowed")] |
| 119 |
fn double_mutable() { |
| 120 |
let a = AtomicRefCell::new(Bar::default()); |
| 121 |
let _first = a.borrow_mut(); |
| 122 |
let _second = a.borrow_mut(); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn mutable_then_try_mutable() { |
| 127 |
let a = AtomicRefCell::new(Bar::default()); |
| 128 |
let _first = a.borrow_mut(); |
| 129 |
let _second = a.try_borrow_mut().unwrap_err(); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn map() { |
| 134 |
let a = AtomicRefCell::new(Bar::default()); |
| 135 |
let b = a.borrow(); |
| 136 |
assert_eq!(b.f.u, 42); |
| 137 |
let c = AtomicRef::map(b, |x| &x.f); |
| 138 |
assert_eq!(c.u, 42); |
| 139 |
let d = AtomicRef::map(c, |x| &x.u); |
| 140 |
assert_eq!(*d, 42); |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn map_mut() { |
| 145 |
let a = AtomicRefCell::new(Bar::default()); |
| 146 |
let mut b = a.borrow_mut(); |
| 147 |
assert_eq!(b.f.u, 42); |
| 148 |
b.f.u = 43; |
| 149 |
let mut c = AtomicRefMut::map(b, |x| &mut x.f); |
| 150 |
assert_eq!(c.u, 43); |
| 151 |
c.u = 44; |
| 152 |
let mut d = AtomicRefMut::map(c, |x| &mut x.u); |
| 153 |
assert_eq!(*d, 44); |
| 154 |
*d = 45; |
| 155 |
assert_eq!(*d, 45); |
| 156 |
} |