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
use crate::prelude::*;
use crate::encodings::varint::*;
use std::convert::{TryFrom, TryInto};

// FIXME: This is just for convenience right now, schema matching and custom encodings are needed instead.
impl<T: IntFromU64> BatchData for T {
    fn read_batch(bytes: &[u8]) -> Vec<Self> {
        read_all(bytes, |b, o| {
            let v = decode_prefix_varint(b, o);
            v.try_into().unwrap_or_else(|_| todo!()) // TODO: Error handling (which won't be needed when schema match occurs)
        })
    }
    fn write_batch(items: &[Self], bytes: &mut Vec<u8>) {
        for item in items {
            let v = (*item).into();
            encode_prefix_varint(v, bytes);
        }
    }
    fn read_one(bytes: &[u8], offset: &mut usize) -> Self {
        let v = decode_prefix_varint(bytes, offset);
        v.try_into().unwrap_or_else(|_| todo!()) // TODO: Error handling (which won't be needed when schema match occurs)s
    }
}

pub trait IntFromU64 : Into<u64> + TryFrom<u64> + Copy + Default {}
impl IntFromU64 for u8 {}
impl IntFromU64 for u16 {}
impl IntFromU64 for u32 {}
impl IntFromU64 for u64 {}


impl<T: IntFromU64> Primitive for T {
    fn id() -> PrimitiveId { PrimitiveId::Integer }
    fn from_dyn_branch(branch: DynBranch) -> OneOrMany<Self> {
        match branch {
            DynBranch::Integer(v) => {
                match v {
                    OneOrMany::One(v) => OneOrMany::One(v.try_into().unwrap_or_else(|_| todo!("schema mismatch"))),
                    OneOrMany::Many(b) => OneOrMany::Many(b),
                }
            },
            _ => todo!("schema mismatch"),
        }
    }
}