use std::fmt;
use std::iter;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Color {
Black,
White,
}
impl Color {
pub fn iter() -> ColorIter {
ColorIter {
current: Some(Color::Black),
}
}
#[must_use]
pub fn flip(self) -> Self {
match self {
Color::Black => Color::White,
Color::White => Color::Black,
}
}
#[inline(always)]
pub fn index(self) -> usize {
self as usize
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
Color::Black => write!(f, "Black"),
Color::White => write!(f, "White"),
}
}
}
pub struct ColorIter {
current: Option<Color>,
}
impl iter::Iterator for ColorIter {
type Item = Color;
fn next(&mut self) -> Option<Self::Item> {
let current = self.current;
if let Some(current) = self.current {
self.current = match current {
Color::Black => Some(Color::White),
Color::White => None,
}
}
current
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flip() {
assert_eq!(Color::White, Color::Black.flip());
assert_eq!(Color::Black, Color::White.flip());
}
}