From df02910660a51aa069999cfcf37efab0b67ed6e3 Mon Sep 17 00:00:00 2001 From: gwenn <45554+gwenn@users.noreply.github.com> Date: Wed, 26 May 2021 14:51:28 +0200 Subject: [PATCH] Implement AsRef for Row(s) (#887) --- src/column.rs | 68 +-------------------------------------------------- src/lib.rs | 2 +- src/row.rs | 11 +++++++++ 3 files changed, 13 insertions(+), 68 deletions(-) diff --git a/src/column.rs b/src/column.rs index b9122c4..ae965a6 100644 --- a/src/column.rs +++ b/src/column.rs @@ -1,6 +1,6 @@ use std::str; -use crate::{Error, Result, Row, Rows, Statement}; +use crate::{Error, Result, Statement}; /// Information about a column of a SQLite query. #[derive(Debug)] @@ -147,72 +147,6 @@ impl Statement<'_> { } } -impl<'stmt> Rows<'stmt> { - /// Get all the column names. - #[inline] - pub fn column_names(&self) -> Option> { - self.stmt.map(Statement::column_names) - } - - /// Return the number of columns. - #[inline] - pub fn column_count(&self) -> Option { - self.stmt.map(Statement::column_count) - } - - /// Return the name of the column. - #[inline] - pub fn column_name(&self, col: usize) -> Option> { - self.stmt.map(|stmt| stmt.column_name(col)) - } - - /// Return the index of the column. - #[inline] - pub fn column_index(&self, name: &str) -> Option> { - self.stmt.map(|stmt| stmt.column_index(name)) - } - - /// Returns a slice describing the columns of the Rows. - #[inline] - #[cfg(feature = "column_decltype")] - pub fn columns(&self) -> Option> { - self.stmt.map(Statement::columns) - } -} - -impl<'stmt> Row<'stmt> { - /// Get all the column names of the Row. - #[inline] - pub fn column_names(&self) -> Vec<&str> { - self.stmt.column_names() - } - - /// Return the number of columns in the current row. - #[inline] - pub fn column_count(&self) -> usize { - self.stmt.column_count() - } - - /// Return the name of the column. - #[inline] - pub fn column_name(&self, col: usize) -> Result<&str> { - self.stmt.column_name(col) - } - - /// Return the index of the column. - #[inline] - pub fn column_index(&self, name: &str) -> Result { - self.stmt.column_index(name) - } - - /// Returns a slice describing the columns of the Row. - #[inline] - #[cfg(feature = "column_decltype")] - pub fn columns(&self) -> Vec { - self.stmt.columns() - } -} - #[cfg(test)] mod test { use crate::{Connection, Result}; diff --git a/src/lib.rs b/src/lib.rs index f588b39..f62ed41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1861,7 +1861,7 @@ mod test { db.execute_batch(sql)?; db.query_row("SELECT * FROM foo", [], |r| { - assert_eq!(2, r.column_count()); + assert_eq!(2, r.as_ref().column_count()); Ok(()) }) } diff --git a/src/row.rs b/src/row.rs index 137f13d..e974919 100644 --- a/src/row.rs +++ b/src/row.rs @@ -78,6 +78,11 @@ impl<'stmt> Rows<'stmt> { { AndThenRows { rows: self, map: f } } + + /// Give access to the underlying statement + pub fn as_ref(&self) -> Option<&Statement<'stmt>> { + self.stmt + } } impl<'stmt> Rows<'stmt> { @@ -358,6 +363,12 @@ impl<'stmt> Row<'stmt> { } } +impl<'stmt> AsRef> for Row<'stmt> { + fn as_ref(&self) -> &Statement<'stmt> { + self.stmt + } +} + mod sealed { /// This trait exists just to ensure that the only impls of `trait Params` /// that are allowed are ones in this crate.