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
use std;
use std::hash::Hash;
use std::ops::Deref;
use std::rc::Rc;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
#[derive(Clone, Debug, Eq, Ord)]
pub enum SharedString {
Dynamic(Rc<String>),
Static(&'static str),
}
impl Deref for SharedString {
type Target = str;
fn deref(&self) -> &str {
match *self {
SharedString::Static(ref s) => *s,
SharedString::Dynamic(ref rc) => rc.deref(),
}
}
}
impl PartialEq for SharedString {
fn eq(&self, other: &SharedString) -> bool {
self.deref() == other.deref()
}
}
impl PartialEq<str> for SharedString {
fn eq(&self, other: &str) -> bool {
self.deref().eq(other)
}
}
impl<'a> PartialEq<&'a str> for SharedString {
fn eq(&self, other: &&'a str) -> bool {
self.deref().eq(*other)
}
}
impl PartialOrd for SharedString {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.deref().partial_cmp(other.deref())
}
}
impl Hash for SharedString {
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
self.deref().hash(hasher)
}
}
impl std::fmt::Display for SharedString {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
self.deref().fmt(formatter)
}
}
impl Default for SharedString {
fn default() -> Self {
SharedString::Static("<uninitialized SharedString>")
}
}
impl Serialize for SharedString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.deref().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for SharedString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let dynamic = String::deserialize(deserializer)?;
Ok(SharedString::Dynamic(Rc::new(dynamic)))
}
}
impl SharedString {
pub fn as_str(&self) -> &str {
self.deref()
}
pub const fn from_str(value: &'static str) -> Self {
SharedString::Static(value)
}
pub fn from_rc_string(value: Rc<String>) -> Self {
SharedString::Dynamic(value)
}
pub fn from_string(value: String) -> Self {
SharedString::Dynamic(Rc::new(value))
}
}
#[macro_export]
macro_rules! shared_string {
(
$(#[$outer:meta])*
pub $name: ident
) => {
$(#[$outer])*
#[derive(Clone, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
pub struct $name(pub shared_string::SharedString);
impl $name {
pub const fn from_str(value: &'static str) -> Self {
$name(shared_string::SharedString::from_str(value))
}
pub fn from_string(value: String) -> Self {
$name(shared_string::SharedString::from_string(value))
}
pub fn from_rc_string(value: std::rc::Rc<String>) -> Self {
$name(shared_string::SharedString::from_rc_string(value))
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
pub fn as_shared_string(&self) -> &shared_string::SharedString {
&self.0
}
}
impl<'a> Into<&'a [u8]> for &'a $name {
fn into(self) -> &'a [u8] {
self.as_str().as_bytes()
}
}
impl PartialEq for $name {
fn eq(&self, other: &$name) -> bool {
self.0.eq(&other.0)
}
}
impl<'a> PartialEq<&'a str> for $name {
fn eq(&self, other: &&'a str) -> bool {
self.0.eq(other)
}
}
impl Default for $name {
fn default() -> Self {
Self::from_str("<uninitialized SharedString>")
}
}
impl std::fmt::Display for $name {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
self.as_str().fmt(formatter)
}
}
};
}