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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! Create custom (de)serializers for key and value (de)serialization.
//!
//! The default `Tree` uses bincode for (de)serialization of types
//! that implement DeserializeOwned. However if you want to use
//! zero-copy deserialization, lazy deserialization or simply want
//! to support deserialization of types that don't implement DeserializeOwned
//! you need a different Deserializer. Implementing [SerDe] and
//! using it together with a [Tree][crate::custom_serde::Tree] allows you
//! to do just that.

// use rkyv::{archived_root, ser::Serializer as _, AlignedVec, Archive, Archived};
use serde::de::DeserializeOwned;
use std::convert::AsRef;

/// The default `Tree` uses bincode for (de)serialization of types
/// that implement DeserializeOwned. However if you want to use
/// zero-copy deserialization, lazy deserialization or simply want
/// to support deserialization of types that don't implement DeserializeOwned
/// you need a different Deserializer. Implementing this trait and
/// using it together with a [Tree][crate::custom_serde::Tree] allows you
/// to do just that.
pub trait SerDe<K, V> {
    /// Key Serializer
    type SK: Serializer<K>;
    /// Value Serializer
    type SV: Serializer<V>;
    /// Key Deserializer
    type DK: Deserializer<K>;
    /// Value Deserializer
    type DV: Deserializer<V>;
}

pub type Key<K, V, SD> = <<SD as SerDe<K, V>>::DK as Deserializer<K>>::DeserializedValue;
pub type Value<K, V, SD> = <<SD as SerDe<K, V>>::DV as Deserializer<V>>::DeserializedValue;

pub trait Serializer<T> {
    type Bytes: AsRef<[u8]>;

    fn serialize(value: &T) -> Self::Bytes;
}

pub trait Deserializer<T> {
    type DeserializedValue;

    fn deserialize(bytes: sled::IVec) -> Self::DeserializedValue;
}

/// (De)serializer using bincode.
#[derive(Debug)]
pub struct BincodeSerDe;
pub trait BincodeSerDeBounds: serde::Serialize + DeserializeOwned {}
impl<T> BincodeSerDeBounds for T where T: serde::Serialize + DeserializeOwned {}
#[derive(Debug)]
pub struct BincodeSerDeLazy;
#[derive(Debug)]
pub struct BincodeSerDeLazyK;
#[derive(Debug)]
pub struct BincodeSerDeLazyV;
#[derive(Debug)]
pub struct BincodeSerializer;
#[derive(Debug)]
pub struct BincodeDeserializer;
pub struct BincodeDeserializerLazy;

impl<K: serde::Serialize + DeserializeOwned, V: serde::Serialize + DeserializeOwned> SerDe<K, V>
    for BincodeSerDe
{
    type SK = BincodeSerializer;
    type SV = BincodeSerializer;
    type DK = BincodeDeserializer;
    type DV = BincodeDeserializer;
}

impl<K: serde::Serialize, V: serde::Serialize> SerDe<K, V> for BincodeSerDeLazy {
    type SK = BincodeSerializer;
    type SV = BincodeSerializer;
    type DK = BincodeDeserializerLazy;
    type DV = BincodeDeserializerLazy;
}

impl<K: serde::Serialize, V: serde::Serialize + DeserializeOwned> SerDe<K, V>
    for BincodeSerDeLazyK
{
    type SK = BincodeSerializer;
    type SV = BincodeSerializer;
    type DK = BincodeDeserializerLazy;
    type DV = BincodeDeserializer;
}

impl<K: serde::Serialize + DeserializeOwned, V: serde::Serialize> SerDe<K, V>
    for BincodeSerDeLazyV
{
    type SK = BincodeSerializer;
    type SV = BincodeSerializer;
    type DK = BincodeDeserializer;
    type DV = BincodeDeserializerLazy;
}

impl<T: serde::Serialize> Serializer<T> for BincodeSerializer {
    type Bytes = Vec<u8>;

    fn serialize(value: &T) -> Self::Bytes {
        bincode::serialize(value).expect("serialization failed, did the type serialized change?")
    }
}

impl<T: serde::de::DeserializeOwned> Deserializer<T> for BincodeDeserializer {
    type DeserializedValue = T;

    fn deserialize(bytes: sled::IVec) -> Self::DeserializedValue {
        bincode::deserialize(&bytes)
            .expect("deserialization failed, did the type serialized change?")
    }
}

impl<T> Deserializer<T> for BincodeDeserializerLazy {
    type DeserializedValue = Lazy<T>;

    fn deserialize(bytes: sled::IVec) -> Self::DeserializedValue {
        Lazy::new(bytes)
    }
}

pub struct Lazy<T> {
    v: sled::IVec,
    _t: std::marker::PhantomData<fn() -> T>,
}

impl<T> Lazy<T> {
    fn new(v: sled::IVec) -> Self {
        Self {
            v,
            _t: std::marker::PhantomData,
        }
    }
}

impl<T> Lazy<T> {
    pub fn deserialize<'de>(&'de self) -> T
    where
        T: serde::Deserialize<'de>,
    {
        bincode::deserialize(&self.v)
            .expect("deserialization failed, did the type serialized change?")
    }
}

#[test]
fn test_lazy() {
    let ref_str_bytes = sled::IVec::from(
        bincode::serialize::<&str>(&"hello there my darling how has your day been?").unwrap(),
    );
    let l = Lazy::<&str>::new(ref_str_bytes);
    l.deserialize();
}

// TODO: Implement (De)serializers for rkyv.