1use super::{IntoRecords, Records};
2
3/// A [Records] implementation for any [IntoIterator].
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
5pub struct IterRecords<I> {
6 iter: I,
7 count_columns: usize,
8 count_rows: Option<usize>,
9}
10
11impl<I> IterRecords<I> {
12 /// Returns a new [IterRecords] object.
13 pub const fn new(iter: I, count_columns: usize, count_rows: Option<usize>) -> Self {
14 Self {
15 iter,
16 count_columns,
17 count_rows,
18 }
19 }
20}
21
22impl<I> IntoRecords for IterRecords<I>
23where
24 I: IntoRecords,
25{
26 type Cell = I::Cell;
27 type IterColumns = I::IterColumns;
28 type IterRows = I::IterRows;
29
30 fn iter_rows(self) -> Self::IterRows {
31 self.iter.iter_rows()
32 }
33}
34
35// why this does not work?
36
37// impl<'a, I> IntoRecords for &'a IterRecords<I>
38// where
39// &'a I: IntoRecords,
40// {
41// type Cell = <&'a I as IntoRecords>::Cell;
42// type IterColumns = <&'a I as IntoRecords>::IterColumns;
43// type IterRows = <&'a I as IntoRecords>::IterRows;
44
45// fn iter_rows(self) -> Self::IterRows {
46// // (&self.iter).iter_rows()
47// todo!()
48// }
49// }
50
51impl<I> Records for IterRecords<I>
52where
53 I: IntoRecords,
54{
55 type Iter = I;
56
57 fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows {
58 self.iter.iter_rows()
59 }
60
61 fn count_columns(&self) -> usize {
62 self.count_columns
63 }
64
65 fn hint_count_rows(&self) -> Option<usize> {
66 self.count_rows
67 }
68}
69
70impl<'a, I> Records for &'a IterRecords<I>
71where
72 &'a I: IntoRecords,
73{
74 type Iter = &'a I;
75
76 fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows {
77 (&self.iter).iter_rows()
78 }
79
80 fn count_columns(&self) -> usize {
81 self.count_columns
82 }
83
84 fn hint_count_rows(&self) -> Option<usize> {
85 self.count_rows
86 }
87}
88