Extract numeric type traits to own module

This commit is contained in:
Matthew Gordon 2024-02-03 12:39:28 -04:00
parent d84a1ee40c
commit 548f801bcf
2 changed files with 99 additions and 99 deletions

View File

@ -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<F> {
fn from_float_floor(f: F) -> Self;
}
trait ToFloat<F> {
fn to_float(self) -> F;
}
trait Integer:
Copy
+ Default
+ Add<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Mul<Self, Output = Self>
+ Div<Self, Output = Self>
+ FromFloatFloor<f32>
+ FromFloatFloor<f64>
+ ToFloat<f32>
+ ToFloat<f64>
{
const MIN: Self;
const MAX: Self;
}
macro_rules! impl_int {
( $t:ident ) => {
impl FromFloatFloor<f32> for $t {
fn from_float_floor(f: f32) -> Self {
f as $t
}
}
impl FromFloatFloor<f64> for $t {
fn from_float_floor(f: f64) -> Self {
f as $t
}
}
impl ToFloat<f32> for $t {
fn to_float(self) -> f32 {
self as f32
}
}
impl ToFloat<f64> 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<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Mul<Self, Output = Self>
+ Div<Self, Output = Self>
{
fn to_int_floor<I: Integer>(self) -> I;
}
macro_rules! impl_float {
( $t:ident ) => {
impl Float for $t {
fn to_int_floor<I: Integer>(self) -> I {
I::from_float_floor(self)
}
}
};
}
impl_float!(f32);
impl_float!(f64);
struct Grid<T> {
width: usize,
height: usize,

94
src/numeric_types.rs Normal file
View File

@ -0,0 +1,94 @@
use std::ops::{Add, Div, Mul, Sub};
pub trait FromFloatFloor<F> {
fn from_float_floor(f: F) -> Self;
}
pub trait ToFloat<F> {
fn to_float(self) -> F;
}
pub trait Integer:
Copy
+ Default
+ Add<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Mul<Self, Output = Self>
+ Div<Self, Output = Self>
+ FromFloatFloor<f32>
+ FromFloatFloor<f64>
+ ToFloat<f32>
+ ToFloat<f64>
{
const MIN: Self;
const MAX: Self;
}
macro_rules! impl_int {
( $t:ident ) => {
impl FromFloatFloor<f32> for $t {
fn from_float_floor(f: f32) -> Self {
f as $t
}
}
impl FromFloatFloor<f64> for $t {
fn from_float_floor(f: f64) -> Self {
f as $t
}
}
impl ToFloat<f32> for $t {
fn to_float(self) -> f32 {
self as f32
}
}
impl ToFloat<f64> 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<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Mul<Self, Output = Self>
+ Div<Self, Output = Self>
{
fn to_int_floor<I: Integer>(self) -> I;
}
macro_rules! impl_float {
( $t:ident ) => {
impl Float for $t {
fn to_int_floor<I: Integer>(self) -> I {
I::from_float_floor(self)
}
}
};
}
impl_float!(f32);
impl_float!(f64);