mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Use boxed closures to allow query_map to take f: F instead of f: &F
This commit is contained in:
parent
95f511d437
commit
3f75300844
13
src/lib.rs
13
src/lib.rs
@ -636,10 +636,10 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
Ok(SqliteRows::new(self))
|
Ok(SqliteRows::new(self))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn query_map<'a, 'map, T, F>(&'a mut self, params: &[&ToSql], f: &'map F)
|
pub fn query_map<'a, 'map, T, F>(&'a mut self, params: &[&ToSql], f: F)
|
||||||
-> SqliteResult<MappedRows<'a, 'map, T>>
|
-> SqliteResult<MappedRows<'a, 'map, T>>
|
||||||
where T: 'static,
|
where T: 'static,
|
||||||
F: Fn(MappedRow) -> T {
|
F: 'map + Fn(MappedRow) -> T {
|
||||||
self.reset_if_needed();
|
self.reset_if_needed();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -648,7 +648,7 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
|
|
||||||
Ok(MappedRows{
|
Ok(MappedRows{
|
||||||
stmt: self,
|
stmt: self,
|
||||||
map: f
|
map: Box::new(f),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -704,7 +704,7 @@ impl<'conn> Drop for SqliteStatement<'conn> {
|
|||||||
|
|
||||||
pub struct MappedRows<'stmt, 'map, T> {
|
pub struct MappedRows<'stmt, 'map, T> {
|
||||||
stmt: &'stmt SqliteStatement<'stmt>,
|
stmt: &'stmt SqliteStatement<'stmt>,
|
||||||
map: &'map Fn(MappedRow) -> T
|
map: Box<Fn(MappedRow) -> T + 'map>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'stmt, 'map, T: 'static> Iterator for MappedRows<'stmt, 'map, T> {
|
impl<'stmt, 'map, T: 'static> Iterator for MappedRows<'stmt, 'map, T> {
|
||||||
@ -1021,7 +1021,10 @@ mod test {
|
|||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||||
let results: SqliteResult<Vec<String>> = query.query_map(&[], &(|row| row.get(1))).unwrap().collect();
|
let results: SqliteResult<Vec<String>> = query
|
||||||
|
.query_map(&[], |row| row.get(1))
|
||||||
|
.unwrap()
|
||||||
|
.collect();
|
||||||
|
|
||||||
assert_eq!(results.unwrap().concat(), "hello, world!");
|
assert_eq!(results.unwrap().concat(), "hello, world!");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user