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
#![cfg(feature = "backends")]
use super::Backend;
use crate::{
compat::{
Box,
ToString,
Vec,
},
symbol::expect_valid_symbol,
Symbol,
};
use core::{
iter::Enumerate,
marker::PhantomData,
slice,
};
#[derive(Debug)]
pub struct SimpleBackend<S> {
strings: Vec<Box<str>>,
symbol_marker: PhantomData<fn() -> S>,
}
impl<S> Default for SimpleBackend<S> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Self {
strings: Vec::new(),
symbol_marker: Default::default(),
}
}
}
impl<S> Backend<S> for SimpleBackend<S>
where
S: Symbol,
{
#[cfg_attr(feature = "inline-more", inline)]
fn with_capacity(cap: usize) -> Self {
Self {
strings: Vec::with_capacity(cap),
symbol_marker: Default::default(),
}
}
#[inline]
fn intern(&mut self, string: &str) -> S {
let symbol = expect_valid_symbol(self.strings.len());
let str = string.to_string().into_boxed_str();
self.strings.push(str);
symbol
}
fn shrink_to_fit(&mut self) {
self.strings.shrink_to_fit()
}
#[inline]
fn resolve(&self, symbol: S) -> Option<&str> {
self.strings.get(symbol.to_usize()).map(|pinned| &**pinned)
}
#[inline]
unsafe fn resolve_unchecked(&self, symbol: S) -> &str {
self.strings.get_unchecked(symbol.to_usize())
}
}
impl<S> Clone for SimpleBackend<S> {
#[cfg_attr(feature = "inline-more", inline)]
fn clone(&self) -> Self {
Self {
strings: self.strings.clone(),
symbol_marker: Default::default(),
}
}
}
impl<S> Eq for SimpleBackend<S> where S: Symbol {}
impl<S> PartialEq for SimpleBackend<S>
where
S: Symbol,
{
#[cfg_attr(feature = "inline-more", inline)]
fn eq(&self, other: &Self) -> bool {
self.strings == other.strings
}
}
impl<'a, S> IntoIterator for &'a SimpleBackend<S>
where
S: Symbol,
{
type Item = (S, &'a str);
type IntoIter = Iter<'a, S>;
#[cfg_attr(feature = "inline-more", inline)]
fn into_iter(self) -> Self::IntoIter {
Self::IntoIter::new(self)
}
}
pub struct Iter<'a, S> {
iter: Enumerate<slice::Iter<'a, Box<str>>>,
symbol_marker: PhantomData<fn() -> S>,
}
impl<'a, S> Iter<'a, S> {
#[cfg_attr(feature = "inline-more", inline)]
pub fn new(backend: &'a SimpleBackend<S>) -> Self {
Self {
iter: backend.strings.iter().enumerate(),
symbol_marker: Default::default(),
}
}
}
impl<'a, S> Iterator for Iter<'a, S>
where
S: Symbol,
{
type Item = (S, &'a str);
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|(id, pinned)| (expect_valid_symbol(id), &**pinned))
}
}