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
prelude! {}
type Memory = crate::mem::Memory<[u8]>;
#[derive(Debug, Clone, Copy, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
pub struct Str {
uid: usize,
}
impl Str {
pub fn factory_mut<'a>() -> AsWrite<'a> {
write()
}
pub fn factory<'a>() -> AsRead<'a> {
read()
}
pub fn uid(&self) -> usize {
self.uid
}
pub fn get(self) -> Arc<[u8]> {
Self::factory().get_elm(self)
}
pub fn str_do<Res>(self, action: impl FnOnce(&str) -> Res) -> Res {
let elm = self.get();
let str = std::str::from_utf8(elm.as_ref()).unwrap_or_else(|e| {
panic!(
"shared string stored as bytes is not a legal string: {:?}\n{}",
elm, e
)
});
action(str)
}
pub fn new(str: &str) -> Str {
Self::factory_mut().get_uid(str)
}
}
impl std::fmt::Display for Str {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
self.str_do(|s| s.fmt(fmt))
}
}
impl PartialEq<String> for Str {
fn eq(&self, other: &String) -> bool {
&*self.get() == other.as_bytes()
}
}
impl PartialEq<Str> for String {
fn eq(&self, other: &Str) -> bool {
&*other.get() == self.as_bytes()
}
}
impl PartialEq<str> for Str {
fn eq(&self, other: &str) -> bool {
&*self.get() == other.as_bytes()
}
}
impl PartialEq<Str> for str {
fn eq(&self, other: &Str) -> bool {
&*other.get() == self.as_bytes()
}
}
impl PartialEq<&str> for Str {
fn eq(&self, other: &&str) -> bool {
&*self.get() == other.as_bytes()
}
}
impl PartialEq<Str> for &str {
fn eq(&self, other: &Str) -> bool {
&*other.get() == self.as_bytes()
}
}
impl PartialEq<Str> for Str {
fn eq(&self, other: &Str) -> bool {
self.uid == other.uid
}
}
impl<'a> PartialEq<Str> for &'a Str {
fn eq(&self, other: &Str) -> bool {
self.uid == other.uid
}
}
impl<'a> PartialEq<&'a Str> for Str {
fn eq(&self, other: &&'a Str) -> bool {
self.uid == other.uid
}
}
pub struct AsRead<'a> {
mem: sync::RwLockReadGuard<'a, Memory>,
}
impl<'a> AsRead<'a> {
pub fn get_elm(&self, uid: Str) -> Arc<[u8]> {
self.mem.get_elm(uid.uid)
}
}
pub struct AsWrite<'a> {
mem: sync::RwLockWriteGuard<'a, Memory>,
}
impl<'a> AsWrite<'a> {
pub fn get_uid(&mut self, s: &str) -> Str {
Str {
uid: self.mem.get_uid(s),
}
}
}
crate::prelude::lazy_static! {
static ref MEM: sync::RwLock<Memory> = sync::RwLock::new(Memory::new());
}
fn read<'a>() -> AsRead<'a> {
AsRead {
mem: MEM
.read()
.expect("fatal error: a data memory has been poisoned"),
}
}
fn write<'a>() -> AsWrite<'a> {
AsWrite {
mem: MEM
.write()
.expect("fatal error: a data memory has been poisoned"),
}
}