cow.rs 1.0 KB

12345678910111213141516171819202122232425262728
  1. use crate::entry::{ItemEntry, XEntry};
  2. use crate::lock::XLock;
  3. use crate::node::deep_copy_node_entry;
  4. /// The COW trait provides the capability for Copy-On-Write (COW) behavior to XEntries with Clone ability.
  5. pub(super) trait Cow<I: ItemEntry, L: XLock> {
  6. /// Check if the target entry that is about to be operated on need to perform COW.
  7. /// If the target entry is subject to a mutable operation and is shared with other XArrays,
  8. /// perform the COW and return the copied XEntry with `Some()`, else return `None`.
  9. fn copy_if_shared(&self) -> Option<XEntry<I, L>>;
  10. }
  11. impl<I: ItemEntry, L: XLock> Cow<I, L> for XEntry<I, L> {
  12. default fn copy_if_shared(&self) -> Option<XEntry<I, L>> {
  13. None
  14. }
  15. }
  16. impl<I: ItemEntry + Clone, L: XLock> Cow<I, L> for XEntry<I, L> {
  17. fn copy_if_shared(&self) -> Option<XEntry<I, L>> {
  18. if self.is_node() && self.node_strong_count().unwrap() > 1 {
  19. let new_entry = deep_copy_node_entry(self);
  20. Some(new_entry)
  21. } else {
  22. None
  23. }
  24. }
  25. }