refcell_lock_api/
lib.rs

1#![cfg_attr(not(test), no_std)]
2#![doc = include_str!("../README.md")]
3
4pub mod raw;
5
6/// A single-threaded [lock_api::Mutex] using a [RefCell](core::cell::RefCell) internally.
7///
8/// A [CellRwLock] is typically more useful,
9/// and has no additional overhead.
10pub type CellMutex<T> = lock_api::Mutex<raw::CellMutex, T>;
11
12/// A single-threaded [lock_api::RwLock] using a [RefCell](core::cell::RefCell) internally.
13///
14/// Useful to abstract between single-threaded and multi-threaded code.
15pub type CellRwLock<T> = lock_api::RwLock<raw::CellRwLock, T>;
16
17#[cfg(test)]
18mod test {
19    use super::CellRwLock;
20
21    #[test]
22    fn basic_rwlock() {
23        let lock = CellRwLock::new(vec![7i32]);
24        {
25            let guard = lock.read();
26            assert_eq!(*guard, vec![7]);
27        }
28        {
29            let mut guard = lock.write();
30            guard.push(18);
31            guard.push(19);
32        }
33        {
34            let guard = lock.read();
35            assert_eq!(*guard, vec![7, 18, 19]);
36            {
37                let guard = lock.read();
38                assert_eq!(guard.first(), Some(&7));
39                assert_eq!(guard.last(), Some(&19))
40            }
41        }
42        {
43            let mut guard = lock.write();
44            guard.push(42);
45        }
46        assert_eq!(lock.into_inner(), vec![7, 18, 19, 42]);
47    }
48}