#![deny(clippy::all, clippy::pedantic, unused)]
use std::cell::RefCell;
use std::sync::Arc;
#[derive(Default, Clone, PartialOrd, PartialEq)]
#[non_exhaustive]
pub struct Wrapper<T> {
data: Arc<RefCell<T>>,
}
impl<T> std::fmt::Debug for Wrapper<T>
where
T: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.data)
}
}
impl<T> Wrapper<T>
where
T: Clone,
{
pub fn new(data: T) -> Self {
Self {
data: Arc::new(RefCell::new(data)),
}
}
pub fn reset(&self, ndata: T) -> &Self {
*self.data.borrow_mut() = ndata;
self
}
pub fn get(&self) -> T {
self.data.borrow().clone()
}
pub fn borrow(&self) -> std::cell::Ref<'_, T> {
self.data.borrow()
}
pub fn map<F, U>(&self, f: F) -> U
where
F: FnOnce(&T) -> U,
{
let borrow = self.data.borrow();
f(&borrow)
}
}
#[cfg(test)]
mod unittest;