Publish a decoder, to make testing a bit easier.

This commit is contained in:
2018-06-02 20:28:53 -07:00
parent 041f824caf
commit a5f0179d77

View File

@@ -6,29 +6,33 @@ pub trait Decoder {
fn from_bytes(x: &[u8]) -> Self;
}
macro_rules! generate_decoder {
($name: ident) => {
impl Decoder for $name {
fn from_bytes(x: &[u8]) -> $name {
let mut res = $name::new();
pub(crate) fn raw_decoder(input: &[u8], output: &mut [u64])
{
let mut item = 0;
let mut shift = 0;
let mut idx = 0;
for v in x.iter().rev() {
for v in input.iter().rev() {
item |= (*v as u64) << shift;
shift += 8;
if shift == 64 {
shift = 0;
res.values[idx] = item;
output[idx] = item;
idx += 1;
item = 0;
}
}
if item != 0 {
res.values[idx] = item;
output[idx] = item;
}
}
macro_rules! generate_decoder {
($name: ident) => {
impl Decoder for $name {
fn from_bytes(x: &[u8]) -> $name {
let mut res = $name::new();
raw_decoder(x, &mut res.values);
res
}
}