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
use crate::de::KeyDeserialize;
use crate::keys::Key;
#[cfg(feature = "iterator")]
use crate::{Bound, Bounder};
use crate::{Endian, Prefixer, PrimaryKey};
use std::marker::PhantomData;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IntKeyOld<T: Endian> {
    pub wrapped: Vec<u8>,
    pub data: PhantomData<T>,
}

impl<T: Endian> IntKeyOld<T> {
    pub fn new(val: T) -> Self {
        IntKeyOld {
            wrapped: val.to_be_bytes().into(),
            data: PhantomData,
        }
    }
}

impl<T: Endian> From<T> for IntKeyOld<T> {
    fn from(val: T) -> Self {
        IntKeyOld::new(val)
    }
}

impl<T: Endian> From<Vec<u8>> for IntKeyOld<T> {
    fn from(wrap: Vec<u8>) -> Self {
        IntKeyOld {
            wrapped: wrap,
            data: PhantomData,
        }
    }
}

impl<T: Endian> From<IntKeyOld<T>> for Vec<u8> {
    fn from(k: IntKeyOld<T>) -> Vec<u8> {
        k.wrapped
    }
}

// this auto-implements PrimaryKey for all the IntKeyOld types
impl<'a, T: Endian + Clone> PrimaryKey<'a> for IntKeyOld<T>
where
    IntKeyOld<T>: KeyDeserialize,
{
    type Prefix = ();
    type SubPrefix = ();
    type Suffix = Self;
    type SuperSuffix = Self;

    fn key(&self) -> Vec<Key> {
        self.wrapped.key()
    }
}

// this auto-implements Prefixer for all the IntKey types
impl<'a, T: Endian> Prefixer<'a> for IntKeyOld<T> {
    fn prefix(&self) -> Vec<Key> {
        self.wrapped.prefix()
    }
}

// this auto-implements Bounder for all the IntKey types
#[cfg(feature = "iterator")]
impl<'a, T: Endian> Bounder<'a> for IntKeyOld<T>
where
    IntKeyOld<T>: KeyDeserialize,
{
    fn inclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::inclusive(self))
    }

    fn exclusive_bound(self) -> Option<Bound<'a, Self>> {
        Some(Bound::exclusive(self))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn u64key_old_works() {
        let k: IntKeyOld<u64> = 134u64.into();
        let path = k.key();
        assert_eq!(1, path.len());
        assert_eq!(134u64.to_be_bytes(), path[0].as_ref());
    }

    #[test]
    fn i32key_old_works() {
        let k: IntKeyOld<i32> = 4242i32.into();
        let path = k.key();
        assert_eq!(1, path.len());
        assert_eq!(4242i32.to_be_bytes(), path[0].as_ref());

        let k: IntKeyOld<i32> = IntKeyOld::<i32>::from(-4242i32);
        let path = k.key();
        assert_eq!(1, path.len());
        assert_eq!((-4242i32).to_be_bytes(), path[0].as_ref());
    }
}