typed_sled/custom_serde/
serialize.rs1use serde::de::DeserializeOwned;
13use std::convert::AsRef;
14
15pub trait SerDe<K, V> {
23 type SK: Serializer<K>;
25 type SV: Serializer<V>;
27 type DK: Deserializer<K>;
29 type DV: Deserializer<V>;
31}
32
33pub type Key<K, V, SD> = <<SD as SerDe<K, V>>::DK as Deserializer<K>>::DeserializedValue;
34pub type Value<K, V, SD> = <<SD as SerDe<K, V>>::DV as Deserializer<V>>::DeserializedValue;
35
36pub trait Serializer<T> {
37 type Bytes: AsRef<[u8]>;
38
39 fn serialize(value: &T) -> Self::Bytes;
40}
41
42pub trait Deserializer<T> {
43 type DeserializedValue;
44
45 fn deserialize(bytes: sled::IVec) -> Self::DeserializedValue;
46}
47
48#[derive(Debug)]
50pub struct BincodeSerDe;
51pub trait BincodeSerDeBounds: serde::Serialize + DeserializeOwned {}
52impl<T> BincodeSerDeBounds for T where T: serde::Serialize + DeserializeOwned {}
53#[derive(Debug)]
54pub struct BincodeSerDeLazy;
55#[derive(Debug)]
56pub struct BincodeSerDeLazyK;
57#[derive(Debug)]
58pub struct BincodeSerDeLazyV;
59#[derive(Debug)]
60pub struct BincodeSerializer;
61#[derive(Debug)]
62pub struct BincodeDeserializer;
63pub struct BincodeDeserializerLazy;
64
65impl<K: serde::Serialize + DeserializeOwned, V: serde::Serialize + DeserializeOwned> SerDe<K, V>
66 for BincodeSerDe
67{
68 type SK = BincodeSerializer;
69 type SV = BincodeSerializer;
70 type DK = BincodeDeserializer;
71 type DV = BincodeDeserializer;
72}
73
74impl<K: serde::Serialize, V: serde::Serialize> SerDe<K, V> for BincodeSerDeLazy {
75 type SK = BincodeSerializer;
76 type SV = BincodeSerializer;
77 type DK = BincodeDeserializerLazy;
78 type DV = BincodeDeserializerLazy;
79}
80
81impl<K: serde::Serialize, V: serde::Serialize + DeserializeOwned> SerDe<K, V>
82 for BincodeSerDeLazyK
83{
84 type SK = BincodeSerializer;
85 type SV = BincodeSerializer;
86 type DK = BincodeDeserializerLazy;
87 type DV = BincodeDeserializer;
88}
89
90impl<K: serde::Serialize + DeserializeOwned, V: serde::Serialize> SerDe<K, V>
91 for BincodeSerDeLazyV
92{
93 type SK = BincodeSerializer;
94 type SV = BincodeSerializer;
95 type DK = BincodeDeserializer;
96 type DV = BincodeDeserializerLazy;
97}
98
99impl<T: serde::Serialize> Serializer<T> for BincodeSerializer {
100 type Bytes = Vec<u8>;
101
102 fn serialize(value: &T) -> Self::Bytes {
103 bincode::serialize(value).expect("serialization failed, did the type serialized change?")
104 }
105}
106
107impl<T: serde::de::DeserializeOwned> Deserializer<T> for BincodeDeserializer {
108 type DeserializedValue = T;
109
110 fn deserialize(bytes: sled::IVec) -> Self::DeserializedValue {
111 bincode::deserialize(&bytes)
112 .expect("deserialization failed, did the type serialized change?")
113 }
114}
115
116impl<T> Deserializer<T> for BincodeDeserializerLazy {
117 type DeserializedValue = Lazy<T>;
118
119 fn deserialize(bytes: sled::IVec) -> Self::DeserializedValue {
120 Lazy::new(bytes)
121 }
122}
123
124pub struct Lazy<T> {
125 v: sled::IVec,
126 _t: std::marker::PhantomData<fn() -> T>,
127}
128
129impl<T> Lazy<T> {
130 fn new(v: sled::IVec) -> Self {
131 Self {
132 v,
133 _t: std::marker::PhantomData,
134 }
135 }
136}
137
138impl<T> Lazy<T> {
139 pub fn deserialize<'de>(&'de self) -> T
140 where
141 T: serde::Deserialize<'de>,
142 {
143 bincode::deserialize(&self.v)
144 .expect("deserialization failed, did the type serialized change?")
145 }
146}
147
148#[test]
149fn test_lazy() {
150 let ref_str_bytes = sled::IVec::from(
151 bincode::serialize::<&str>(&"hello there my darling how has your day been?").unwrap(),
152 );
153 let l = Lazy::<&str>::new(ref_str_bytes);
154 l.deserialize();
155}
156
157