Add convenience to_vec methods to fallible iterators

The very common case is to simply collect these to a vector, and this
lets people avoid having to import FallibleIterator.
This commit is contained in:
Steven Fackler
2019-01-29 19:29:33 -08:00
parent e3a25ad6c8
commit df84dd8fd0
3 changed files with 20 additions and 15 deletions

View File

@@ -21,6 +21,11 @@ impl<'a> Query<'a> {
_p: PhantomData,
}
}
/// A convenience API which collects the resulting rows into a `Vec` and returns them.
pub fn to_vec(self) -> Result<Vec<Row>, Error> {
self.collect()
}
}
impl<'a> FallibleIterator for Query<'a> {

View File

@@ -21,6 +21,11 @@ impl<'a> QueryPortal<'a> {
_p: PhantomData,
}
}
/// A convenience API which collects the resulting rows into a `Vec` and returns them.
pub fn to_vec(self) -> Result<Vec<Row>, Error> {
self.collect()
}
}
impl<'a> FallibleIterator for QueryPortal<'a> {

View File

@@ -1,4 +1,3 @@
use fallible_iterator::FallibleIterator;
use std::io::Read;
use tokio_postgres::types::Type;
use tokio_postgres::NoTls;
@@ -21,11 +20,7 @@ fn query_prepared() {
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
let stmt = client.prepare("SELECT $1::TEXT").unwrap();
let rows = client
.query(&stmt, &[&"hello"])
.unwrap()
.collect::<Vec<_>>()
.unwrap();
let rows = client.query(&stmt, &[&"hello"]).unwrap().to_vec().unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, &str>(0), "hello");
}
@@ -37,7 +32,7 @@ fn query_unprepared() {
let rows = client
.query("SELECT $1::TEXT", &[&"hello"])
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, &str>(0), "hello");
@@ -62,7 +57,7 @@ fn transaction_commit() {
let rows = client
.query("SELECT * FROM foo", &[])
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -87,7 +82,7 @@ fn transaction_rollback() {
let rows = client
.query("SELECT * FROM foo", &[])
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 0);
}
@@ -111,7 +106,7 @@ fn transaction_drop() {
let rows = client
.query("SELECT * FROM foo", &[])
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 0);
}
@@ -141,7 +136,7 @@ fn nested_transactions() {
let rows = transaction
.query("SELECT id FROM foo ORDER BY id", &[])
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -165,7 +160,7 @@ fn nested_transactions() {
let rows = client
.query("SELECT id FROM foo ORDER BY id", &[])
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 3);
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -192,7 +187,7 @@ fn copy_in() {
let rows = client
.query("SELECT id, name FROM foo ORDER BY id", &[])
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 2);
@@ -251,7 +246,7 @@ fn portal() {
let rows = transaction
.query_portal(&portal, 2)
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 2);
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -260,7 +255,7 @@ fn portal() {
let rows = transaction
.query_portal(&portal, 2)
.unwrap()
.collect::<Vec<_>>()
.to_vec()
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 3);