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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
use std::alloc::Layout;
use std::cmp::Ordering;
use std::fmt::{Debug, Formatter};
#[derive(Clone, Copy)]
#[repr(C)]
struct GSSmall {
len: u32,
data: [u8; 12],
}
#[derive(Clone, Copy)]
#[repr(transparent)]
struct StorageClassPtr(usize);
impl StorageClassPtr {
fn transient(ptr: *mut u8) -> Self {
debug_assert!(
(ptr as usize & 0b11) == 0,
"pointer must be at least 4-byte aligned"
);
Self(ptr as usize)
}
fn as_ptr(&self) -> *mut u8 {
(self.0 & !0b11) as *mut u8
}
}
#[derive(Clone, Copy)]
#[repr(C)]
struct GSLarge {
len: u32,
prefix: [u8; 4],
data: StorageClassPtr,
}
const _ASSERT_VARIANTS_SIZE: () = assert!(
std::mem::size_of::<GSSmall>() == std::mem::size_of::<GSLarge>(),
"German String variants must have the same size"
);
union GSRepr {
small: GSSmall,
large: GSLarge,
}
#[repr(transparent)]
pub struct GermanString(GSRepr);
const _ASSERT_GSTRING_SIZE: () = assert!(
std::mem::size_of::<GermanString>() == 16,
"German String should be 16 bytes in size",
);
impl GermanString {
/// Creates a new transient German String from the given slice, copying the
/// data in the process.
pub fn transient(bytes: &[u8]) -> GermanString {
if bytes.len() > u32::MAX as usize {
panic!("GermanString maximum length is {} bytes", u32::MAX);
}
if bytes.len() <= 12 {
let mut s = GSSmall {
len: bytes.len() as u32,
data: [0u8; 12],
};
s.data[..bytes.len()].copy_from_slice(bytes);
GermanString(GSRepr { small: s })
} else {
let layout = Layout::array::<u8>(bytes.len()).unwrap();
let mut large = GSLarge {
len: bytes.len() as u32,
prefix: [0u8; 4],
data: unsafe {
let ptr = std::alloc::alloc(layout);
if ptr.is_null() {
std::alloc::handle_alloc_error(layout);
}
std::ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, bytes.len());
StorageClassPtr::transient(ptr)
},
};
large.prefix.copy_from_slice(&bytes[..4]);
GermanString(GSRepr { large })
}
}
/// Creates a new transient German String from the given owned bytes. Short
/// strings will be copied into the string representation, long strings will
/// be moved out of the given vector without additional allocations.
pub fn transient_from_owned(bytes: Vec<u8>) -> GermanString {
if bytes.len() > u32::MAX as usize {
panic!("GermanString maximum length is {} bytes", u32::MAX);
}
if bytes.len() <= 12 {
let mut s = GSSmall {
len: bytes.len() as u32,
data: [0u8; 12],
};
s.data[..bytes.len()].copy_from_slice(&bytes);
GermanString(GSRepr { small: s })
} else {
let mut md = std::mem::ManuallyDrop::new(bytes);
let mut large = GSLarge {
len: md.len() as u32,
prefix: [0u8; 4],
data: StorageClassPtr::transient(md.as_mut_ptr()),
};
large.prefix.copy_from_slice(&md[..4]);
GermanString(GSRepr { large })
}
}
pub fn len(&self) -> usize {
// SAFETY: The length field is located in the same location for both
// variants, reading it from either is safe.
unsafe { self.0.small.len as usize }
}
pub fn as_bytes(&self) -> &[u8] {
if self.len() > 12 {
unsafe { std::slice::from_raw_parts(self.0.large.data.as_ptr(), self.len()) }
} else {
unsafe { &self.0.small.data.as_ref()[..self.len()] }
}
}
pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> {
std::str::from_utf8(self.as_bytes())
}
}
impl Drop for GermanString {
fn drop(&mut self) {
if self.len() > 12 {
let layout = Layout::array::<u8>(self.len()).unwrap();
unsafe {
std::alloc::dealloc(self.0.large.data.as_ptr(), layout);
}
}
}
}
impl PartialEq for GermanString {
fn eq(&self, other: &GermanString) -> bool {
if self.len() != other.len() {
return false;
}
unsafe {
if self.len() <= 12 {
return self.0.small.data[..self.len()] == other.0.small.data[..other.len()];
}
return self.0.large.data.as_ptr() == other.0.large.data.as_ptr()
|| (self.0.large.prefix == other.0.large.prefix
&& self.as_bytes() == other.as_bytes());
}
}
}
impl Eq for GermanString {}
impl Ord for GermanString {
fn cmp(&self, other: &GermanString) -> Ordering {
match (self.len().cmp(&12), other.len().cmp(&12)) {
// two small strings
(Ordering::Less | Ordering::Equal, Ordering::Less | Ordering::Equal) => unsafe {
self.0.small.data[..self.len()].cmp(&other.0.small.data[..other.len()])
},
// two large strings
(Ordering::Greater, Ordering::Greater) => unsafe {
match self.0.large.prefix.cmp(&other.0.large.prefix) {
Ordering::Equal => self.as_bytes().cmp(other.as_bytes()),
ordering => ordering,
}
},
// LHS large, RHS small
(Ordering::Greater, _) => {
let prefix_ordering =
unsafe { self.0.large.prefix.as_slice().cmp(&other.0.small.data[..4]) };
if prefix_ordering != Ordering::Equal {
return prefix_ordering;
}
self.as_bytes().cmp(other.as_bytes())
}
// LHS small, RHS large
(_, Ordering::Greater) => {
let prefix_ordering =
unsafe { self.0.small.data[..4].cmp(other.0.large.prefix.as_slice()) };
if prefix_ordering != Ordering::Equal {
return prefix_ordering;
}
self.as_bytes().cmp(other.as_bytes())
}
}
}
}
impl PartialOrd for GermanString {
fn partial_cmp(&self, other: &GermanString) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Debug for GermanString {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
String::from_utf8_lossy(self.as_bytes()).fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
impl Arbitrary for GermanString {
type Parameters = <String as Arbitrary>::Parameters;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
any_with::<String>(args)
.prop_map(|s| GermanString::transient(s.as_bytes()))
.boxed()
}
}
#[test]
fn test_empty_string() {
let empty = GermanString::transient(b"");
assert_eq!(empty.len(), 0, "empty string should be empty");
assert_eq!(empty.as_bytes(), b"", "empty string should contain nothing");
assert_eq!(
empty.as_str().expect("empty string is valid UTF-8"),
"",
"empty string should contain empty string"
);
}
#[test]
fn test_short_string() {
let short = GermanString::transient(b"meow");
assert_eq!(short.len(), 4, "'meow' is four characters");
assert_eq!(
short.as_bytes(),
b"meow",
"short string returns correct bytes"
);
assert_eq!(
short.as_str().expect("'meow' is valid UTF-8"),
"meow",
"short string returns correct string"
);
}
#[test]
fn test_long_string() {
let input: &str = "This code was written at https://signal.live";
let long = GermanString::transient(input.as_bytes());
assert_eq!(long.len(), 44, "long string has correct length");
assert_eq!(
long.as_bytes(),
input.as_bytes(),
"long string returns correct bytes"
);
assert_eq!(
long.as_str().expect("input is valid UTF-8"),
input,
"long string returns correct string"
);
}
proptest! {
#[test]
fn test_roundtrip_vec(input: Vec<u8>) {
let gs = GermanString::transient_from_owned(input.clone());
assert_eq!(input.len(), gs.len(), "length should match");
let out = gs.as_bytes().to_owned();
assert_eq!(input, out, "roundtrip should yield same bytes");
}
#[test]
fn test_roundtrip_string(input: String) {
let gs = GermanString::transient_from_owned(input.clone().into_bytes());
assert_eq!(input.len(), gs.len(), "length should match");
let out = String::from_utf8(gs.as_bytes().to_owned())
.expect("string should be valid after roundtrip");
assert_eq!(input, out, "roundtrip should yield same string");
}
// Test [`Eq`] implementation.
#[test]
fn test_eq(lhs: Vec<u8>, rhs: Vec<u8>) {
let lhs_gs = GermanString::transient(lhs.as_slice());
let rhs_gs = GermanString::transient(rhs.as_slice());
assert_eq!(
(lhs == rhs),
(lhs_gs == rhs_gs),
"Eq should match between std::String and GermanString ({:?} == {:?})",
lhs, rhs,
);
}
#[test]
fn test_reflexivity(x: GermanString) {
prop_assert!(x == x);
}
#[test]
fn test_symmetry(x: GermanString, y: GermanString) {
prop_assert_eq!(x == y, y == x);
}
#[test]
fn test_transitivity(x: GermanString, y: GermanString, z: GermanString) {
if x == y && y == z {
assert!(x == z);
}
}
}
}
|