Repository navigation
Expand file tree
/
Copy pathencode.rs
More file actions
98 lines (83 loc) · 2.23 KB
/
Copy pathencode.rs
File metadata and controls
98 lines (83 loc) · 2.23 KB
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
use std::mem;
use crate::database::{Database, HasArguments};
use crate::types::Type;
/// The return type of [Encode::encode].
pub enum IsNull {
/// The value is null; no data was written.
Yes,
/// The value is not null.
///
/// This does not mean that data was written.
No,
}
/// Encode a single value to be sent to the database.
pub trait Encode<'q, DB: Database>: Type<DB> {
fn produces(&self) -> DB::TypeInfo {
Self::type_info()
}
/// Writes the value of `self` into `buf` in the expected format for the database.
#[must_use]
fn encode(self, buf: &mut <DB as HasArguments<'q>>::Arguments) -> IsNull
where
Self: Sized,
{
self.encode_by_ref(buf)
}
#[must_use]
fn encode_by_ref(&self, buf: &mut <DB as HasArguments<'q>>::Arguments) -> IsNull;
#[inline]
fn size_hint(&self) -> usize {
mem::size_of_val(self)
}
}
impl<'q, T, DB: Database> Encode<'q, DB> for &'_ T
where
T: Encode<'q, DB>,
{
#[inline]
fn produces(&self) -> DB::TypeInfo {
(**self).produces()
}
#[inline]
fn encode(self, buf: &mut <DB as HasArguments<'q>>::Arguments) -> IsNull {
<T as Encode<DB>>::encode_by_ref(self, buf)
}
#[inline]
fn encode_by_ref(&self, buf: &mut <DB as HasArguments<'q>>::Arguments) -> IsNull {
<&T as Encode<DB>>::encode(self, buf)
}
#[inline]
fn size_hint(&self) -> usize {
(**self).size_hint()
}
}
impl<'q, T: 'q + Encode<'q, DB>, DB: Database> Encode<'q, DB> for Option<T> {
#[inline]
fn produces(&self) -> DB::TypeInfo {
if let Some(v) = self {
v.produces()
} else {
T::type_info()
}
}
#[inline]
fn encode(self, buf: &mut <DB as HasArguments<'q>>::Arguments) -> IsNull {
if let Some(v) = self {
v.encode(buf)
} else {
IsNull::Yes
}
}
#[inline]
fn encode_by_ref(&self, buf: &mut <DB as HasArguments<'q>>::Arguments) -> IsNull {
if let Some(v) = self {
v.encode_by_ref(buf)
} else {
IsNull::Yes
}
}
#[inline]
fn size_hint(&self) -> usize {
self.as_ref().map_or(0, Encode::size_hint)
}
}