readable/
lib.rs

1use std::io::BufRead;
2
3use input::{Readable, Source};
4
5pub enum Usize1 {}
6pub enum Isize1 {}
7pub enum Chars {}
8pub enum Bytes {}
9
10impl Readable for Usize1 {
11    type Output = usize;
12    fn read<R: BufRead, S: Source<R>>(source: &mut S) -> usize {
13        usize::read(source).checked_sub(1).unwrap()
14    }
15}
16
17impl Readable for Isize1 {
18    type Output = isize;
19    fn read<R: BufRead, S: Source<R>>(source: &mut S) -> isize {
20        isize::read(source).checked_sub(1).unwrap()
21    }
22}
23
24impl Readable for Chars {
25    type Output = Vec<char>;
26    fn read<R: BufRead, S: Source<R>>(source: &mut S) -> Vec<char> {
27        source.next_token_unwrap().chars().collect()
28    }
29}
30
31impl Readable for Bytes {
32    type Output = Vec<u8>;
33    fn read<R: BufRead, S: Source<R>>(source: &mut S) -> Vec<u8> {
34        source.next_token_unwrap().bytes().collect()
35    }
36}
37
38#[test]
39fn sanity_check() {
40    use input::{scan, AutoSource};
41
42    let src = AutoSource::from("10 20 3 chars bytes");
43    scan! {
44        from src,
45        (l, r): (Usize1, usize),
46        y: Isize1,
47        c: Chars,
48        b: Bytes,
49    }
50
51    assert_eq!(l..r, 9..20);
52    assert_eq!(y, 2);
53    assert!("chars".chars().eq(c));
54    assert!("bytes".bytes().eq(b));
55}