Here's a simplified version of what I'm trying to do:
type Items = Vec<Item>;
fn read_items(rec_ids: &[i32]) -> Result<Items> {
let mut db = Database::open()?;
let mut items = vec![];
items.par_extend(rec_ids.par_iter().filter_map(|rec_id| {
if db.set_record(*rec_id) == true {
Some(db.read_items(*rec_id)) // Vec<Item>
} else {
None
}
}));
Ok(items.into_par_iter().flatten().collect())
}
The borrow checker is telling me "cannot borrow db
as mutable, as it is a captured variable in a Fn
closure" and that I should use FnMut
. I need db
to be mutable because set_record
changes an internal database cursor. But I don't know how to change this to FnMut
.
Also I doubt that the way I'm doing this is the best way. I'm trying to iterate over a selected bunch of records and for each record I gather a vector of items, the aim being to end up with a single flat vector of all the items from all the records in the rec_ids
list.