arc_cow.rs

  1use std::{
  2    borrow::Cow,
  3    cmp::Ordering,
  4    fmt::{self, Debug},
  5    hash::{Hash, Hasher},
  6    sync::Arc,
  7};
  8
  9pub enum ArcCow<'a, T: ?Sized> {
 10    Borrowed(&'a T),
 11    Owned(Arc<T>),
 12}
 13
 14impl<'a, T: ?Sized + PartialEq> PartialEq for ArcCow<'a, T> {
 15    fn eq(&self, other: &Self) -> bool {
 16        let a = self.as_ref();
 17        let b = other.as_ref();
 18        a == b
 19    }
 20}
 21
 22impl<'a, T: ?Sized + PartialOrd> PartialOrd for ArcCow<'a, T> {
 23    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
 24        self.as_ref().partial_cmp(other.as_ref())
 25    }
 26}
 27
 28impl<'a, T: ?Sized + Ord> Ord for ArcCow<'a, T> {
 29    fn cmp(&self, other: &Self) -> Ordering {
 30        self.as_ref().cmp(other.as_ref())
 31    }
 32}
 33
 34impl<'a, T: ?Sized + Eq> Eq for ArcCow<'a, T> {}
 35
 36impl<'a, T: ?Sized + Hash> Hash for ArcCow<'a, T> {
 37    fn hash<H: Hasher>(&self, state: &mut H) {
 38        match self {
 39            Self::Borrowed(borrowed) => Hash::hash(borrowed, state),
 40            Self::Owned(owned) => Hash::hash(&**owned, state),
 41        }
 42    }
 43}
 44
 45impl<'a, T: ?Sized> Clone for ArcCow<'a, T> {
 46    fn clone(&self) -> Self {
 47        match self {
 48            Self::Borrowed(borrowed) => Self::Borrowed(borrowed),
 49            Self::Owned(owned) => Self::Owned(owned.clone()),
 50        }
 51    }
 52}
 53
 54impl<'a, T: ?Sized> From<&'a T> for ArcCow<'a, T> {
 55    fn from(s: &'a T) -> Self {
 56        Self::Borrowed(s)
 57    }
 58}
 59
 60impl<T: ?Sized> From<Arc<T>> for ArcCow<'_, T> {
 61    fn from(s: Arc<T>) -> Self {
 62        Self::Owned(s)
 63    }
 64}
 65
 66impl<T: ?Sized> From<&'_ Arc<T>> for ArcCow<'_, T> {
 67    fn from(s: &'_ Arc<T>) -> Self {
 68        Self::Owned(s.clone())
 69    }
 70}
 71
 72impl From<String> for ArcCow<'_, str> {
 73    fn from(value: String) -> Self {
 74        Self::Owned(value.into())
 75    }
 76}
 77
 78impl<'a> From<Cow<'a, str>> for ArcCow<'a, str> {
 79    fn from(value: Cow<'a, str>) -> Self {
 80        match value {
 81            Cow::Borrowed(borrowed) => Self::Borrowed(borrowed),
 82            Cow::Owned(owned) => Self::Owned(owned.into()),
 83        }
 84    }
 85}
 86
 87impl<T> From<Vec<T>> for ArcCow<'_, [T]> {
 88    fn from(vec: Vec<T>) -> Self {
 89        ArcCow::Owned(Arc::from(vec))
 90    }
 91}
 92
 93impl<'a> From<&'a str> for ArcCow<'a, [u8]> {
 94    fn from(s: &'a str) -> Self {
 95        ArcCow::Borrowed(s.as_bytes())
 96    }
 97}
 98
 99impl<'a, T: ?Sized + ToOwned> std::borrow::Borrow<T> for ArcCow<'a, T> {
100    fn borrow(&self) -> &T {
101        match self {
102            ArcCow::Borrowed(borrowed) => borrowed,
103            ArcCow::Owned(owned) => owned.as_ref(),
104        }
105    }
106}
107
108impl<T: ?Sized> std::ops::Deref for ArcCow<'_, T> {
109    type Target = T;
110
111    fn deref(&self) -> &Self::Target {
112        match self {
113            ArcCow::Borrowed(s) => s,
114            ArcCow::Owned(s) => s.as_ref(),
115        }
116    }
117}
118
119impl<T: ?Sized> AsRef<T> for ArcCow<'_, T> {
120    fn as_ref(&self) -> &T {
121        match self {
122            ArcCow::Borrowed(borrowed) => borrowed,
123            ArcCow::Owned(owned) => owned.as_ref(),
124        }
125    }
126}
127
128impl<'a, T: ?Sized + Debug> Debug for ArcCow<'a, T> {
129    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130        match self {
131            ArcCow::Borrowed(borrowed) => Debug::fmt(borrowed, f),
132            ArcCow::Owned(owned) => Debug::fmt(&**owned, f),
133        }
134    }
135}