From 548f801bcfc4fa1b15ecfc9306fcc33144560f32 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 3 Feb 2024 12:39:28 -0400 Subject: [PATCH] Extract numeric type traits to own module --- src/main.rs | 104 +++---------------------------------------- src/numeric_types.rs | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 99 deletions(-) create mode 100644 src/numeric_types.rs diff --git a/src/main.rs b/src/main.rs index 402e929..62a38eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,14 +4,13 @@ use { image::{ImageBuffer, Luma}, las::Read as LasRead, proj::Proj, - std::{ - ops::{Add, Div, Mul, Sub}, - path::PathBuf, - }, + std::path::PathBuf, tokio::io::AsyncReadExt, }; mod geonb; +mod numeric_types; +use numeric_types::{Float, Integer, ToFloat}; #[derive(Parser)] #[command(author, version, about, long_about=None)] @@ -46,108 +45,15 @@ enum GeoNBCommand { /// Download the LIDAR tile that includes a location DownloadLidarTile { /// Latitude to fetch LIDAR tile at - #[arg(long, allow_hyphen_values=true)] + #[arg(long, allow_hyphen_values = true)] latitude: f64, /// Longitude to fetch LIDAR tile at - #[arg(long, allow_hyphen_values=true)] + #[arg(long, allow_hyphen_values = true)] longitude: f64, }, } -trait FromFloatFloor { - fn from_float_floor(f: F) -> Self; -} - -trait ToFloat { - fn to_float(self) -> F; -} - -trait Integer: - Copy - + Default - + Add - + Sub - + Mul - + Div - + FromFloatFloor - + FromFloatFloor - + ToFloat - + ToFloat -{ - const MIN: Self; - const MAX: Self; -} - -macro_rules! impl_int { - ( $t:ident ) => { - impl FromFloatFloor for $t { - fn from_float_floor(f: f32) -> Self { - f as $t - } - } - impl FromFloatFloor for $t { - fn from_float_floor(f: f64) -> Self { - f as $t - } - } - impl ToFloat for $t { - fn to_float(self) -> f32 { - self as f32 - } - } - impl ToFloat for $t { - fn to_float(self) -> f64 { - self as f64 - } - } - impl Integer for $t { - const MIN: $t = $t::MIN; - const MAX: $t = $t::MAX; - } - }; -} - -impl_int!(u8); -impl_int!(i8); -impl_int!(u16); -impl_int!(i16); -impl_int!(u32); -impl_int!(i32); -impl_int!(u64); -impl_int!(i64); - -trait Unsigned {} - -impl Unsigned for u8 {} -impl Unsigned for u16 {} -impl Unsigned for u32 {} -impl Unsigned for u64 {} - -trait Float: - Copy - + Default - + Add - + Sub - + Mul - + Div -{ - fn to_int_floor(self) -> I; -} - -macro_rules! impl_float { - ( $t:ident ) => { - impl Float for $t { - fn to_int_floor(self) -> I { - I::from_float_floor(self) - } - } - }; -} - -impl_float!(f32); -impl_float!(f64); - struct Grid { width: usize, height: usize, diff --git a/src/numeric_types.rs b/src/numeric_types.rs new file mode 100644 index 0000000..9223130 --- /dev/null +++ b/src/numeric_types.rs @@ -0,0 +1,94 @@ +use std::ops::{Add, Div, Mul, Sub}; + +pub trait FromFloatFloor { + fn from_float_floor(f: F) -> Self; +} + +pub trait ToFloat { + fn to_float(self) -> F; +} + +pub trait Integer: + Copy + + Default + + Add + + Sub + + Mul + + Div + + FromFloatFloor + + FromFloatFloor + + ToFloat + + ToFloat +{ + const MIN: Self; + const MAX: Self; +} + +macro_rules! impl_int { + ( $t:ident ) => { + impl FromFloatFloor for $t { + fn from_float_floor(f: f32) -> Self { + f as $t + } + } + impl FromFloatFloor for $t { + fn from_float_floor(f: f64) -> Self { + f as $t + } + } + impl ToFloat for $t { + fn to_float(self) -> f32 { + self as f32 + } + } + impl ToFloat for $t { + fn to_float(self) -> f64 { + self as f64 + } + } + impl Integer for $t { + const MIN: $t = $t::MIN; + const MAX: $t = $t::MAX; + } + }; +} + +impl_int!(u8); +impl_int!(i8); +impl_int!(u16); +impl_int!(i16); +impl_int!(u32); +impl_int!(i32); +impl_int!(u64); +impl_int!(i64); + +pub trait Unsigned {} + +impl Unsigned for u8 {} +impl Unsigned for u16 {} +impl Unsigned for u32 {} +impl Unsigned for u64 {} + +pub trait Float: + Copy + + Default + + Add + + Sub + + Mul + + Div +{ + fn to_int_floor(self) -> I; +} + +macro_rules! impl_float { + ( $t:ident ) => { + impl Float for $t { + fn to_int_floor(self) -> I { + I::from_float_floor(self) + } + } + }; +} + +impl_float!(f32); +impl_float!(f64);