diff --git a/rustfmt.toml b/rustfmt.toml index 63b0868..04e249e 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,11 +1,13 @@ +#binop_separator= "Back" +#brace_style="SameLineWhere" +#force_multiline_blocks = true +#imports_layout = "Vertical" array_width = 0 -binop_separator= "Back" -brace_style="SameLineWhere" -chain_width = 240 +attr_fn_like_width = 80 +chain_width = 144 fn_params_layout = "Tall" force_explicit_abi = true hard_tabs = false -imports_layout = "Vertical" match_block_trailing_comma = true max_width = 240 merge_derives = true @@ -14,4 +16,8 @@ remove_nested_parens = true reorder_imports = true reorder_modules = true single_line_if_else_max_width = 0 +single_line_let_else_max_width = 0 tab_spaces = 4 +use_field_init_shorthand = true +use_small_heuristics = "Off" +use_try_shorthand = true diff --git a/src/coordinate_systems/kartesian/direction.rs b/src/coordinate_systems/kartesian/direction.rs index b504448..f2be107 100644 --- a/src/coordinate_systems/kartesian/direction.rs +++ b/src/coordinate_systems/kartesian/direction.rs @@ -1,5 +1,7 @@ use std::fmt::Display; +use crate::enum_alias; + use super::{Kartesian, KartesianIterator}; use num::*; @@ -17,6 +19,20 @@ pub enum KartesianDirection { BottomRight, } +impl KartesianDirection { + enum_alias!(KartesianDirection, NorthWest, TopLeft); + enum_alias!(KartesianDirection, North, Top); + enum_alias!(KartesianDirection, Up, Top); + enum_alias!(KartesianDirection, NorthEast, TopRight); + enum_alias!(KartesianDirection, West, Left); + enum_alias!(KartesianDirection, Center, None); + enum_alias!(KartesianDirection, East, Right); + enum_alias!(KartesianDirection, SouthWest, BottomLeft); + enum_alias!(KartesianDirection, South, Bottom); + enum_alias!(KartesianDirection, Down, Bottom); + enum_alias!(KartesianDirection, SouthEast, BottomLeft); +} + impl KartesianDirection { pub fn vector(self) -> Kartesian { Kartesian { diff --git a/src/coordinate_systems/kartesian/maximum.rs b/src/coordinate_systems/kartesian/maximum.rs index 16b7162..7cfc71b 100644 --- a/src/coordinate_systems/kartesian/maximum.rs +++ b/src/coordinate_systems/kartesian/maximum.rs @@ -5,32 +5,23 @@ pub trait MaximumFromMap { fn maximum(map: &Vec>) -> Kartesian; } -impl MaximumFromMap for Kartesian { - fn maximum(map: &Vec>) -> Kartesian { - Kartesian { - x: map.len().to_u32().unwrap(), - y: map[0].len().to_u32().unwrap(), +macro_rules! impl_maximum { + ($type:ident) => { + impl MaximumFromMap<$type> for Kartesian<$type> { + fn maximum(map: &Vec>) -> Kartesian<$type> { + Kartesian { + x: map.len() as $type, + y: map[0].len() as $type, + } + } } - } -} - -impl MaximumFromMap for Kartesian { - fn maximum(map: &Vec>) -> Kartesian { - Kartesian { - x: map.len().to_u64().unwrap(), - y: map[0].len().to_u64().unwrap(), - } - } -} - -impl MaximumFromMap for Kartesian { - fn maximum(map: &Vec>) -> Kartesian { - Kartesian { - x: map.len().to_u128().unwrap(), - y: map[0].len().to_u128().unwrap(), - } - } + }; } +impl_maximum!(u8); +impl_maximum!(u16); +impl_maximum!(u32); +impl_maximum!(u64); +impl_maximum!(u128); impl MaximumFromMap for Kartesian { fn maximum(map: &Vec>) -> Kartesian { diff --git a/src/coordinate_systems/kartesian/mod.rs b/src/coordinate_systems/kartesian/mod.rs index aaa66f8..cd1ac38 100644 --- a/src/coordinate_systems/kartesian/mod.rs +++ b/src/coordinate_systems/kartesian/mod.rs @@ -18,10 +18,6 @@ pub use traits::*; pub type KD = KartesianDirection; -pub trait ToCoords: Copy { - fn to_coords(self) -> Kartesian; -} - #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Kartesian where @@ -37,42 +33,6 @@ impl Kartesian { } } -impl ToCoords for Kartesian { - fn to_coords(self) -> Kartesian { - Kartesian { x: self.x as usize, y: self.y as usize } - } -} - -impl ToCoords for Kartesian { - fn to_coords(self) -> Kartesian { - Kartesian { x: self.x as usize, y: self.y as usize } - } -} - -impl ToCoords for Kartesian { - fn to_coords(self) -> Kartesian { - Kartesian { x: self.x as usize, y: self.y as usize } - } -} - -impl ToCoords for Kartesian { - fn to_coords(self) -> Kartesian { - Kartesian { x: self.x as usize, y: self.y as usize } - } -} - -impl ToCoords for Kartesian { - fn to_coords(self) -> Kartesian { - Kartesian { x: self.x as usize, y: self.y as usize } - } -} - -impl ToCoords for Kartesian { - fn to_coords(self) -> Kartesian { - Kartesian { x: self.x, y: self.y } - } -} - fn wrap + Copy + MaximumValue + Display, const FACTOR: u32>(v: T, max: T) -> T { if v < max { v diff --git a/src/coordinate_systems/kartesian/traits.rs b/src/coordinate_systems/kartesian/traits.rs index 80f6b58..c99967f 100644 --- a/src/coordinate_systems/kartesian/traits.rs +++ b/src/coordinate_systems/kartesian/traits.rs @@ -1,9 +1,15 @@ use num::*; -macro_rules! predefined_const { - ($trait_name:ident, $const:ident, $t:ty) => { - impl $trait_name for $t { - const $const: $t = <$t>::$const; +use crate::predefined_const; + +use super::Kartesian; + +macro_rules! to_coords_impl { + ($subtype:ident) => { + impl ToCoords for Kartesian<$subtype> { + fn to_coords(self) -> Kartesian { + Kartesian { x: self.x as usize, y: self.y as usize } + } } }; } @@ -41,3 +47,19 @@ predefined_const!(MinimumValue, MIN, i32); predefined_const!(MinimumValue, MIN, i64); predefined_const!(MinimumValue, MIN, i128); predefined_const!(MinimumValue, MIN, isize); + +pub trait ToCoords: Copy { + fn to_coords(self) -> Kartesian; +} + +to_coords_impl!(u8); +to_coords_impl!(u16); +to_coords_impl!(u32); +to_coords_impl!(u64); +to_coords_impl!(u128); + +impl ToCoords for Kartesian { + fn to_coords(self) -> Kartesian { + Kartesian { x: self.x, y: self.y } + } +} diff --git a/src/lib.rs b/src/lib.rs index 5bb743d..287a6c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,3 +65,24 @@ pub enum ExtendedOption { Some(T), None, } + +#[macro_export] +macro_rules! enum_alias { + ($enum:ident, $alias:ident, $target:ident) => { + #[allow(non_upper_case_globals)] + pub const $alias: $enum = <$enum>::$target; + }; + ($visibility:vis $enum:ident, $alias:ident, $target:ident) => { + #[allow(non_upper_case_globals)] + $visibility const $alias: $enum = <$enum>::$target; + }; +} + +#[macro_export] +macro_rules! predefined_const { + ($trait_name:ident, $const:ident, $t:ty) => { + impl $trait_name for $t { + const $const: $t = <$t>::$const; + } + }; +}