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
use super::{IntoRecords, Records};

/// A [Records] implementation for any [IntoIterator].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct IterRecords<I> {
    iter: I,
    count_columns: usize,
    count_rows: Option<usize>,
}

impl<I> IterRecords<I> {
    /// Returns a new [IterRecords] object.
    pub const fn new(iter: I, count_columns: usize, count_rows: Option<usize>) -> Self {
        Self {
            iter,
            count_columns,
            count_rows,
        }
    }
}

impl<I> IntoRecords for IterRecords<I>
where
    I: IntoRecords,
{
    type Cell = I::Cell;
    type IterColumns = I::IterColumns;
    type IterRows = I::IterRows;

    fn iter_rows(self) -> Self::IterRows {
        self.iter.iter_rows()
    }
}

// why this does not work?

// impl<'a, I> IntoRecords for &'a IterRecords<I>
// where
//     &'a I: IntoRecords,
// {
//     type Cell = <&'a I as IntoRecords>::Cell;
//     type IterColumns = <&'a I as IntoRecords>::IterColumns;
//     type IterRows = <&'a I as IntoRecords>::IterRows;

//     fn iter_rows(self) -> Self::IterRows {
//         // (&self.iter).iter_rows()
//         todo!()
//     }
// }

impl<I> Records for IterRecords<I>
where
    I: IntoRecords,
{
    type Iter = I;

    fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows {
        self.iter.iter_rows()
    }

    fn count_columns(&self) -> usize {
        self.count_columns
    }

    fn hint_count_rows(&self) -> Option<usize> {
        self.count_rows
    }
}

impl<'a, I> Records for &'a IterRecords<I>
where
    &'a I: IntoRecords,
{
    type Iter = &'a I;

    fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows {
        (&self.iter).iter_rows()
    }

    fn count_columns(&self) -> usize {
        self.count_columns
    }

    fn hint_count_rows(&self) -> Option<usize> {
        self.count_rows
    }
}