drizzle 0.3.1

A type-safe SQL query builder for Rust
use core::marker::PhantomData;

use drizzle_core::error::DrizzleError;

/// Rows returned by `.rows()` on the PostgreSQL drivers: fetched up front,
/// decoded into `R` as you iterate.
pub struct DecodeRows<RowT, R> {
    #[cfg(feature = "std")]
    rows: std::vec::IntoIter<RowT>,
    #[cfg(not(feature = "std"))]
    rows: alloc::vec::IntoIter<RowT>,
    _marker: PhantomData<R>,
}

impl<RowT, R> DecodeRows<RowT, R> {
    #[cfg(feature = "std")]
    pub(crate) fn new(rows: Vec<RowT>) -> Self {
        Self {
            rows: rows.into_iter(),
            _marker: PhantomData,
        }
    }

    #[cfg(not(feature = "std"))]
    pub(crate) fn new(rows: alloc::vec::Vec<RowT>) -> Self {
        Self {
            rows: rows.into_iter(),
            _marker: PhantomData,
        }
    }
}

impl<RowT, R> Iterator for DecodeRows<RowT, R>
where
    R: for<'r> TryFrom<&'r RowT>,
    for<'r> <R as TryFrom<&'r RowT>>::Error: Into<DrizzleError>,
{
    type Item = drizzle_core::error::Result<R>;

    fn next(&mut self) -> Option<Self::Item> {
        self.rows
            .next()
            .map(|row| R::try_from(&row).map_err(Into::into))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.rows.size_hint()
    }
}

impl<RowT, R> ExactSizeIterator for DecodeRows<RowT, R>
where
    R: for<'r> TryFrom<&'r RowT>,
    for<'r> <R as TryFrom<&'r RowT>>::Error: Into<DrizzleError>,
{
}