hext_boards

Struct HexagonalBoard

Source
pub struct HexagonalBoard<T> { /* private fields */ }
Expand description

A board composed of hexagons.

You can construct a HexagonalBoard the same ways you would construct a HashMap, where the keys are something that can be converted to a glam::IVec2 (e.g., [i32; 2]).

The positions are interpreted as hexagonal coordinates, where the basis [1, 0] is the hexagon to the left and up, and [0, 1] is the one to the right and up.

You can render a HexagonalBoard using HexagonalBoard::render if T: Into<char> + Clone. Otherwise, you can use HexagonalBoard::render_with to specify how to convert the T into a char.

You can also use HexagonalBoard::char_map if you want to easily get what character should be printed where, but you want to do the rendering yourself.

§Examples

Basic usage:

use hext_boards::HexagonalBoard;

// Put an `'A'` at `[0, 0]` and a `'B'` at `[1, 1]`.
let board = HexagonalBoard::from([
    ([0, 0], 'A'),
    ([1, 1], 'B'),
]);

let output = board.render();
let expected = indoc::indoc!(
    r"
         /---\
        ⟨  B  ⟩
         ⟩---⟨
        ⟨  A  ⟩
         \---/
    "
).trim_end_matches('\n');

println!("{output}");

assert_eq!(output, expected)

Using HexagonalBoard::render_with:

use hext_boards::HexagonalBoard;

let board = HexagonalBoard::from([
    ([0, 0], 5),
    ([0, 1], 13),
    ([1, 1], 25),
]);

// Everything needs to be one char, so we have to use hexadecimal or some other radix to
// output higher numbers.
let output = board.render_with(|n| char::from_digit(*n, 36).expect("`n` is less than 36."));

let expected = indoc::indoc!(
    r"
         /---\
        ⟨  p  ⟩---\
         ⟩---⟨  d  ⟩
        ⟨  5  ⟩---/
         \---/
    "
).trim_end_matches('\n');

assert_eq!(output, expected)

Implementations§

Source§

impl<T> HexagonalBoard<T>

Source

pub fn char_map(&self, into_char: impl Fn(&T) -> char) -> HashMap<IVec2, char>

Map from coordinates

Source

pub fn render_with(&self, into_char: impl Fn(&T) -> char) -> String

Renders the hexagonal board into a String, converting the Ts into chars using into_char.

If T can easily be converted to a char (i.e., T: Into<char> + Copy), you can use Self::render.

Source

pub fn render(&self) -> String
where for<'a> char: From<T>, T: Copy,

Renders the hexagonal board into a String.

To specify how to convert the T into a char, see Self::render_with.

Trait Implementations§

Source§

impl<T: Clone> Clone for HexagonalBoard<T>

Source§

fn clone(&self) -> HexagonalBoard<T>

Returns a copy 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<T: Debug> Debug for HexagonalBoard<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for HexagonalBoard<T>

Source§

fn default() -> HexagonalBoard<T>

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

impl<T> Display for HexagonalBoard<T>
where for<'a> char: From<T>, T: Copy,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<P: Into<IVec2>, T, V> From<V> for HexagonalBoard<T>
where V: IntoIterator<Item = (P, T)>,

Source§

fn from(value: V) -> Self

Converts to this type from the input type.
Source§

impl<P: Into<IVec2>, T> FromIterator<(P, T)> for HexagonalBoard<T>

Source§

fn from_iter<I: IntoIterator<Item = (P, T)>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for HexagonalBoard<T>

§

impl<T> RefUnwindSafe for HexagonalBoard<T>
where T: RefUnwindSafe,

§

impl<T> Send for HexagonalBoard<T>
where T: Send,

§

impl<T> Sync for HexagonalBoard<T>
where T: Sync,

§

impl<T> Unpin for HexagonalBoard<T>
where T: Unpin,

§

impl<T> UnwindSafe for HexagonalBoard<T>
where T: UnwindSafe,

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.