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
use crate::database::Database;
use crate::types::Type;
use std::mem;
pub enum IsNull {
Yes,
No,
}
pub trait Encode<DB>
where
DB: Database + ?Sized,
{
fn encode(&self, buf: &mut DB::RawBuffer);
fn encode_nullable(&self, buf: &mut DB::RawBuffer) -> IsNull {
self.encode(buf);
IsNull::No
}
fn size_hint(&self) -> usize {
mem::size_of_val(self)
}
}
impl<T: ?Sized, DB> Encode<DB> for &'_ T
where
DB: Database,
T: Type<DB>,
T: Encode<DB>,
{
fn encode(&self, buf: &mut DB::RawBuffer) {
(*self).encode(buf)
}
fn encode_nullable(&self, buf: &mut DB::RawBuffer) -> IsNull {
(*self).encode_nullable(buf)
}
fn size_hint(&self) -> usize {
(*self).size_hint()
}
}
impl<T, DB> Encode<DB> for Option<T>
where
DB: Database,
T: Type<DB>,
T: Encode<DB>,
{
fn encode(&self, buf: &mut DB::RawBuffer) {
let _ = self.encode_nullable(buf);
}
fn encode_nullable(&self, buf: &mut DB::RawBuffer) -> IsNull {
if let Some(self_) = self {
self_.encode(buf);
IsNull::No
} else {
IsNull::Yes
}
}
fn size_hint(&self) -> usize {
self.as_ref().map_or(0, Encode::size_hint)
}
}