| Crates.io | io-extra |
| lib.rs | io-extra |
| version | 0.3.0 |
| created_at | 2023-12-07 23:55:17.402444+00 |
| updated_at | 2024-04-12 12:57:02.439357+00 |
| description | An extension trait for `std::io::Error`, with shorthand constructors for various `std::io::ErrorKind`s. |
| homepage | https://github.com/aatifsyed/io-extra |
| repository | https://github.com/aatifsyed/io-extra |
| max_upload_size | |
| id | 1061392 |
| size | 8,950 |
An extension trait for [io::Error], with shorthand constructors for various
[io::ErrorKind]s, and a [context()] method.
use std::{fs::File, io::{self, Write as _}, str};
use io_extra::{IoErrorExt as _, context, with};
fn write_log(contents: &[u8], mut file: File) -> io::Result<()> {
if let Err(e) = str::from_utf8(contents) {
return Err(io::Error::invalid_input("`contents` was not UTF-8"))
// ^ shorthand constructor
}
file.write_all(contents).map_err(with("couldn't write file"))
// ^ easily add context
}