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<T: ?Sized + PartialEq> PartialEq for ArcCow<'_, 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<T: ?Sized + PartialOrd> PartialOrd for ArcCow<'_, T> {
23 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
24 self.as_ref().partial_cmp(other.as_ref())
25 }
26}
27
28impl<T: ?Sized + Ord> Ord for ArcCow<'_, T> {
29 fn cmp(&self, other: &Self) -> Ordering {
30 self.as_ref().cmp(other.as_ref())
31 }
32}
33
34impl<T: ?Sized + Eq> Eq for ArcCow<'_, T> {}
35
36impl<T: ?Sized + Hash> Hash for ArcCow<'_, 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<T: ?Sized> Clone for ArcCow<'_, 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 From<&String> for ArcCow<'_, str> {
79 fn from(value: &String) -> Self {
80 Self::Owned(value.clone().into())
81 }
82}
83
84impl<'a> From<Cow<'a, str>> for ArcCow<'a, str> {
85 fn from(value: Cow<'a, str>) -> Self {
86 match value {
87 Cow::Borrowed(borrowed) => Self::Borrowed(borrowed),
88 Cow::Owned(owned) => Self::Owned(owned.into()),
89 }
90 }
91}
92
93impl<T> From<Vec<T>> for ArcCow<'_, [T]> {
94 fn from(vec: Vec<T>) -> Self {
95 ArcCow::Owned(Arc::from(vec))
96 }
97}
98
99impl<'a> From<&'a str> for ArcCow<'a, [u8]> {
100 fn from(s: &'a str) -> Self {
101 ArcCow::Borrowed(s.as_bytes())
102 }
103}
104
105impl<T: ?Sized + ToOwned> std::borrow::Borrow<T> for ArcCow<'_, T> {
106 fn borrow(&self) -> &T {
107 match self {
108 ArcCow::Borrowed(borrowed) => borrowed,
109 ArcCow::Owned(owned) => owned.as_ref(),
110 }
111 }
112}
113
114impl<T: ?Sized> std::ops::Deref for ArcCow<'_, T> {
115 type Target = T;
116
117 fn deref(&self) -> &Self::Target {
118 match self {
119 ArcCow::Borrowed(s) => s,
120 ArcCow::Owned(s) => s.as_ref(),
121 }
122 }
123}
124
125impl<T: ?Sized> AsRef<T> for ArcCow<'_, T> {
126 fn as_ref(&self) -> &T {
127 match self {
128 ArcCow::Borrowed(borrowed) => borrowed,
129 ArcCow::Owned(owned) => owned.as_ref(),
130 }
131 }
132}
133
134impl<T: ?Sized + Debug> Debug for ArcCow<'_, T> {
135 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136 match self {
137 ArcCow::Borrowed(borrowed) => Debug::fmt(borrowed, f),
138 ArcCow::Owned(owned) => Debug::fmt(&**owned, f),
139 }
140 }
141}