From d85c92bf5d71335176c8b727923b5886243121d2 Mon Sep 17 00:00:00 2001 From: Zachary Sunforge Date: Wed, 10 Jul 2024 22:06:16 -0700 Subject: [PATCH] Switched custom quantities to using a macro for generation. --- Cargo.toml | 6 +- generate-quantity/Cargo.toml | 16 + generate-quantity/src/lib.rs | 993 +++++++++++++++++++++++++++++++++++ src/quantity/irradiance.rs | 24 +- src/quantity/mod.rs | 16 +- src/quantity/resistance.rs | 22 +- src/quantity/temperature.rs | 86 +-- src/quantity/voltage.rs | 43 +- src/quantity/volume_rate.rs | 24 +- 9 files changed, 1053 insertions(+), 177 deletions(-) create mode 100644 generate-quantity/Cargo.toml create mode 100644 generate-quantity/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 60fd6ae..cc0c0e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,12 @@ members = [ # Device types "node", "commander", + # Meta + "generate-quantity" ] [workspace.package] -version = "0.3.5" +version = "0.3.6" edition = "2021" repository = "https://git.bfpower.io/BFPOWER/physical" readme = "README.md" @@ -114,6 +116,8 @@ lm35 = [] pid = [] stm32 = [] +[dependencies.generate-quantity] +path = "generate-quantity" [dependencies.num-traits] workspace = true [dependencies.derive_more] diff --git a/generate-quantity/Cargo.toml b/generate-quantity/Cargo.toml new file mode 100644 index 0000000..5d14cbd --- /dev/null +++ b/generate-quantity/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "generate-quantity" +description = "Macros for generating physical quantity type" +version.workspace = true +edition.workspace = true +repository.workspace = true +readme.workspace = true +license.workspace = true + +[lib] +proc-macro = true + +[dependencies.quote] +workspace = true +[dependencies.syn] +workspace = true \ No newline at end of file diff --git a/generate-quantity/src/lib.rs b/generate-quantity/src/lib.rs new file mode 100644 index 0000000..45dbe3d --- /dev/null +++ b/generate-quantity/src/lib.rs @@ -0,0 +1,993 @@ +extern crate proc_macro; +use proc_macro::TokenStream; +use quote::quote; +use syn::parse::{Parse, ParseStream}; +use syn::{parse_macro_input, Ident, LitStr, Token}; + +// Define the input structure for the macro +struct QuantityInput { + struct_name: Ident, + symbol: LitStr, +} + +// Implement the parsing for the input structure +impl Parse for QuantityInput { + fn parse(input: ParseStream) -> syn::Result { + let struct_name: Ident = input.parse()?; + input.parse::()?; + let symbol: LitStr = input.parse()?; + Ok(QuantityInput { + struct_name, + symbol, + }) + } +} + +#[proc_macro] +pub fn quantity_type(input: TokenStream) -> TokenStream { + // Parse the input tokens into the QuantityInput structure + let QuantityInput { + struct_name, + symbol, + } = parse_macro_input!(input as QuantityInput); + + // Generate the output code + let expanded = quote! { + #[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] + #[display(fmt = "{} {}", _0, "Self::symbol()")] + #[repr(transparent)] + pub struct #struct_name(pub V); + + impl Quantity for #struct_name { + #[inline(always)] + fn value(self) -> V { + self.0 + } + + #[inline(always)] + fn symbol() -> &'static str { + #symbol + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f64(self) -> #struct_name { + #struct_name(self.0 as f64) + } + } + + impl #struct_name { + #[inline(always)] + pub fn as_u8(self) -> #struct_name { + #struct_name(self.0 as u8) + } + + #[inline(always)] + pub fn as_u16(self) -> #struct_name { + #struct_name(self.0 as u16) + } + + #[inline(always)] + pub fn as_u32(self) -> #struct_name { + #struct_name(self.0 as u32) + } + + #[inline(always)] + pub fn as_u64(self) -> #struct_name { + #struct_name(self.0 as u64) + } + + #[inline(always)] + pub fn as_u128(self) -> #struct_name { + #struct_name(self.0 as u128) + } + + #[inline(always)] + pub fn as_usize(self) -> #struct_name { + #struct_name(self.0 as usize) + } + + #[inline(always)] + pub fn as_i8(self) -> #struct_name { + #struct_name(self.0 as i8) + } + + #[inline(always)] + pub fn as_i16(self) -> #struct_name { + #struct_name(self.0 as i16) + } + + #[inline(always)] + pub fn as_i32(self) -> #struct_name { + #struct_name(self.0 as i32) + } + + #[inline(always)] + pub fn as_i64(self) -> #struct_name { + #struct_name(self.0 as i64) + } + + #[inline(always)] + pub fn as_i128(self) -> #struct_name { + #struct_name(self.0 as i128) + } + + #[inline(always)] + pub fn as_isize(self) -> #struct_name { + #struct_name(self.0 as isize) + } + + #[inline(always)] + pub fn as_f32(self) -> #struct_name { + #struct_name(self.0 as f32) + } + } + }; + + TokenStream::from(expanded) +} diff --git a/src/quantity/irradiance.rs b/src/quantity/irradiance.rs index 297942a..20a041a 100644 --- a/src/quantity/irradiance.rs +++ b/src/quantity/irradiance.rs @@ -1,22 +1,6 @@ -use derive_more::{Add, AddAssign, Display, Sub, SubAssign}; use crate::quantity::{Quantity, Value}; +use derive_more::{Add, AddAssign, Display, Sub, SubAssign}; +use generate_quantity::quantity_type; -// --------------------------------------------------------------------------------------------------------------------- -// ----- Watts per Square Meter ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct WattsPerSquareMeter(pub V); - -impl Quantity for WattsPerSquareMeter { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "W/m²" - } -} \ No newline at end of file +//----- Watter per Square Meter ---------------------------------- +quantity_type! {WattsPerSquareMeter, "W/m²"} diff --git a/src/quantity/mod.rs b/src/quantity/mod.rs index 76fe2ed..20448a2 100644 --- a/src/quantity/mod.rs +++ b/src/quantity/mod.rs @@ -49,7 +49,7 @@ pub trait Value: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display { fn kelvins(self) -> Kelvins { Kelvins(self) } - + #[inline(always)] fn deci_kelvins(self) -> DeciKelvins { DeciKelvins(self) @@ -59,7 +59,7 @@ pub trait Value: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display { fn degrees_celsius(self) -> DegreesCelsius { DegreesCelsius(self) } - + #[inline(always)] fn deci_degrees_celsius(self) -> DeciDegreesCelsius { DeciDegreesCelsius(self) @@ -161,6 +161,12 @@ mod defmt_impl { } } + impl> Format for Fmt { + fn format(&self, fmt: Formatter) { + write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); + } + } + impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); @@ -191,6 +197,12 @@ mod defmt_impl { } } + impl> Format for Fmt { + fn format(&self, fmt: Formatter) { + write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); + } + } + impl> Format for Fmt { fn format(&self, fmt: Formatter) { write!(fmt, "{} {}", self.quantity.value(), Q::symbol()); diff --git a/src/quantity/resistance.rs b/src/quantity/resistance.rs index 01b0bee..ecbbd9d 100644 --- a/src/quantity/resistance.rs +++ b/src/quantity/resistance.rs @@ -1,22 +1,6 @@ use derive_more::{Add, AddAssign, Display, Sub, SubAssign}; +use generate_quantity::quantity_type; use crate::quantity::{Quantity, Value}; -// --------------------------------------------------------------------------------------------------------------------- -// ----- Ohms ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct Ohms(pub V); - -impl Quantity for Ohms { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "Ω" - } -} +//----- Ohms ---------------------------------- +quantity_type! {Ohms, "Ω"} diff --git a/src/quantity/temperature.rs b/src/quantity/temperature.rs index 3a8aafd..57dfd98 100644 --- a/src/quantity/temperature.rs +++ b/src/quantity/temperature.rs @@ -1,28 +1,11 @@ use derive_more::{Add, AddAssign, Display, Sub, SubAssign}; -use num_traits::float::FloatCore; +use generate_quantity::quantity_type; use crate::quantity::{DECI, Quantity, Value}; const KELVIN_CELSIUS_OFFSET: f64 = 273.15; -// --------------------------------------------------------------------------------------------------------------------- -// ----- Kelvins ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct Kelvins(pub V); - -impl Quantity for Kelvins { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "K" - } -} +//----- Kelvins ---------------------------------- +quantity_type! {Kelvins, "K"} impl Kelvins { #[inline] @@ -39,25 +22,8 @@ impl Kelvins { } } -// --------------------------------------------------------------------------------------------------------------------- -// ----- Deci Kelvins ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct DeciKelvins(pub V); - -impl Quantity for DeciKelvins { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "dK" - } -} +//----- Deci Kelvins ---------------------------------- +quantity_type! {DeciKelvins, "dK"} impl DeciKelvins { #[inline] @@ -67,25 +33,8 @@ impl DeciKelvins { } } -// --------------------------------------------------------------------------------------------------------------------- -// ----- Degrees Celsius ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct DegreesCelsius(pub V); - -impl Quantity for DegreesCelsius { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "℃" - } -} +//----- Degrees Celsius ---------------------------------- +quantity_type! {DegreesCelsius, "℃"} impl DegreesCelsius { #[inline] @@ -102,25 +51,8 @@ impl DegreesCelsius { } } -// --------------------------------------------------------------------------------------------------------------------- -// ----- Deci Degrees Celsius ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct DeciDegreesCelsius(pub V); - -impl Quantity for DeciDegreesCelsius { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "d℃" - } -} +//----- Deci Degrees Celsius ---------------------------------- +quantity_type! {DeciDegreesCelsius, "d℃"} impl DeciDegreesCelsius { #[inline] diff --git a/src/quantity/voltage.rs b/src/quantity/voltage.rs index 500c32f..f49fbfa 100644 --- a/src/quantity/voltage.rs +++ b/src/quantity/voltage.rs @@ -1,27 +1,11 @@ use derive_more::{Add, AddAssign, Display, Sub, SubAssign}; +use generate_quantity::quantity_type; use crate::quantity::{Quantity, Value}; const VOLT_MV_RATIO: u16 = 1_000; -// --------------------------------------------------------------------------------------------------------------------- -// ----- Volts ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct Volts(pub V); - -impl Quantity for Volts { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "V" - } -} +//----- Volts ---------------------------------- +quantity_type! {Volts, "V"} impl Volts { #[inline] @@ -31,25 +15,8 @@ impl Volts { } } -// --------------------------------------------------------------------------------------------------------------------- -// ----- MilliVolts ------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct MilliVolts(pub V); - -impl Quantity for MilliVolts { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "mV" - } -} +//----- Milli Volts ---------------------------------- +quantity_type! {MilliVolts, "mV"} impl MilliVolts { pub fn to_volts(self) -> Volts { diff --git a/src/quantity/volume_rate.rs b/src/quantity/volume_rate.rs index 04cc14a..9646b80 100644 --- a/src/quantity/volume_rate.rs +++ b/src/quantity/volume_rate.rs @@ -1,22 +1,6 @@ -use derive_more::{Add, AddAssign, Display, Sub, SubAssign}; use crate::quantity::{Quantity, Value}; +use derive_more::{Add, AddAssign, Display, Sub, SubAssign}; +use generate_quantity::quantity_type; -// --------------------------------------------------------------------------------------------------------------------- -// ----- Liters per Minute------------------------ -// --------------------------------------------------------------------------------------------------------------------- -#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)] -#[display(fmt = "{} {}", _0, "Self::symbol()")] -#[repr(transparent)] -pub struct LitersPerMinute(pub V); - -impl Quantity for LitersPerMinute { - #[inline(always)] - fn value(self) -> V { - self.0 - } - - #[inline(always)] - fn symbol() -> &'static str { - "L/min" - } -} \ No newline at end of file +//----- Liters per Minute ---------------------------------- +quantity_type! {LitersPerMinute, "L/min"}