Color

Enum Color 

Source
#[repr(u8)]
pub enum Color { White = 0, Black = 1, }
Expand description

Represents the color of a player, piece, square, etc. within a chess board.

In Western chess, White traditionally moves first, and therefore Color defaults to Color::White.

Variants§

§

White = 0

§

Black = 1

Implementations§

Source§

impl Color

Source

pub const COUNT: usize = 2usize

Number of color variants.

Source

pub const fn all() -> [Color; 2]

An array of both colors, starting with White.

Source

pub fn iter() -> impl Iterator<Item = Color>

An iterator over both colors, starting with White.

Source

pub fn from_bits(bits: u8) -> Result<Color, Error>

Creates a new Color from a set of bits.

bits must be [0,1].

§Panics

If bits is greater than 1.

§Example
let white = Color::from_bits(0);
assert!(white.is_ok());
assert_eq!(white.unwrap(), Color::White);

let err = Color::from_bits(42);
assert!(err.is_err());
Source

pub const fn from_bits_unchecked(bits: u8) -> Color

Creates a new Color from a set of bits, ignoring safety checks.

bits must be [0,1].

§Panics

If bits is greater than 1 and debug assertions are enabled.

§Example
let white = Color::from_bits(0);
assert!(white.is_ok());
assert_eq!(white.unwrap(), Color::White);

let err = Color::from_bits(42);
assert!(err.is_err());
Source

pub const fn from_bool(color: bool) -> Color

Creates a new Color from a bool, where false = White.

§Example
let white = Color::from_bool(false);
assert_eq!(white, Color::White);

let black = Color::from_bool(true);
assert_eq!(black, Color::Black);
Source

pub const fn is_white(&self) -> bool

Returns true if this Color is White.

Source

pub const fn is_black(&self) -> bool

Returns true if this Color is Black.

Source

pub const fn negation_multiplier(&self) -> i8

Returns a multiplier for negating numbers relative to this color.

§Example
assert_eq!(Color::White.negation_multiplier(), 1);
assert_eq!(Color::Black.negation_multiplier(), -1);
Source

pub const fn opponent(&self) -> Color

Returns this Color’s opposite / inverse / enemy.

§Example
assert_eq!(Color::White.opponent(), Color::Black);
assert_eq!(Color::Black.opponent(), Color::White);
Source

pub const fn index(&self) -> usize

Returns this Color as a usize.

Will be 0 for White, 1 for Black.

Useful for indexing into lists.

§Example
assert_eq!(Color::White.index(), 0);
assert_eq!(Color::Black.index(), 1);
Source

pub const fn bits(&self) -> u8

Returns this Color as a u8.

Will be 0 for White, 1 for Black.

Useful for bit twiddling.

§Example
assert_eq!(Color::White.bits(), 0);
assert_eq!(Color::Black.bits(), 1);
Source

pub fn from_uci(color: char) -> Result<Color, Error>

Creates a Color from a char, according to the Universal Chess Interface notation.

§Example
let white = Color::from_uci('w');
assert!(white.is_ok());
assert_eq!(white.unwrap(), Color::White);

let err = Color::from_uci('x');
assert!(err.is_err());
Source

pub const fn from_case(c: char) -> Color

Creates a Color based on the ASCII case of the provided character, with uppercase being White and lowercase being Black.

Note this is intended to follow the Universal Chess Interface notation, but can be used in odd ways, such as trying to find the color of the char 'z' (Black).

§Example
assert_eq!(Color::from_case('k'), Color::Black);
Source

pub const fn to_uci(&self) -> char

Converts this Color to a char, according to the Universal Chess Interface notation.

§Example
assert_eq!(Color::White.to_uci(), 'w');
Source

pub const fn as_str(&self) -> &'static str

Converts this Color to a str, according to the Universal Chess Interface notation.

§Example
assert_eq!(Color::White.as_str(), "w");
Source

pub const fn name(&self) -> &'static str

Fetches a human-readable name for this Color.

§Example
let white = Color::White;
assert_eq!(white.name(), "white");

Trait Implementations§

Source§

impl AsRef<str> for Color

Source§

fn as_ref(&self) -> &str

Alias for Self::as_str.

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Debug formatting displays a $type as its human-readable name and index value.

Source§

impl Default for Color

Source§

fn default() -> Color

Returns the “default value” for a type. Read more
Source§

impl Display for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

By default, a $type displays as a lowercase char.

Source§

impl FromStr for Color

Source§

fn from_str(s: &str) -> Result<Color, <Color as FromStr>::Err>

Does the same as Self::from_uci, but only if s is one character in length.

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

impl Hash for Color

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<Color> for Board

Source§

type Output = Bitboard

The returned type after indexing.
Source§

fn index(&self, index: Color) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<Color> for Board

Source§

fn index_mut(&mut self, index: Color) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Neg for Color

Source§

fn neg(self) -> <Color as Neg>::Output

Negating Color::White yields Color::Black and vice versa.

Source§

type Output = Color

The resulting type after applying the - operator.
Source§

impl Ord for Color

Source§

fn cmp(&self, other: &Color) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Color

Source§

fn partial_cmp(&self, other: &Color) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Color

Source§

impl Eq for Color

Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.