use crate::entry::{ItemEntry, XEntry}; use crate::lock::XLock; use crate::node::deep_copy_node_entry; /// The COW trait provides the capability for Copy-On-Write (COW) behavior to XEntries with Clone ability. pub(super) trait Cow { /// Check if the target entry that is about to be operated on need to perform COW. /// If the target entry is subject to a mutable operation and is shared with other XArrays, /// perform the COW and return the copied XEntry with `Some()`, else return `None`. fn copy_if_shared(&self) -> Option>; } impl Cow for XEntry { default fn copy_if_shared(&self) -> Option> { None } } impl Cow for XEntry { fn copy_if_shared(&self) -> Option> { if self.is_node() && self.node_strong_count().unwrap() > 1 { let new_entry = deep_copy_node_entry(self); Some(new_entry) } else { None } } }