BurntSushi / BurntSushi/rust-csv

`write_byte_record` and `write_field` does not mix well and this is not properly documented.

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

Description

#### What version of the `csv` crate are you using?

1.2.2

#### Briefly describe the question, bug or feature request.

When I use `write_byte_record` with a non-empty iterator after prior series of `write_field`s, the library produces strange output and may fail with `UnequalLengths`.

#### Include a complete program demonstrating a problem.

```rust
fn main() -> Result<(), Box>{
let mut b = csv::WriterBuilder::new();
b.has_headers(false);
let mut csv = b.from_path("out.csv")?;
let mut record = csv::ByteRecord::new();
record.push_field(b"12");
for _ in 0..10000 {
csv.write_field("F")?;
csv.write_byte_record(&record)?;
}
Ok(())
}
```

I used similar code (with a `csv.write_field("")?` workaround to insert the missing comma), thinking that `write_record` is only for string records, not byte ones.

#### What is the observed behavior of the code above?

Output file contains records glued together (except of the last line before `UnequalLengths` error).

The documentation does not suggest, but also does not explicitly prohibit this combination of `csv::Writer` methods.

#### What is the expected or desired behavior of the code above?

`write_byte_record`'s documentation explicitly mentions that it `write_field` should not be used to prepend fields. Or the program behaves just like as if it were `write_record` instead.

Maybe usage of `write_byte_record` after `write_field` should panic, at least in debug profile.

---

Here are steps of how similar code can end up in a project:

1. Example code from `write_field`'s documentation with `wtr.write_record(None::<&[u8]>)?;` (why there is no dedicated method to avoid those turbofishes?);
2. `None::<&[u8]>` gets replaced with a record from other CSV file (e.g. to round trip with prepended fields).
3. Let's preserve non-UTF-8 content, so `ByteRecord` instead of `StringRecord`. As we have switched to byte records, assume (erroneously) that we need to switch to `write_byte_record` from `write_record`. It also mentions "more quickly" in the docs, which makes the switch even more attractive.

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.