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