//! [`Shared`]: a value shared between copies of a node until one of them changes it. use std::sync::Arc; use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// A copy-on-write value: cloning shares it, and the first change through `DerefMut` makes the /// changed copy its own (`Arc::make_mut`). A node moved or recoloured keeps sharing what it didn't /// change with the version the undo history holds. Reads, writes, equality and the saved form are /// exactly those of the value itself. #[derive(Clone, Default)] pub struct Shared(Arc); impl Shared { pub fn new(v: T) -> Self { Shared(Arc::new(v)) } /// Whether `a` and `b` share one value (an unchanged copy). pub fn ptr_eq(a: &Self, b: &Self) -> bool { Arc::ptr_eq(&a.0, &b.0) } } impl std::ops::Deref for Shared { type Target = T; fn deref(&self) -> &T { &self.0 } } impl std::ops::DerefMut for Shared { fn deref_mut(&mut self) -> &mut T { Arc::make_mut(&mut self.0) } } impl From for Shared { fn from(v: T) -> Self { Shared::new(v) } } impl PartialEq for Shared { fn eq(&self, other: &Self) -> bool { Arc::ptr_eq(&self.0, &other.0) || *self.0 == *other.0 } } /// A shared value equals the plain value it holds (`node.appearance == style.appearance`). impl PartialEq for Shared { fn eq(&self, other: &T) -> bool { *self.0 == *other } } impl std::fmt::Debug for Shared { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl Serialize for Shared { fn serialize(&self, s: S) -> Result { self.0.serialize(s) } } impl<'de, T: Deserialize<'de>> Deserialize<'de> for Shared { fn deserialize>(d: D) -> Result { T::deserialize(d).map(Shared::new) } }