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
use std::mem::transmute;
use serde_cbor::{Value, from_slice, to_vec};

pub trait ToBytes {
    fn to_bytes(self: Self) -> Vec<u8>;
}

impl ToBytes for u64 {
    fn to_bytes(self: Self) -> Vec<u8> {
        let slice = unsafe { transmute::<u64, [u8; 8]>(self) };
        slice.to_vec()
    }
}

impl ToBytes for bool {
    fn to_bytes(self: Self) -> Vec<u8> {
        if self {
            vec![1]
        } else {
            vec![0]
        }
    }
}

impl ToBytes for &str {
    fn to_bytes(self: Self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }
}

impl ToBytes for String {
    fn to_bytes(self: Self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }
}

impl ToBytes for Vec<u8> {
    fn to_bytes(self: Self) -> Vec<u8> {
        self
    }
}

impl ToBytes for Vec<Value> {
    fn to_bytes(self: Self) -> Vec<u8> {
        to_vec(&self).unwrap()
    }
}

impl ToBytes for Value {
    fn to_bytes(self: Self) -> Vec<u8> {
        to_vec(&self).unwrap()
    }
}

pub trait FromBytes {
    fn from_bytes(_bytes: Vec<u8>) -> Self;
}

impl FromBytes for u64 {
    fn from_bytes(bytes: Vec<u8>) -> u64 {
        if bytes.len() == 8 {
            let mut slice: [u8; 8] = [0; 8];
            slice.copy_from_slice(&bytes[..]);
            unsafe { transmute::<[u8; 8], u64>(slice) }
        } else {
            0
        }
    }
}

impl FromBytes for Vec<Value> {
    fn from_bytes(bytes: Vec<u8>) -> Vec<Value> {
        if bytes.len() == 0 {
            vec![]
        } else {
            let value: Value = from_slice(&bytes).unwrap();
            value.as_array().unwrap().to_vec()
        }
    }
}

impl FromBytes for Vec<u8> {
    fn from_bytes(bytes: Vec<u8>) -> Vec<u8> {
        bytes
    }
}

impl FromBytes for bool {
    fn from_bytes(bytes: Vec<u8>) -> bool {
        if bytes.len() == 1 {
            bytes[0] == 1
        } else if bytes.len() == 0 {
            false
        } else {
            panic!("Cannot convert {} bytes to bool", bytes.len())
        }
    }
}