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
use std::{convert::TryInto, mem::size_of};
use impl_trait_for_tuples::impl_for_tuples;
/// Size of the KeyFormat prefix.
const KEY_FORMAT_PREFIX_SIZE: usize = size_of::<u8>();
/// A key formatting helper trait to be used together with key-value
/// backends for constructing keys.
pub trait KeyFormat {
/// The prefix that identifies the key format.
fn prefix() -> u8;
/// The minimum size of the encoded key.
fn size() -> usize;
/// Encode the given key format into a set of atoms.
fn encode_atoms(self, atoms: &mut Vec<Vec<u8>>);
/// Decode the given key format from data (without prefix).
///
/// The caller must ensure that the size of the passed data is at
/// least the minimum size returned by `size`.
fn decode_atoms(data: &[u8]) -> Self
where
Self: Sized;
/// Encode the first few atoms in the key format.
///
/// This method can be used to construct key prefixes for iteration.
/// Specifying a zero count will only generate the prefix.
fn encode_partial(self, count: usize) -> Vec<u8>
where
Self: Sized,
{
let mut v = Vec::with_capacity(KEY_FORMAT_PREFIX_SIZE + Self::size());
v.push(Self::prefix());
if count == 0 {
return v;
}
let mut atoms = Vec::new();
self.encode_atoms(&mut atoms);
for (included, mut atom) in atoms.into_iter().enumerate() {
if included >= count {
break;
}
v.append(&mut atom);
}
v
}
/// Encode the given key format.
fn encode(self) -> Vec<u8>
where
Self: Sized,
{
self.encode_partial(usize::MAX)
}
/// Decode the given key format from data.
///
/// The method may return `None` in case the key is of a different
/// type as indicated by the prefix byte.
fn decode(data: &[u8]) -> Option<Self>
where
Self: Sized,
{
assert!(!data.is_empty(), "key format: malformed input (empty data)");
if data[0] != Self::prefix() {
return None;
}
assert!(
data.len() >= Self::size() + KEY_FORMAT_PREFIX_SIZE,
"key format: malformed input"
);
Some(Self::decode_atoms(&data[1..]))
}
}
/// Part of the KeyFormat to be used with key-value backends for constructing keys.
pub trait KeyFormatAtom {
fn size() -> usize;
fn encode_atom(self) -> Vec<u8>;
fn decode_atom(data: &[u8]) -> Self
where
Self: Sized;
}
impl KeyFormatAtom for u64 {
fn size() -> usize {
8
}
fn encode_atom(self) -> Vec<u8> {
self.to_be_bytes().to_vec()
}
fn decode_atom(data: &[u8]) -> Self
where
Self: Sized,
{
u64::from_be_bytes(data.try_into().expect("key_format: malformed u64 input"))
}
}
impl KeyFormatAtom for u8 {
fn size() -> usize {
1
}
fn encode_atom(self) -> Vec<u8> {
vec![self]
}
fn decode_atom(data: &[u8]) -> Self
where
Self: Sized,
{
assert!(!data.is_empty(), "key_format: malformed: u8 input");
data[0]
}
}
impl KeyFormatAtom for () {
fn size() -> usize {
0
}
fn encode_atom(self) -> Vec<u8> {
Vec::new()
}
fn decode_atom(_: &[u8]) {}
}
#[impl_for_tuples(2, 10)]
impl KeyFormatAtom for Tuple {
fn size() -> usize {
for_tuples!( #( Tuple::size() )+* );
}
fn encode_atom(self) -> Vec<u8> {
let mut atoms: Vec<Vec<u8>> = [for_tuples!( #( self.Tuple.encode_atom() ),* )].to_vec();
atoms.into_iter().flatten().collect()
}
fn decode_atom(data: &[u8]) -> for_tuples!( ( #( Tuple ),* ) ) {
assert!(
data.len() >= Self::size(),
"key format atom: malformed input"
);
let mut sizes: Vec<usize> = [for_tuples!( #( Tuple::size() ),* )].to_vec();
sizes.reverse();
let mut data = data.to_vec();
/*
(
{
let x = T1::decode_atom(data.drain(0..T1::size()));
x
},
{
let x = T2::decode_atom(data.drain(0..T2::size()));
x
}
...
)
*/
for_tuples!(
(
#(
{
let x = Tuple::decode_atom(data.drain(0..sizes.pop().unwrap()).as_slice());
x
}
),*
)
)
}
}
/// Define a KeyFormat from KeyFormatAtom and a prefix.
///
/// # Examples
///
/// ```rust,ignore
/// key_format!(NewKeyFormatName, 0x01, InnerType);
/// ```
#[macro_export]
macro_rules! key_format {
($name:ident, $prefix:expr, $inner:ty) => {
#[derive(Debug, Default, PartialEq, Eq, Clone)]
struct $name($inner);
impl KeyFormat for $name {
fn prefix() -> u8 {
$prefix
}
fn size() -> usize {
<$inner>::size()
}
fn encode_atoms(self, atoms: &mut Vec<Vec<u8>>) {
atoms.push(self.0.encode_atom());
}
fn decode_atoms(data: &[u8]) -> Self {
Self(<$inner>::decode_atom(data))
}
}
};
}
#[cfg(test)]
mod test {
use rustc_hex::ToHex;
use crate::common::crypto::hash::Hash;
use super::*;
#[derive(Debug, PartialEq)]
struct Test1KeyFormat {
h: Hash,
}
impl KeyFormat for Test1KeyFormat {
fn prefix() -> u8 {
b'T'
}
fn size() -> usize {
32
}
fn encode_atoms(self, atoms: &mut Vec<Vec<u8>>) {
atoms.push(self.h.as_ref().to_vec());
}
fn decode_atoms(data: &[u8]) -> Self {
Self { h: data.into() }
}
}
#[test]
fn test_key_format() {
let mut enc = Test1KeyFormat {
h: Hash::empty_hash(),
}
.encode();
assert_eq!(
enc.to_hex::<String>(),
"54c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a"
);
let dec = Test1KeyFormat::decode(&enc);
assert_eq!(
dec,
Some(Test1KeyFormat {
h: Hash::empty_hash()
})
);
// Clear type.
enc[0] = 0x00;
let dec = Test1KeyFormat::decode(&enc);
assert_eq!(dec, None);
// Partial encoding.
let enc = Test1KeyFormat {
h: Hash::empty_hash(),
}
.encode_partial(0);
assert_eq!(enc.to_hex::<String>(), "54");
}
#[test]
fn test_key_format_atom() {
key_format!(TestKeyFormat, 0x01, (u8, u64, u8, u64, u64));
let key = TestKeyFormat((1, 2, 3, 4, 5));
let enc = key.clone().encode();
let dec = TestKeyFormat::decode(&enc);
assert_eq!(dec, Some(key),)
}
}