use anyhow::Result as AnyResult;
use std::cell::{Ref, RefCell, RefMut};
use std::rc::{Rc, Weak};
#[derive(Debug)]
pub(in super::super) struct SyncUnsafeRcRefCell<T>(pub(super) Rc<RefCell<T>>);
impl<T> SyncUnsafeRcRefCell<T> {
pub(in super::super) fn new(t: T) -> Self {
SyncUnsafeRcRefCell(Rc::new(RefCell::new(t)))
}
pub(in super::super) fn new_cyclic<F>(f: F) -> Self
where
F: FnOnce(SyncUnsafeWeakRefCell<T>) -> T,
{
let r =Rc::new_cyclic(|d: &Weak<RefCell<T>>| {
RefCell::new(f(SyncUnsafeWeakRefCell(Weak::clone(d))))
});
SyncUnsafeRcRefCell(r)
}
pub(in super::super) fn unsafe_downgrade(&self) -> SyncUnsafeWeakRefCell<T> {
SyncUnsafeWeakRefCell(Rc::downgrade(&self.0))
}
pub(in super::super) fn unsafe_try_borrow_mut(&self) -> AnyResult<RefMut<'_, T>> {
self.0.try_borrow_mut().or_else(|e| Err(e.into()))
}
pub(in super::super) fn unsafe_try_borrow(&self) -> AnyResult<Ref<'_, T>> {
self.0.try_borrow().or_else(|e| Err(e.into()))
}
pub(in super::super) fn unsafe_clone(&self) -> Self {
SyncUnsafeRcRefCell(self.0.clone())
}
}
unsafe impl<T> Send for SyncUnsafeRcRefCell<T> {}
#[derive(Debug)]
pub(in super::super) struct SyncUnsafeWeakRefCell<T>(pub(super) Weak<RefCell<T>>);
impl<T> SyncUnsafeWeakRefCell<T> {
pub(in super::super) fn unsafe_upgrade(&self) -> Option<SyncUnsafeRcRefCell<T>> {
self.0.upgrade().map(|r| SyncUnsafeRcRefCell(r))
}
pub(in super::super) fn unsafe_clone(&self) -> Self {
SyncUnsafeWeakRefCell(self.0.clone())
}
}
unsafe impl<T> Send for SyncUnsafeWeakRefCell<T> {}