arc_cow.rs

 1use std::sync::Arc;
 2
 3#[derive(PartialEq, Eq)]
 4pub enum ArcCow<'a, T: ?Sized> {
 5    Borrowed(&'a T),
 6    Owned(Arc<T>),
 7}
 8
 9use std::hash::{Hash, Hasher};
10
11impl<'a, T: ?Sized + Hash> Hash for ArcCow<'a, T> {
12    fn hash<H: Hasher>(&self, state: &mut H) {
13        match self {
14            Self::Borrowed(borrowed) => Hash::hash(borrowed, state),
15            Self::Owned(owned) => Hash::hash(&**owned, state),
16        }
17    }
18}
19
20impl<'a, T: ?Sized> Clone for ArcCow<'a, T> {
21    fn clone(&self) -> Self {
22        match self {
23            Self::Borrowed(borrowed) => Self::Borrowed(borrowed),
24            Self::Owned(owned) => Self::Owned(owned.clone()),
25        }
26    }
27}
28
29impl<'a, T: ?Sized> From<&'a T> for ArcCow<'a, T> {
30    fn from(s: &'a T) -> Self {
31        Self::Borrowed(s)
32    }
33}
34
35impl<T> From<Arc<T>> for ArcCow<'_, T> {
36    fn from(s: Arc<T>) -> Self {
37        Self::Owned(s)
38    }
39}
40
41impl From<String> for ArcCow<'_, str> {
42    fn from(value: String) -> Self {
43        Self::Owned(value.into())
44    }
45}
46
47impl<'a, T: ?Sized + ToOwned> std::borrow::Borrow<T> for ArcCow<'a, T> {
48    fn borrow(&self) -> &T {
49        match self {
50            ArcCow::Borrowed(borrowed) => borrowed,
51            ArcCow::Owned(owned) => owned.as_ref(),
52        }
53    }
54}
55
56impl<T: ?Sized> std::ops::Deref for ArcCow<'_, T> {
57    type Target = T;
58
59    fn deref(&self) -> &Self::Target {
60        match self {
61            ArcCow::Borrowed(s) => s,
62            ArcCow::Owned(s) => s.as_ref(),
63        }
64    }
65}
66
67impl<T: ?Sized> AsRef<T> for ArcCow<'_, T> {
68    fn as_ref(&self) -> &T {
69        match self {
70            ArcCow::Borrowed(borrowed) => borrowed,
71            ArcCow::Owned(owned) => owned.as_ref(),
72        }
73    }
74}