io-extra

Crates.ioio-extra
lib.rsio-extra
version0.3.0
sourcesrc
created_at2023-12-07 23:55:17.402444
updated_at2024-04-12 12:57:02.439357
descriptionAn extension trait for `std::io::Error`, with shorthand constructors for various `std::io::ErrorKind`s.
homepagehttps://github.com/aatifsyed/io-extra
repositoryhttps://github.com/aatifsyed/io-extra
max_upload_size
id1061392
size8,950
Aatif Syed (aatifsyed)

documentation

https://docs.rs/io-extra

README

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
}
Commit count: 13

cargo fmt