xitca_postgres/
iter.rs

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
use core::future::Future;

/// async streaming iterator with borrowed Item from Self.
pub trait AsyncLendingIterator {
    type Ok<'i>
    where
        Self: 'i;
    type Err;

    fn try_next(&mut self) -> impl Future<Output = Result<Option<Self::Ok<'_>>, Self::Err>> + Send;

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, None)
    }
}

pub trait AsyncLendingIteratorExt: AsyncLendingIterator {
    fn map_ok<F, O>(self, func: F) -> MapOk<Self, F>
    where
        F: Fn(Self::Ok<'_>) -> O,
        Self: Sized,
    {
        MapOk { iter: self, func }
    }

    #[inline]
    fn try_collect<T>(self) -> impl Future<Output = Result<T, Self::Err>> + Send
    where
        T: Default + for<'i> Extend<Self::Ok<'i>> + Send,
        Self: Send + Sized,
    {
        self.try_collect_into(T::default())
    }

    fn try_collect_into<T>(mut self, mut collection: T) -> impl Future<Output = Result<T, Self::Err>> + Send
    where
        T: for<'i> Extend<Self::Ok<'i>> + Send,
        Self: Send + Sized,
    {
        async move {
            while let Some(item) = self.try_next().await? {
                collection.extend([item]);
            }
            Ok(collection)
        }
    }
}

pub struct MapOk<I, F> {
    iter: I,
    func: F,
}

impl<I, F, O> AsyncLendingIterator for MapOk<I, F>
where
    I: AsyncLendingIterator + Send,
    F: Fn(I::Ok<'_>) -> O + Send,
    O: Send,
{
    type Ok<'i>
        = O
    where
        Self: 'i;
    type Err = I::Err;

    async fn try_next(&mut self) -> Result<Option<Self::Ok<'_>>, Self::Err> {
        let next = self.iter.try_next().await?;
        Ok(next.map(|item| (self.func)(item)))
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<I> AsyncLendingIteratorExt for I where I: AsyncLendingIterator {}

async fn _try_collect_test(stream: crate::RowStreamOwned) -> Result<Vec<String>, crate::error::Error> {
    stream.map_ok(|row| row.get(0)).try_collect::<Vec<_>>().await
}