| Crates.io | io-adapters |
| lib.rs | io-adapters |
| version | 0.4.0 |
| created_at | 2023-12-06 23:47:10.143143+00 |
| updated_at | 2024-10-26 14:33:14.698345+00 |
| description | Adapters to convert between different writable APIs. |
| homepage | |
| repository | https://github.com/SUPERCILEX/io-adapters |
| max_upload_size | |
| id | 1060532 |
| size | 26,203 |
This crate provides adapters to compose writeable traits in the standard library. The following conversions are available:
fmt::Write -> io::Writeio::Write -> hash::HasherSuppose you are writing a function which emits human-readable data in a zero-alloc way. The best interface looks something like this:
fn foo<Out: fmt::Write>(mut output: Out, ...) {
// Do stuff
writeln!(output, "My computation: {result}").unwrap();
}
Notice the use of fmt::Write: using this trait provides a type-system guarantee that the data
written is valid UTF-8, hence why it should be preferred over io::Write.
Now users of this API can gather data into a String, provide their own fmt::Write
implementation, etc. The problem you'll run into is if you'd like to send the output of this
function to stdout: there is no built-in way to do so! That's where this crate comes in by providing
an adapter, so you can write the following:
foo(&mut io::stdout().write_adapter());