From b96ce245935ed2ae9ba4d251a3941199a2b44268 Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Mon, 30 Aug 2021 21:29:24 +0100 Subject: [PATCH] Get rid of Qr::new It's useless in const context because we have all possible instances as conts already. Also: improve iter() documentation and make Qr::NAMES pub. --- src/_sqrid.rs | 16 ++++------------ src/lib.rs | 11 ----------- tests/qr_tests.rs | 6 ++---- 3 files changed, 6 insertions(+), 27 deletions(-) diff --git a/src/_sqrid.rs b/src/_sqrid.rs index 50a32b9..34127b8 100644 --- a/src/_sqrid.rs +++ b/src/_sqrid.rs @@ -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() -> 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 { @@ -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() -> QrIterator { QrIterator::::default() } diff --git a/src/lib.rs b/src/lib.rs index b981f67..30fc9c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::() { diff --git a/tests/qr_tests.rs b/tests/qr_tests.rs index db2573a..6f59627 100644 --- a/tests/qr_tests.rs +++ b/tests/qr_tests.rs @@ -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(()) }