|
| 1 | +use std::borrow::Borrow; |
| 2 | + |
| 3 | + |
| 4 | +/// A trait for keys in a QP-trie. |
| 5 | +/// |
| 6 | +/// Implementing types must be borrowable in the form of both a key slice, |
| 7 | +/// such as `&str`, and the plain byte slice `&[u8]`. The former is used in |
| 8 | +/// the public `trie::Trie` API, while the latter is used internally to match |
| 9 | +/// and store keys. |
| 10 | +/// |
| 11 | +/// Note that, as a consequence, keys which are not bytewise-equivalent will |
| 12 | +/// not associate to the same entry, even if they are equal under `Eq`. |
| 13 | +pub trait AsKey { |
| 14 | + /// The borrowed form of this key type. |
| 15 | + type Borrowed: ?Sized; |
| 16 | + |
| 17 | + /// Borrow this key as a slice. |
| 18 | + fn as_key_slice(&self) -> &Self::Borrowed; |
| 19 | + |
| 20 | + /// View the key slice as a plain byte sequence. |
| 21 | + fn as_key_bytes(key: &Self::Borrowed) -> &[u8]; |
| 22 | + |
| 23 | + /// Borrow the key as nybbles, in the form of a plain byte sequence. |
| 24 | + #[inline] |
| 25 | + fn as_nybbles(&self) -> &[u8] { |
| 26 | + Self::as_key_bytes(self.as_key_slice()) |
| 27 | + } |
| 28 | + |
| 29 | + /// View the nybbles of a value that can borrowed as a key slice. |
| 30 | + #[inline] |
| 31 | + fn nybbles_from<'a, Q: 'a + ?Sized>(val: &'a Q) -> &'a [u8] |
| 32 | + where |
| 33 | + Self::Borrowed: 'a, |
| 34 | + Q: Borrow<Self::Borrowed>, |
| 35 | + { |
| 36 | + Self::as_key_bytes(val.borrow()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +impl<'a> AsKey for &'a [u8] { |
| 42 | + type Borrowed = [u8]; |
| 43 | + |
| 44 | + #[inline] |
| 45 | + fn as_key_slice(&self) -> &Self::Borrowed { |
| 46 | + self |
| 47 | + } |
| 48 | + |
| 49 | + #[inline] |
| 50 | + fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { |
| 51 | + key |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +impl AsKey for Vec<u8> { |
| 57 | + type Borrowed = [u8]; |
| 58 | + |
| 59 | + #[inline] |
| 60 | + fn as_key_slice(&self) -> &Self::Borrowed { |
| 61 | + self.as_slice() |
| 62 | + } |
| 63 | + |
| 64 | + #[inline] |
| 65 | + fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { |
| 66 | + key |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +impl<'a> AsKey for &'a str { |
| 72 | + type Borrowed = str; |
| 73 | + |
| 74 | + #[inline] |
| 75 | + fn as_key_slice(&self) -> &Self::Borrowed { |
| 76 | + self |
| 77 | + } |
| 78 | + |
| 79 | + #[inline] |
| 80 | + fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { |
| 81 | + key.as_bytes() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +impl AsKey for String { |
| 87 | + type Borrowed = str; |
| 88 | + |
| 89 | + #[inline] |
| 90 | + fn as_key_slice(&self) -> &Self::Borrowed { |
| 91 | + self.as_str() |
| 92 | + } |
| 93 | + |
| 94 | + #[inline] |
| 95 | + fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { |
| 96 | + key.as_bytes() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +pub trait Break: AsKey { |
| 102 | + fn empty<'a>() -> &'a <Self as AsKey>::Borrowed; |
| 103 | + fn find_break(&self, loc: usize) -> &<Self as AsKey>::Borrowed; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +impl<'b> Break for &'b [u8] { |
| 108 | + #[inline] |
| 109 | + fn empty<'a>() -> &'a [u8] { |
| 110 | + <&'a [u8]>::default() |
| 111 | + } |
| 112 | + |
| 113 | + #[inline] |
| 114 | + fn find_break(&self, loc: usize) -> &[u8] { |
| 115 | + &self[..loc] |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +impl Break for Vec<u8> { |
| 121 | + #[inline] |
| 122 | + fn empty<'a>() -> &'a [u8] { |
| 123 | + <&'a [u8]>::default() |
| 124 | + } |
| 125 | + |
| 126 | + #[inline] |
| 127 | + fn find_break(&self, loc: usize) -> &[u8] { |
| 128 | + &self[..loc] |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +impl<'b> Break for &'b str { |
| 134 | + #[inline] |
| 135 | + fn empty<'a>() -> &'a str { |
| 136 | + <&'a str>::default() |
| 137 | + } |
| 138 | + |
| 139 | + #[inline] |
| 140 | + fn find_break(&self, mut loc: usize) -> &str { |
| 141 | + while !self.is_char_boundary(loc) { |
| 142 | + loc -= 1; |
| 143 | + } |
| 144 | + |
| 145 | + &self[..loc] |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +impl Break for String { |
| 151 | + #[inline] |
| 152 | + fn empty<'a>() -> &'a str { |
| 153 | + <&'a str>::default() |
| 154 | + } |
| 155 | + |
| 156 | + #[inline] |
| 157 | + fn find_break(&self, mut loc: usize) -> &str { |
| 158 | + while !self.is_char_boundary(loc) { |
| 159 | + loc -= 1; |
| 160 | + } |
| 161 | + |
| 162 | + &self[..loc] |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +macro_rules! impl_for_arrays_of_size { |
| 168 | + ($($length:expr)+) => { $( |
| 169 | + impl AsKey for [u8; $length] { |
| 170 | + type Borrowed = [u8]; |
| 171 | + |
| 172 | + #[inline] |
| 173 | + fn as_key_slice(&self) -> &Self::Borrowed { |
| 174 | + self.as_ref() |
| 175 | + } |
| 176 | + |
| 177 | + #[inline] |
| 178 | + fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { |
| 179 | + key |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + impl Break for [u8; $length] { |
| 184 | + #[inline] |
| 185 | + fn empty<'a>() -> &'a [u8] { |
| 186 | + <&'a [u8]>::default() |
| 187 | + } |
| 188 | + |
| 189 | + #[inline] |
| 190 | + fn find_break(&self, loc: usize) -> &[u8] { |
| 191 | + &self.as_key_slice()[..loc] |
| 192 | + } |
| 193 | + } |
| 194 | + )+ } |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | +impl_for_arrays_of_size! { |
| 199 | + 0 1 2 3 4 5 6 7 8 9 |
| 200 | + 10 11 12 13 14 15 16 17 18 19 |
| 201 | + 20 21 22 23 24 25 26 27 28 29 |
| 202 | + 30 31 32 |
| 203 | +} |
0 commit comments