Repository navigation
Expand file tree
/
Copy pathencode.rs
More file actions
68 lines (55 loc) · 1.43 KB
/
Copy pathencode.rs
File metadata and controls
68 lines (55 loc) · 1.43 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
use std::mem;
use crate::database::Database;
/// 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<DB: Database> {
fn produces() -> DB::TypeInfo;
/// Writes the value of `self` into `buf` in the expected format for the database.
#[must_use]
fn encode(&self, buf: &mut DB::RawBuffer) -> IsNull;
#[inline]
fn size_hint(&self) -> usize {
mem::size_of_val(self)
}
}
impl<T: Encode<DB> + ?Sized, DB: Database> Encode<DB> for &'_ T {
#[inline]
fn produces() -> DB::TypeInfo {
<T as Encode<DB>>::produces()
}
#[inline]
fn encode(&self, buf: &mut DB::RawBuffer) -> IsNull {
(*self).encode(buf)
}
#[inline]
fn size_hint(&self) -> usize {
(*self).size_hint()
}
}
impl<T: Encode<DB>, DB: Database> Encode<DB> for Option<T> {
#[inline]
fn produces() -> DB::TypeInfo {
<T as Encode<DB>>::produces()
}
#[inline]
fn encode(&self, buf: &mut DB::RawBuffer) -> IsNull {
if let Some(v) = self {
v.encode(buf);
IsNull::No
} else {
IsNull::Yes
}
}
#[inline]
fn size_hint(&self) -> usize {
self.as_ref().map_or(0, Encode::size_hint)
}
}