arc_cow.rs

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