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
158
159
160
161
162
163
164
165
166
167
168
169
use std::error::Error;
use std::fmt;
use std::io::{self, Write};
use std::ops::{Deref, DerefMut};
use std::result;
use backend::Backend;
use sql_types::TypeMetadata;
pub type Result = result::Result<IsNull, Box<Error + Send + Sync>>;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum IsNull {
Yes,
No,
}
#[derive(Clone, Copy)]
pub struct Output<'a, T, DB>
where
DB: TypeMetadata,
DB::MetadataLookup: 'a,
{
out: T,
metadata_lookup: &'a DB::MetadataLookup,
}
impl<'a, T, DB: TypeMetadata> Output<'a, T, DB> {
pub fn new(out: T, metadata_lookup: &'a DB::MetadataLookup) -> Self {
Output {
out,
metadata_lookup,
}
}
pub fn with_buffer<U>(&self, new_out: U) -> Output<'a, U, DB> {
Output {
out: new_out,
metadata_lookup: self.metadata_lookup,
}
}
pub fn into_inner(self) -> T {
self.out
}
pub fn metadata_lookup(&self) -> &'a DB::MetadataLookup {
self.metadata_lookup
}
}
#[cfg(test)]
impl<DB: TypeMetadata> Output<'static, Vec<u8>, DB> {
pub fn test() -> Self {
use std::mem;
#[cfg_attr(feature = "clippy", allow(invalid_ref))]
Self::new(Vec::new(), unsafe { mem::uninitialized() })
}
}
impl<'a, T: Write, DB: TypeMetadata> Write for Output<'a, T, DB> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.out.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.out.flush()
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.out.write_all(buf)
}
fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
self.out.write_fmt(fmt)
}
}
impl<'a, T, DB: TypeMetadata> Deref for Output<'a, T, DB> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.out
}
}
impl<'a, T, DB: TypeMetadata> DerefMut for Output<'a, T, DB> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.out
}
}
impl<'a, T, U, DB> PartialEq<U> for Output<'a, T, DB>
where
DB: TypeMetadata,
T: PartialEq<U>,
{
fn eq(&self, rhs: &U) -> bool {
self.out == *rhs
}
}
impl<'a, T, DB> fmt::Debug for Output<'a, T, DB>
where
T: fmt::Debug,
DB: TypeMetadata,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.out.fmt(f)
}
}
pub trait ToSql<A, DB: Backend>: fmt::Debug {
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> Result;
}
impl<'a, A, T, DB> ToSql<A, DB> for &'a T
where
DB: Backend,
T: ToSql<A, DB> + ?Sized,
{
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> Result {
(*self).to_sql(out)
}
}