BurntSushi / byteorder

Rust library for reading/writing numbers in big-endian and little-endian.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Avoid unwrap

tkaitchuck opened this issue · comments

Currently when writing to a vector it is nessicary to call .unwrap() frequently because the calls return Result but can't actually fail. An example copied from the byteorder documentation is here:

use byteorder::WriteBytesExt;

let mut wtr = Vec::new();
wtr.write_u8(2).unwrap();
wtr.write_u8(5).unwrap();
assert_eq!(wtr, b"\x02\x05");

This code would be a lot cleaner without those two unwrap calls. Could we add an additional trait that has write calls that specifically operates on Vec to avoid the need for the .unwrap()?

I'd rather not. The standard library has the same problem. If you use a Vec as a io::Write, then you have to go through the generic interface which returns a Result.

I think if the unwraps are that burdensome and you're writing a lot of them, then a few helper functions would be the best solution.