statiolake / proconio-rs

Apache License 2.0
126 stars 7 forks source link

Allow values for marker types #48

Closed MiSawa closed 7 months ago

MiSawa commented 7 months ago

It'd be nice if we can specify a value (not a type) for the type part of the macro. This allows us to perform parameterized read+conversion, for example

struct DirectedGraph(usize, usize);

impl ReadWith for DirectedGraph {
    type Output = Vec<Vec<usize>>;

    fn read<R: BufRead, S: Source<R>>(&self, source: &mut S) -> Self::Output {
        input! {
            from source,
            edges: [(Usize1, Usize1); self.m],
        };
        let mut g = vec![vec![]; self.n];
        for e in edges {
            g[e.0].push(e.1);
        }
        e
    }
}

input! {
    n: usize,
    m: usize,
    some_random_values: [i32; n],
    g: with DirectedGraph(n, m),
}

. Here, with distinguishes if input! should treat it as a type or a value.

MiSawa commented 7 months ago

Awesome, thank you!!!