use crate::{backend::Backend, decode::Decode, types::HasSqlType}; pub trait RawRow: Send { type Backend: Backend; fn len(&self) -> usize; fn get_raw(&self, index: usize) -> Option<&[u8]>; fn get(&self, index: usize) -> T where Self::Backend: HasSqlType, T: Decode, { T::decode(self.get_raw(index)) } } pub struct Row(pub(crate) DB::Row) where DB: Backend; impl Row where DB: Backend, { pub fn get(&self, index: usize) -> T where DB: HasSqlType, T: Decode, { self.0.get(index) } } pub trait FromRow> { fn from_row(row: Row) -> Self; } #[allow(unused)] macro_rules! impl_from_row { ($B:ident: $( ($idx:tt) -> $T:ident );+;) => { // Row -> (T1, T2, ...) impl<$($T,)+> crate::row::FromRow<$B> for ($($T,)+) where $($B: crate::types::HasSqlType<$T>,)+ $($T: crate::decode::Decode<$B>,)+ { #[inline] fn from_row(row: crate::row::Row<$B>) -> Self { ($(row.get($idx),)+) } } // (T1, T2, ...) -> (T1, T2, ...) impl<$($T,)+> crate::row::FromRow<$B, ($($T,)+)> for ($($T,)+) where $($B: crate::types::HasSqlType<$T>,)+ $($T: crate::decode::Decode<$B>,)+ { #[inline] fn from_row(row: crate::row::Row<$B>) -> Self { ($(row.get($idx),)+) } } }; } #[allow(unused)] macro_rules! impl_from_row_for_backend { ($B:ident) => { impl crate::row::FromRow<$B> for crate::row::Row<$B> where $B: crate::Backend { #[inline] fn from_row(row: crate::row::Row<$B>) -> Self { row } } impl_from_row!($B: (0) -> T1; ); impl_from_row!($B: (0) -> T1; (1) -> T2; ); impl_from_row!($B: (0) -> T1; (1) -> T2; (2) -> T3; ); impl_from_row!($B: (0) -> T1; (1) -> T2; (2) -> T3; (3) -> T4; ); impl_from_row!($B: (0) -> T1; (1) -> T2; (2) -> T3; (3) -> T4; (4) -> T5; ); impl_from_row!($B: (0) -> T1; (1) -> T2; (2) -> T3; (3) -> T4; (4) -> T5; (5) -> T6; ); impl_from_row!($B: (0) -> T1; (1) -> T2; (2) -> T3; (3) -> T4; (4) -> T5; (5) -> T6; (6) -> T7; ); impl_from_row!($B: (0) -> T1; (1) -> T2; (2) -> T3; (3) -> T4; (4) -> T5; (5) -> T6; (6) -> T7; (7) -> T8; ); impl_from_row!($B: (0) -> T1; (1) -> T2; (2) -> T3; (3) -> T4; (4) -> T5; (5) -> T6; (6) -> T7; (7) -> T8; (8) -> T9; ); } }