#adapter #api #io

io-adapters

Adapters to convert between different writable APIs

4 releases (breaking)

0.4.0 Oct 26, 2024
0.3.0 Dec 10, 2023
0.2.0 Dec 9, 2023
0.1.0 Dec 6, 2023
0.0.0 Dec 6, 2023

#2858 in Rust patterns

Download history 11/week @ 2025-12-24 8/week @ 2025-12-31 2/week @ 2026-01-07 11/week @ 2026-01-14 16/week @ 2026-01-21 17/week @ 2026-01-28 11/week @ 2026-02-04 18/week @ 2026-02-18 2/week @ 2026-02-25 9/week @ 2026-03-04 21/week @ 2026-03-11 11/week @ 2026-03-18 10/week @ 2026-03-25 48/week @ 2026-04-01 7/week @ 2026-04-08

77 downloads per month
Used in 4 crates

Apache-2.0

9KB
60 lines

I/O adapters

This crate provides adapters to compose writeable traits in the standard library. The following conversions are available:

  • fmt::Write -> io::Write
  • io::Write -> hash::Hasher

Use case

Suppose 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());

No runtime deps