Skip to content

Commit

Permalink
Get rid of Qr::new
Browse files Browse the repository at this point in the history
It's useless in const context because we have all possible instances
as conts already.

Also: improve iter() documentation and make Qr::NAMES pub.
  • Loading branch information
lpenz committed Aug 30, 2021
1 parent 2916ce9 commit b96ce24
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
16 changes: 4 additions & 12 deletions src/_sqrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,8 @@ impl Qr {
/// [`Qr`].
const INVERSE: [usize; 9] = [7, 0, 1, 6, usize::MAX, 2, 5, 4, 3];

/// Create a new [`Qr`] instance.
/// Can be used in const context.
/// Bounds are checked at compile-time, if possible.
pub const fn new<const DX: i16, const DY: i16>() -> Self {
// Trick for compile-time check of X and Y:
const ASSERT_FALSE: [(); 1] = [(); 1];
let _ =
ASSERT_FALSE[(DX < -1 || DX > 1 || DY < -1 || DY > 1 || (DX == 0 && DY == 0)) as usize];
Self { dx: DX, dy: DY }
}

/// The names of all corresponding values
const NAMES: [&'static str; 8] = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
pub const NAMES: [&'static str; 8] = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];

/// Returns true if the Qr is a diagonal: NE, SE, SW or NW.
pub const fn is_diagonal(&self) -> bool {
Expand All @@ -252,6 +241,9 @@ impl Qr {

/// Returns an iterator that returns all possible values for the
/// [`Qr`] type used, in clockwise order.
///
/// This function takes a generic const argument `D` that
/// indicates if diagonals should be in the iteration or not.
pub fn iter<const D: bool>() -> QrIterator<D> {
QrIterator::<D>::default()
}
Expand Down
11 changes: 0 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,6 @@
//! Ok(())
//! }
//! ```
//! - Calling [`Qr::new`], which checks the bounds in const contexts:
//! ```rust
//! use sqrid::Qr;
//! const DOWNRIGHT : Qr = Qr::new::<1, 1>();
//! ```
//! The following, for instance, doesn't compile due to the
//! arguments not being units:
//! ```compile_fail
//! use sqrid::Qr;
//! const DOWNRIGHT2 : Qr = Qr::new::<2, 2>();
//! ```
//! - Calling [`Qr::iter`] to iterate all directions:
//! ```rust
//! for qr in sqrid::Qr::iter::<true>() {
Expand Down
6 changes: 2 additions & 4 deletions tests/qr_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ use std::convert::TryFrom;
fn test_basic() -> Result<()> {
let qr0 = Qr::default();
let qr1 = Qr::N;
let qr2 = Qr::new::<0, -1>();
assert_eq!(qr0, qr1);
assert_eq!(qr0, qr2);
assert_eq!(<(i16, i16)>::from(qr1), (0, -1));
let qr3 = Qr::try_from((-1_i16, 0_i16));
assert_eq!(qr3, Ok(Qr::W));
let qr2 = Qr::try_from((-1_i16, 0_i16));
assert_eq!(qr2, Ok(Qr::W));
Ok(())
}

Expand Down

0 comments on commit b96ce24

Please sign in to comment.