udoprog.github.io

Advent of Rust Day 4 - Testing things that read stuff

This is a series where I’ll be discussing interesting Rust tidbits that I encounter when solving Advent of Code 2017.

You can find the complete (spoiler) solution here: udoprog/rust-advent-of-code-2017

If you want to test a function that reads stuff, you can do the following:

use std::io::Read;

fn my_fun<R: Read>(reader: R) {
    /*  */
}

This allows you to write tests like these:

#[test]
fn test_with_cursor() {
    use std::io::Cursor;
    my_fun(Cursor::new("hello\nworld"));
}

If you don’t like monomorphization (large binaries?), use a trait object instead:

use std::io::Read;

fn my_fun(reader: &mut Read) {
    /*  */
}

But pay the price of dynamic dispatch.