use std::rc::Rc;
use cranpose_ui_layout::ParentData;
#[derive(Clone, Debug, Default)]
pub(crate) struct SharedParentData(Option<Rc<ParentData>>);
impl SharedParentData {
pub(crate) fn of(data: ParentData) -> Self {
Self((data != ParentData::default()).then(|| Rc::new(data)))
}
pub(crate) fn set(&mut self, data: ParentData) {
match self.0.as_mut().and_then(Rc::get_mut) {
Some(held) if data != ParentData::default() => *held = data,
_ => *self = Self::of(data),
}
}
pub(crate) fn read<R>(&self, read: impl FnOnce(&ParentData) -> R) -> R {
match &self.0 {
Some(data) => read(data),
None => read(&ParentData::default()),
}
}
pub(crate) fn same(&self, other: &Self) -> bool {
match (&self.0, &other.0) {
(None, None) => true,
(Some(data), Some(other)) => Rc::ptr_eq(data, other),
_ => false,
}
}
}