fix bug in parse_n

I initially wrote this for 10 elements in the octopi flashing grid
puzzle, which is why I had a 10 in there, even though I did have the
const N already. Here I rewrote it slightly, because with `try_into`
already doing a length check, mine was redundant anyway.
This commit is contained in:
Jan Christian Grünhage 2021-12-12 12:58:01 +01:00
parent c473ee4fc7
commit 49cfe7a7cb

View file

@ -42,12 +42,11 @@ pub fn parse_n<T: Debug, P: Tokens<Item = char>, const N: usize>(
parser: &dyn Fn(&mut P) -> Option<T>, parser: &dyn Fn(&mut P) -> Option<T>,
separator: &dyn Fn(&mut P) -> bool, separator: &dyn Fn(&mut P) -> bool,
) -> Option<[T; N]> { ) -> Option<[T; N]> {
let elements: Vec<T> = tokens.sep_by(parser, separator).collect(); tokens
if elements.len() != 10 { .sep_by(parser, separator)
None .collect::<Vec<T>>()
} else { .try_into()
Some(elements.try_into().unwrap()) .ok()
}
} }
#[cfg(test)] #[cfg(test)]