arc_cow.rs

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