1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#![forbid(unsafe_code)]
#![deny(
clippy::dbg_macro,
missing_copy_implementations,
rustdoc::missing_crate_level_docs,
missing_debug_implementations,
nonstandard_style,
unused_qualifications
)]
#![warn(missing_docs)]
use smartstring::{LazyCompact, SmartString};
use std::{
borrow::{Borrow, Cow},
fmt::{Debug, Display, Formatter, Result},
hash::{Hash, Hasher},
ops::Deref,
};
#[derive(Clone)]
pub enum SmartCow<'a> {
Borrowed(&'a str),
Owned(SmartString<LazyCompact>),
}
impl SmartCow<'_> {
pub fn to_owned(&self) -> SmartCow<'static> {
self.clone().into_owned()
}
pub fn into_owned(self) -> SmartCow<'static> {
match self {
SmartCow::Borrowed(b) => SmartCow::Owned(SmartString::from(b)),
SmartCow::Owned(o) => SmartCow::Owned(o),
}
}
pub fn borrow(&self) -> SmartCow<'_> {
match self {
SmartCow::Borrowed(b) => SmartCow::Borrowed(b),
SmartCow::Owned(o) => SmartCow::Borrowed(o),
}
}
}
impl Default for SmartCow<'_> {
fn default() -> Self {
Self::Borrowed("")
}
}
impl PartialEq<SmartCow<'_>> for SmartCow<'_> {
fn eq(&self, other: &SmartCow<'_>) -> bool {
**self == **other
}
}
impl Eq for SmartCow<'_> {}
impl PartialEq<&str> for SmartCow<'_> {
fn eq(&self, other: &&str) -> bool {
&**self == *other
}
}
impl PartialEq<String> for SmartCow<'_> {
fn eq(&self, other: &String) -> bool {
**self == **other
}
}
impl PartialEq<&String> for SmartCow<'_> {
fn eq(&self, other: &&String) -> bool {
**self == **other
}
}
impl Debug for SmartCow<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Debug::fmt(&**self, f)
}
}
impl Display for SmartCow<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(self)
}
}
impl AsRef<str> for SmartCow<'_> {
fn as_ref(&self) -> &str {
self
}
}
impl AsRef<[u8]> for SmartCow<'_> {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl<'a> From<&'a str> for SmartCow<'a> {
fn from(s: &'a str) -> Self {
Self::Borrowed(s)
}
}
impl<'a> From<Cow<'a, str>> for SmartCow<'a> {
fn from(s: Cow<'a, str>) -> Self {
match s {
Cow::Owned(string) => Self::Owned(SmartString::from(string)),
Cow::Borrowed(s) => Self::Borrowed(s),
}
}
}
impl From<String> for SmartCow<'_> {
fn from(s: String) -> Self {
Self::Owned(SmartString::from(s))
}
}
impl From<SmartString<LazyCompact>> for SmartCow<'_> {
fn from(s: SmartString<LazyCompact>) -> Self {
Self::Owned(s)
}
}
impl From<SmartCow<'_>> for SmartString<LazyCompact> {
fn from(value: SmartCow<'_>) -> Self {
match value {
SmartCow::Borrowed(b) => b.into(),
SmartCow::Owned(o) => o,
}
}
}
impl Deref for SmartCow<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
match self {
Self::Borrowed(b) => b,
Self::Owned(o) => o,
}
}
}
impl Borrow<str> for SmartCow<'_> {
fn borrow(&self) -> &str {
self
}
}
impl Hash for SmartCow<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.deref().hash(state)
}
}