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<'a, T: ?Sized + ToOwned> std::borrow::Borrow<T> for ArcCow<'a, T> {
61 fn borrow(&self) -> &T {
62 match self {
63 ArcCow::Borrowed(borrowed) => borrowed,
64 ArcCow::Owned(owned) => owned.as_ref(),
65 }
66 }
67}
68
69impl<T: ?Sized> std::ops::Deref for ArcCow<'_, T> {
70 type Target = T;
71
72 fn deref(&self) -> &Self::Target {
73 match self {
74 ArcCow::Borrowed(s) => s,
75 ArcCow::Owned(s) => s.as_ref(),
76 }
77 }
78}
79
80impl<T: ?Sized> AsRef<T> for ArcCow<'_, T> {
81 fn as_ref(&self) -> &T {
82 match self {
83 ArcCow::Borrowed(borrowed) => borrowed,
84 ArcCow::Owned(owned) => owned.as_ref(),
85 }
86 }
87}
88
89impl<'a, T: ?Sized + Debug> Debug for ArcCow<'a, T> {
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 match self {
92 ArcCow::Borrowed(borrowed) => Debug::fmt(borrowed, f),
93 ArcCow::Owned(owned) => Debug::fmt(&**owned, f),
94 }
95 }
96}