use crate::core::node::StructureNode;
#[cfg(not(feature = "mutli-thread"))]
mod impl_ {
use super::StructureNode;
use std::cell::{Ref, RefCell, RefMut};
use std::rc::Rc;
pub type RootNode = Rc<RefCell<StructureNode>>;
pub fn new_root_node(node: StructureNode) -> RootNode {
Rc::new(RefCell::new(node))
}
pub type RootReadGuard<'a> = Ref<'a, StructureNode>;
pub type RootWriteGuard<'a> = RefMut<'a, StructureNode>;
pub fn root_read(root: &RootNode) -> RootReadGuard<'_> {
root.borrow()
}
pub fn root_write(root: &RootNode) -> RootWriteGuard<'_> {
root.borrow_mut()
}
pub fn root_read_with<F, R>(root: &RootNode, f: F) -> R
where
F: FnOnce(&StructureNode) -> R,
{
let guard = root_read(root);
f(&guard)
}
}
#[cfg(feature = "mutli-thread")]
mod impl_ {
use super::StructureNode;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub type RootNode = Arc<RwLock<StructureNode>>;
pub fn new_root_node(node: StructureNode) -> RootNode {
Arc::new(RwLock::new(node))
}
pub type RootReadGuard<'a> = RwLockReadGuard<'a, StructureNode>;
pub type RootWriteGuard<'a> = RwLockWriteGuard<'a, StructureNode>;
pub fn root_read(
root: &RootNode,
) -> Result<RootReadGuard<'_>, std::sync::PoisonError<RwLockReadGuard<'_, StructureNode>>> {
root.read()
}
pub fn root_write(
root: &RootNode,
) -> Result<RootWriteGuard<'_>, std::sync::PoisonError<RwLockWriteGuard<'_, StructureNode>>>
{
root.write()
}
pub fn root_read_with<F, R>(root: &RootNode, f: F) -> R
where
F: FnOnce(&StructureNode) -> R,
R: Default,
{
match root_read(root) {
Ok(guard) => f(&guard),
Err(_) => R::default(),
}
}
}
pub use impl_::{new_root_node, root_read, root_read_with, root_write, RootNode};