BurntSushi / BurntSushi/rust-csv

Newbie question: implementation of `_::_serde::Deserialize` is not general enough

Open
#229 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2k
Forks
257
PR merge metrics
No merged PRs in 30d

Description

Sorry for the Rust newbie question. I am trying to port existing working code to be zero-copy.

I have spent a fair amount adding/removing lifetimes, without success. Everything commented out I have already tried. The full error is at the end. Perhaps it is obvious stuff to a more Rust-savy person.

Any help appreciated.

```rust
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Trade<'a> {
pub account: &'a str,
#[serde(with = "my_date_format")]
pub date: DateTime,
pub r#type: TradeType,
pub stock: &'a str,
pub units: f64,
pub price: Option,
pub fees: Option,
pub split: f64,
pub currency: f64,
}

impl Store<'_> {
pub fn load_trades(&self) -> Result> {
self.load_csv(TRADES_FILE)
}
pub fn load_stocks(&self) -> Result> {
self.load_csv(STOCKS_FILE)
}

fn load_csv(&self, data: &str) -> Result>
where
//T: for<'de> Deserialize<'de>,
T: serde::de::DeserializeOwned,
{
let mut rdr = csv::ReaderBuilder::new()
.delimiter(b'\t')
.flexible(true)
.trim(csv::Trim::All)
.comment(Some(b'#'))
.from_reader(data.as_bytes());

/*
let raw_record = csv::StringRecord::new();
let headers = rdr.headers().chain_err(|| "Can't get headers?")?.clone();

let mut res = Vec::with_capacity(1024);
while rdr
.read_record(&mut raw_record)
.chain_err(|| "Csv not well formed")?
{
let record: T = raw_record
.deserialize(Some(&headers))
.chain_err(|| "Csv not well formed")?;
res.push(record);
}
Ok(res)
*/
rdr.deserialize()
.map(|r| r.chain_err(|| "Badly formatted csv."))
.collect::>>()
}
}
```
```console
error[E0277]: the trait bound `for<'de> T: _::_serde::Deserialize<'de>` is not satisfied
--> src/lib.rs:196:14
|
196 | .collect::>>()
| ^^^^^^^ the trait `for<'de> _::_serde::Deserialize<'de>` is not implemented for `T`
|
= note: required because of the requirements on the impl of `DeserializeOwned` for `T`
= note: required because of the requirements on the impl of `Iterator` for `DeserializeRecordsIter<'_, &[u8], T>`
= note: 1 redundant requirements hidden
= note: required because of the requirements on the impl of `Iterator` for `std::iter::Map, [closure@src/lib.rs:195:18: 195:60]>`
help: consider further restricting this bound
|
168 | T: Deserialize<'b> + for<'de> _::_serde::Deserialize<'de>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.