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
use super::compact::Compact;
use super::compact_vec::CompactVec;
use std::marker::PhantomData;
#[derive(Clone, Default)]
pub struct CompactString {
chars: CompactVec<u8>,
}
impl CompactString {
pub fn new() -> Self {
Default::default()
}
pub fn push_str(&mut self, string: &str) {
self.chars.extend_from_copy_slice(string.as_bytes());
}
}
impl ::std::ops::Deref for CompactString {
type Target = str;
fn deref(&self) -> &str {
unsafe { ::std::str::from_utf8_unchecked(&self.chars) }
}
}
impl ::std::convert::From<String> for CompactString {
fn from(string: String) -> CompactString {
CompactString {
chars: string.into_bytes().into(),
}
}
}
impl Compact for CompactString {
fn is_still_compact(&self) -> bool {
self.chars.is_still_compact()
}
fn dynamic_size_bytes(&self) -> usize {
self.chars.dynamic_size_bytes()
}
unsafe fn compact(source: *mut Self, dest: *mut Self, new_dynamic_part: *mut u8) {
Compact::compact(&mut (*source).chars, &mut (*dest).chars, new_dynamic_part)
}
unsafe fn decompact(source: *const Self) -> Self {
CompactString {
chars: Compact::decompact(&(*source).chars),
}
}
}
#[cfg(feature = "serde-serialization")]
impl ::serde::ser::Serialize for CompactString{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::ser::Serializer,
{
serializer.serialize_str(&self)
}
}
#[cfg(feature = "serde")]
struct CompactStringVisitor {
marker: PhantomData<fn() -> CompactString>
}
#[cfg(feature = "serde")]
impl CompactStringVisitor {
fn new() -> Self {
CompactStringVisitor {
marker: PhantomData
}
}
}
#[cfg(feature = "serde")]
impl<'de> ::serde::de::Visitor<'de> for CompactStringVisitor
{
type Value = CompactString;
fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
formatter.write_str("A string")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
Ok(s.to_owned().into())
}
fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
where
E: ::serde::de::Error,
{
Ok(s.into())
}
}
#[cfg(feature = "serde")]
impl<'de> ::serde::de::Deserialize<'de> for CompactString
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::de::Deserializer<'de>,
{
deserializer.deserialize_string(CompactStringVisitor::new())
}
}