Skip to content

Commit df84dd8

Browse files
committed
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.
1 parent e3a25ad commit df84dd8

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

postgres/src/query.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ impl<'a> Query<'a> {
2121
_p: PhantomData,
2222
}
2323
}
24+
25+
/// A convenience API which collects the resulting rows into a `Vec` and returns them.
26+
pub fn to_vec(self) -> Result<Vec<Row>, Error> {
27+
self.collect()
28+
}
2429
}
2530

2631
impl<'a> FallibleIterator for Query<'a> {

postgres/src/query_portal.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ impl<'a> QueryPortal<'a> {
2121
_p: PhantomData,
2222
}
2323
}
24+
25+
/// A convenience API which collects the resulting rows into a `Vec` and returns them.
26+
pub fn to_vec(self) -> Result<Vec<Row>, Error> {
27+
self.collect()
28+
}
2429
}
2530

2631
impl<'a> FallibleIterator for QueryPortal<'a> {

postgres/src/test.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use fallible_iterator::FallibleIterator;
21
use std::io::Read;
32
use tokio_postgres::types::Type;
43
use tokio_postgres::NoTls;
@@ -21,11 +20,7 @@ fn query_prepared() {
2120
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
2221

2322
let stmt = client.prepare("SELECT $1::TEXT").unwrap();
24-
let rows = client
25-
.query(&stmt, &[&"hello"])
26-
.unwrap()
27-
.collect::<Vec<_>>()
28-
.unwrap();
23+
let rows = client.query(&stmt, &[&"hello"]).unwrap().to_vec().unwrap();
2924
assert_eq!(rows.len(), 1);
3025
assert_eq!(rows[0].get::<_, &str>(0), "hello");
3126
}
@@ -37,7 +32,7 @@ fn query_unprepared() {
3732
let rows = client
3833
.query("SELECT $1::TEXT", &[&"hello"])
3934
.unwrap()
40-
.collect::<Vec<_>>()
35+
.to_vec()
4136
.unwrap();
4237
assert_eq!(rows.len(), 1);
4338
assert_eq!(rows[0].get::<_, &str>(0), "hello");
@@ -62,7 +57,7 @@ fn transaction_commit() {
6257
let rows = client
6358
.query("SELECT * FROM foo", &[])
6459
.unwrap()
65-
.collect::<Vec<_>>()
60+
.to_vec()
6661
.unwrap();
6762
assert_eq!(rows.len(), 1);
6863
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -87,7 +82,7 @@ fn transaction_rollback() {
8782
let rows = client
8883
.query("SELECT * FROM foo", &[])
8984
.unwrap()
90-
.collect::<Vec<_>>()
85+
.to_vec()
9186
.unwrap();
9287
assert_eq!(rows.len(), 0);
9388
}
@@ -111,7 +106,7 @@ fn transaction_drop() {
111106
let rows = client
112107
.query("SELECT * FROM foo", &[])
113108
.unwrap()
114-
.collect::<Vec<_>>()
109+
.to_vec()
115110
.unwrap();
116111
assert_eq!(rows.len(), 0);
117112
}
@@ -141,7 +136,7 @@ fn nested_transactions() {
141136
let rows = transaction
142137
.query("SELECT id FROM foo ORDER BY id", &[])
143138
.unwrap()
144-
.collect::<Vec<_>>()
139+
.to_vec()
145140
.unwrap();
146141
assert_eq!(rows.len(), 1);
147142
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -165,7 +160,7 @@ fn nested_transactions() {
165160
let rows = client
166161
.query("SELECT id FROM foo ORDER BY id", &[])
167162
.unwrap()
168-
.collect::<Vec<_>>()
163+
.to_vec()
169164
.unwrap();
170165
assert_eq!(rows.len(), 3);
171166
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -192,7 +187,7 @@ fn copy_in() {
192187
let rows = client
193188
.query("SELECT id, name FROM foo ORDER BY id", &[])
194189
.unwrap()
195-
.collect::<Vec<_>>()
190+
.to_vec()
196191
.unwrap();
197192

198193
assert_eq!(rows.len(), 2);
@@ -251,7 +246,7 @@ fn portal() {
251246
let rows = transaction
252247
.query_portal(&portal, 2)
253248
.unwrap()
254-
.collect::<Vec<_>>()
249+
.to_vec()
255250
.unwrap();
256251
assert_eq!(rows.len(), 2);
257252
assert_eq!(rows[0].get::<_, i32>(0), 1);
@@ -260,7 +255,7 @@ fn portal() {
260255
let rows = transaction
261256
.query_portal(&portal, 2)
262257
.unwrap()
263-
.collect::<Vec<_>>()
258+
.to_vec()
264259
.unwrap();
265260
assert_eq!(rows.len(), 1);
266261
assert_eq!(rows[0].get::<_, i32>(0), 3);

0 commit comments

Comments
 (0)