From 20d4d8a71a9670ca796a3cac6bad7e188d8a2484 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Fri, 27 Apr 2018 17:26:49 -0700 Subject: [PATCH] Fix docs for simple_query Also fixes the return type to be consistent with regular "Rows" API. --- postgres/src/lib.rs | 8 +++++--- postgres/src/text_rows.rs | 12 +++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index 6da7f068..99df51b0 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -1365,9 +1365,11 @@ impl Connection { /// ```rust,no_run /// # use postgres::{Connection, TlsMode}; /// # let conn = Connection::connect("", TlsMode::None).unwrap(); - /// for row in &conn.simple_query("SELECT foo FROM bar WHERE baz = 'quux'").unwrap() { - /// let foo: String = row.get("foo"); - /// println!("foo: {}", foo); + /// for response in &conn.simple_query("SELECT foo FROM bar WHERE baz = 'quux'").unwrap() { + /// for row in response { + /// let foo: &str = row.get("foo"); + /// println!("foo: {}", foo); + /// } /// } /// ``` pub fn simple_query(&self, query: &str) -> Result> { diff --git a/postgres/src/text_rows.rs b/postgres/src/text_rows.rs index 208c2a0f..a1657e4d 100644 --- a/postgres/src/text_rows.rs +++ b/postgres/src/text_rows.rs @@ -9,6 +9,7 @@ use std::str; pub use postgres_shared::rows::RowIndex; use stmt::{Column}; +use {Result, error}; /// The resulting rows of a query. pub struct TextRows { @@ -145,25 +146,26 @@ impl<'a> TextRow<'a> { } /// stub - pub fn get(&self, idx: I) -> Option<&str> + pub fn get(&self, idx: I) -> &str where I: RowIndex + fmt::Debug, { match self.get_inner(&idx) { - Some(value) => value, + Some(Ok(value)) => value, + Some(Err(err)) => panic!("error retrieving column {:?}: {:?}", idx, err), None => panic!("no such column {:?}", idx), } } /// stub - pub fn get_opt(&self, idx: I) -> Option> + pub fn get_opt(&self, idx: I) -> Option> where I: RowIndex, { self.get_inner(&idx) } - fn get_inner(&self, idx: &I) -> Option> + fn get_inner(&self, idx: &I) -> Option> where I: RowIndex, { @@ -173,6 +175,6 @@ impl<'a> TextRow<'a> { }; self.data.get(idx) - .map(|s| str::from_utf8(s).ok()) + .map(|s| str::from_utf8(s).map_err(|e| error::conversion(Box::new(e)))) } }