Trying to make a shorthand function to replace some lines of code.
Instead of this:
let csvr = csv::Reader::from_path(my_path)?;
for item in csvr.deserialize() {
let item = item?;
// useful job
}
I want to remove the first lines (and imports) into one func placed in utility module:
pub fn read_from_file<'de, D: Deserialize<'de>>(path: &str) -> Result<DeserializeRecordsIter<'de, File, D>, Box<dyn Error>> {
let csvr: Reader<File> = csv::Reader::from_path(path)?;
Ok(csvr.deserialize())
}
for item: MyType in read_from_file(&my_path)? {
let item = item?;
// useful job
}
(It doesn't save much code, but saves me from importing pieces of serde and csv and adding them to every Cargo.toml
, which is annoying.)
I get a new sort of error message (did I miss something in nightly builds?)
error[E0277]: the trait bound `for<'de> D: serde::Deserialize<'de>` is not satisfied
--> src/main.rs:9:10
|
9 | Ok(csvr.deserialize())
| ^^^^^^^^^^^ the trait `for<'de> serde::Deserialize<'de>` is not implemented for `D`
|
= note: required for `D` to implement `DeserializeOwned`
note: required by a bound in `Reader::<R>::deserialize`
--> /home/culebron/.cargo/registry/src/github.com-1ecc6299db9ec823/csv-1.2.1/src/reader.rs:1050:12
|
1050 | D: DeserializeOwned,
| ^^^^^^^^^^^^^^^^ required by this bound in `Reader::<R>::deserialize`
help: consider further restricting this bound
|
7 | pub fn read_from_file<'de, D: Deserialize<'de> + for<'de> serde::Deserialize<'de>>(path: &str) -> Result<DeserializeRecordsIter<'de, File, D>, Box<dyn Error>> {
| ++++++++++++++++++++++++++++++++++
When I add this for<'de> serde... etc.
, I get this same message again and again.