Generalize StatementContainer

This commit is contained in:
Steven Fackler 2016-12-18 17:30:29 -08:00
parent ce53a497ca
commit ad69145b56

View File

@ -16,39 +16,39 @@ use types::{FromSql, SessionInfo, WrongType};
use stmt::{Statement, Column}; use stmt::{Statement, Column};
use error::Error; use error::Error;
enum StatementContainer<'a> { enum MaybeOwned<'a, T: 'a> {
Borrowed(&'a Statement<'a>), Borrowed(&'a T),
Owned(Statement<'a>), Owned(T),
} }
impl<'a> Deref for StatementContainer<'a> { impl<'a, T> Deref for MaybeOwned<'a, T> {
type Target = Statement<'a>; type Target = T;
fn deref(&self) -> &Statement<'a> { fn deref(&self) -> &T {
match *self { match *self {
StatementContainer::Borrowed(s) => s, MaybeOwned::Borrowed(s) => s,
StatementContainer::Owned(ref s) => s, MaybeOwned::Owned(ref s) => s,
} }
} }
} }
/// The resulting rows of a query. /// The resulting rows of a query.
pub struct Rows<'stmt> { pub struct Rows<'stmt> {
stmt: StatementContainer<'stmt>, stmt: MaybeOwned<'stmt, Statement<'stmt>>,
data: Vec<Vec<Option<Vec<u8>>>>, data: Vec<Vec<Option<Vec<u8>>>>,
} }
impl<'a> RowsNew<'a> for Rows<'a> { impl<'a> RowsNew<'a> for Rows<'a> {
fn new(stmt: &'a Statement<'a>, data: Vec<Vec<Option<Vec<u8>>>>) -> Rows<'a> { fn new(stmt: &'a Statement<'a>, data: Vec<Vec<Option<Vec<u8>>>>) -> Rows<'a> {
Rows { Rows {
stmt: StatementContainer::Borrowed(stmt), stmt: MaybeOwned::Borrowed(stmt),
data: data, data: data,
} }
} }
fn new_owned(stmt: Statement<'a>, data: Vec<Vec<Option<Vec<u8>>>>) -> Rows<'a> { fn new_owned(stmt: Statement<'a>, data: Vec<Vec<Option<Vec<u8>>>>) -> Rows<'a> {
Rows { Rows {
stmt: StatementContainer::Owned(stmt), stmt: MaybeOwned::Owned(stmt),
data: data, data: data,
} }
} }
@ -87,7 +87,7 @@ impl<'stmt> Rows<'stmt> {
pub fn get<'a>(&'a self, idx: usize) -> Row<'a> { pub fn get<'a>(&'a self, idx: usize) -> Row<'a> {
Row { Row {
stmt: &*self.stmt, stmt: &*self.stmt,
data: Cow::Borrowed(&self.data[idx]), data: MaybeOwned::Borrowed(&self.data[idx]),
} }
} }
@ -122,7 +122,7 @@ impl<'a> Iterator for Iter<'a> {
self.iter.next().map(|row| { self.iter.next().map(|row| {
Row { Row {
stmt: &*self.stmt, stmt: &*self.stmt,
data: Cow::Borrowed(row), data: MaybeOwned::Borrowed(row),
} }
}) })
} }
@ -137,7 +137,7 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
self.iter.next_back().map(|row| { self.iter.next_back().map(|row| {
Row { Row {
stmt: &*self.stmt, stmt: &*self.stmt,
data: Cow::Borrowed(row), data: MaybeOwned::Borrowed(row),
} }
}) })
} }
@ -148,7 +148,7 @@ impl<'a> ExactSizeIterator for Iter<'a> {}
/// A single result row of a query. /// A single result row of a query.
pub struct Row<'a> { pub struct Row<'a> {
stmt: &'a Statement<'a>, stmt: &'a Statement<'a>,
data: Cow<'a, [Option<Vec<u8>>]>, data: MaybeOwned<'a, Vec<Option<Vec<u8>>>>,
} }
impl<'a> fmt::Debug for Row<'a> { impl<'a> fmt::Debug for Row<'a> {
@ -385,7 +385,7 @@ impl<'trans, 'stmt> FallibleIterator for LazyRows<'trans, 'stmt> {
.map(|r| { .map(|r| {
Row { Row {
stmt: self.stmt, stmt: self.stmt,
data: Cow::Owned(r), data: MaybeOwned::Owned(r),
} }
}); });