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
use crate::{Error, SqlDataType, SqlDataTypeKind};
use std::{cmp, iter::FromIterator, marker::PhantomData};
pub trait ExtFnValueCursor {
type Item: ?Sized;
fn advance(&mut self) -> Result<(), Error>;
fn get(&self) -> Option<&Self::Item>;
fn is_null(&self) -> bool;
fn kind(&self) -> SqlDataTypeKind;
fn data_type(&self) -> SqlDataType;
fn len(&self) -> usize;
fn next(&mut self) -> Result<Option<&Self::Item>, Error> {
self.advance()?;
Ok((*self).get())
}
fn for_each<F>(mut self, mut f: F) -> Result<(), Error>
where
Self: Sized,
F: FnMut(&Self::Item),
{
while let Some(value) = self.next()? {
f(value);
}
Ok(())
}
fn fold<F, U>(mut self, init: U, f: F) -> Result<U, Error>
where
Self: Sized,
F: FnMut(U, &Self::Item) -> U,
{
let mut f = f;
let mut output = init;
while let Some(value) = self.next()? {
output = f(output, value);
}
Ok(output)
}
fn collect<U>(mut self) -> Result<U, Error>
where
Self: Sized,
U: for<'s> Extend<&'s Self::Item> + Default,
{
self.fold(U::default(), |mut out, x| {
out.extend(std::iter::once(x));
out
})
}
}
impl<'a, I: ?Sized> ExtFnValueCursor for &'a mut I
where
I: ExtFnValueCursor,
{
type Item = I::Item;
fn advance(&mut self) -> Result<(), Error> {
(**self).advance()
}
fn get(&self) -> Option<&I::Item> {
(**self).get()
}
fn next(&mut self) -> Result<Option<&I::Item>, Error> {
(**self).next()
}
fn is_null(&self) -> bool {
(**self).is_null()
}
fn kind(&self) -> SqlDataTypeKind {
(**self).kind()
}
fn data_type(&self) -> SqlDataType {
(**self).data_type()
}
fn len(&self) -> usize {
(**self).len()
}
}
#[cfg(test)]
mod test {
use super::*;
fn _is_object_safe(_: &dyn ExtFnValueCursor<Item = ()>) {}
}