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
use std::{mem, str};

/// A Key can be used as a key to a database
pub trait Key: AsRef<[u8]> {}

/// A Value can be stored in a database
pub trait Value<'a>: AsRef<[u8]> {
    /// Used to convert a byte-slice to Value
    fn from_raw(raw: &'a [u8]) -> Self;
}

/// Integer key type
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Integer([u8; 8]);

impl From<u64> for Integer {
    #[cfg(target_endian = "little")]
    fn from(i: u64) -> Integer {
        unsafe { Integer(mem::transmute(i.to_le())) }
    }

    #[cfg(target_endian = "big")]
    fn from(i: u64) -> Integer {
        unsafe { Integer(mem::transmute(i.to_be())) }
    }
}

impl From<Integer> for u64 {
    fn from(i: Integer) -> u64 {
        unsafe { mem::transmute(i.0) }
    }
}

impl AsRef<[u8]> for Integer {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl<'a> From<&'a [u8]> for Integer {
    fn from(buf: &'a [u8]) -> Integer {
        let mut dst = Integer::from(0);
        dst.0[..8].clone_from_slice(&buf[..8]);
        dst
    }
}

/// A reference to an existing value slice
#[derive(Debug, PartialEq, PartialOrd)]
pub struct ValueRef<'a>(&'a [u8]);

impl<'a> ValueRef<'a> {
    /// Create a new ValueRef from an existing byte slice
    pub fn new(buf: &'a [u8]) -> ValueRef<'a> {
        ValueRef(buf)
    }
}

impl<'a> AsRef<[u8]> for ValueRef<'a> {
    fn as_ref(&self) -> &[u8] {
        self.0
    }
}

impl<'a> Value<'a> for ValueRef<'a> {
    fn from_raw(raw: &'a [u8]) -> ValueRef<'a> {
        ValueRef(raw)
    }
}

impl<'a> From<&'a str> for ValueRef<'a> {
    fn from(s: &'a str) -> ValueRef<'a> {
        ValueRef(s.as_bytes())
    }
}

/// A mutable reference to an existing value slice
#[derive(Debug)]
pub struct ValueMut<'a>(&'a mut [u8]);

impl<'a> ValueMut<'a> {
    /// Create a new ValueMut from an existing byte slice
    pub fn new(buf: &'a mut [u8]) -> ValueMut<'a> {
        ValueMut(buf)
    }

    /// Convert a ValueMut to ValueRef
    pub fn as_value<V: Value<'a>>(&'a self) -> ValueRef<'a> {
        ValueRef(self.0)
    }
}

impl<'a> AsMut<[u8]> for ValueMut<'a> {
    fn as_mut(&mut self) -> &mut [u8] {
        self.0
    }
}

impl<'a> AsRef<[u8]> for ValueMut<'a> {
    fn as_ref(&self) -> &[u8] {
        self.0
    }
}

impl<'a, S: AsRef<[u8]>> Key for S {}

impl<'a> Value<'a> for &'a [u8] {
    fn from_raw(raw: &'a [u8]) -> Self {
        raw
    }
}

impl<'a> Value<'a> for &'a str {
    fn from_raw(raw: &'a [u8]) -> Self {
        unsafe { str::from_utf8_unchecked(raw) }
    }
}

impl<'a> Value<'a> for String {
    fn from_raw(raw: &'a [u8]) -> Self {
        unsafe { String::from(str::from_utf8_unchecked(raw)) }
    }
}