leodos_protocols/utils/cell.rs
1use core::cell::RefCell;
2
3/// Interior-mutable cell that only allows access through sync closures.
4///
5/// Wraps `RefCell<T>` but restricts borrows to non-`async` closures,
6/// making it a compile error to hold a borrow across an `.await` point.
7pub(crate) struct SyncRefCell<T>(RefCell<T>);
8
9impl<T> SyncRefCell<T> {
10 pub(crate) fn new(val: T) -> Self {
11 Self(RefCell::new(val))
12 }
13
14 #[allow(dead_code)]
15 pub(crate) fn with<F, R>(&self, f: F) -> R
16 where
17 F: FnOnce(&T) -> R,
18 {
19 f(&self.0.borrow())
20 }
21
22 pub(crate) fn with_mut<F, R>(&self, f: F) -> R
23 where
24 F: FnOnce(&mut T) -> R,
25 {
26 f(&mut self.0.borrow_mut())
27 }
28}