Switched custom quantities to using a macro for generation.

This commit is contained in:
Zachary Sunforge
2024-07-10 22:06:16 -07:00
parent da5fca74ef
commit c72766cd31
9 changed files with 1059 additions and 177 deletions

View File

@ -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]

View File

@ -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

View File

@ -0,0 +1,999 @@
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<Self> {
let struct_name: Ident = input.parse()?;
input.parse::<Token![,]>()?;
let symbol: LitStr = input.parse()?;
Ok(QuantityInput {
struct_name,
symbol,
})
}
}
/// The following imports must be in scope for this macro to work
/// ```
/// use physical::quantity::{Quantity, Value};
/// use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
/// use generate_quantity::quantity_type;
/// ```
#[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<V: Value>(pub V);
impl<V: Value> Quantity<V> for #struct_name<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
#symbol
}
}
impl #struct_name<u8> {
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<u16> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<u32> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<u64> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<u128> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<usize> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<i8> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<i16> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<i32> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<i64> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<i128> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<isize> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<f32> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f64(self) -> #struct_name<f64> {
#struct_name(self.0 as f64)
}
}
impl #struct_name<f64> {
#[inline(always)]
pub fn as_u8(self) -> #struct_name<u8> {
#struct_name(self.0 as u8)
}
#[inline(always)]
pub fn as_u16(self) -> #struct_name<u16> {
#struct_name(self.0 as u16)
}
#[inline(always)]
pub fn as_u32(self) -> #struct_name<u32> {
#struct_name(self.0 as u32)
}
#[inline(always)]
pub fn as_u64(self) -> #struct_name<u64> {
#struct_name(self.0 as u64)
}
#[inline(always)]
pub fn as_u128(self) -> #struct_name<u128> {
#struct_name(self.0 as u128)
}
#[inline(always)]
pub fn as_usize(self) -> #struct_name<usize> {
#struct_name(self.0 as usize)
}
#[inline(always)]
pub fn as_i8(self) -> #struct_name<i8> {
#struct_name(self.0 as i8)
}
#[inline(always)]
pub fn as_i16(self) -> #struct_name<i16> {
#struct_name(self.0 as i16)
}
#[inline(always)]
pub fn as_i32(self) -> #struct_name<i32> {
#struct_name(self.0 as i32)
}
#[inline(always)]
pub fn as_i64(self) -> #struct_name<i64> {
#struct_name(self.0 as i64)
}
#[inline(always)]
pub fn as_i128(self) -> #struct_name<i128> {
#struct_name(self.0 as i128)
}
#[inline(always)]
pub fn as_isize(self) -> #struct_name<isize> {
#struct_name(self.0 as isize)
}
#[inline(always)]
pub fn as_f32(self) -> #struct_name<f32> {
#struct_name(self.0 as f32)
}
}
};
TokenStream::from(expanded)
}

View File

@ -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<V: Value>(pub V);
impl <V: Value> Quantity<V> for WattsPerSquareMeter<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"W/m²"
}
}
//----- Watter per Square Meter ----------------------------------
quantity_type! {WattsPerSquareMeter, "W/m²"}

View File

@ -49,7 +49,7 @@ pub trait Value: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display {
fn kelvins(self) -> Kelvins<Self> {
Kelvins(self)
}
#[inline(always)]
fn deci_kelvins(self) -> DeciKelvins<Self> {
DeciKelvins(self)
@ -59,7 +59,7 @@ pub trait Value: Num + Copy + PartialOrd + FromPrimitive + NumCast + Display {
fn degrees_celsius(self) -> DegreesCelsius<Self> {
DegreesCelsius(self)
}
#[inline(always)]
fn deci_degrees_celsius(self) -> DeciDegreesCelsius<Self> {
DeciDegreesCelsius(self)
@ -161,6 +161,12 @@ mod defmt_impl {
}
}
impl<Q: Quantity<usize>> Format for Fmt<usize, Q> {
fn format(&self, fmt: Formatter) {
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
}
}
impl<Q: Quantity<i8>> Format for Fmt<i8, Q> {
fn format(&self, fmt: Formatter) {
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
@ -191,6 +197,12 @@ mod defmt_impl {
}
}
impl<Q: Quantity<isize>> Format for Fmt<isize, Q> {
fn format(&self, fmt: Formatter) {
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
}
}
impl<Q: Quantity<f32>> Format for Fmt<f32, Q> {
fn format(&self, fmt: Formatter) {
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());

View File

@ -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<V: Value>(pub V);
impl <V: Value> Quantity<V> for Ohms<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"Ω"
}
}
//----- Ohms ----------------------------------
quantity_type! {Ohms, "Ω"}

View File

@ -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<V: Value>(pub V);
impl <V: Value> Quantity<V> for Kelvins<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"K"
}
}
//----- Kelvins ----------------------------------
quantity_type! {Kelvins, "K"}
impl <V: Value> Kelvins<V> {
#[inline]
@ -39,25 +22,8 @@ impl <V: Value> Kelvins<V> {
}
}
// ---------------------------------------------------------------------------------------------------------------------
// ----- Deci Kelvins ------------------------
// ---------------------------------------------------------------------------------------------------------------------
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
#[display(fmt = "{} {}", _0, "Self::symbol()")]
#[repr(transparent)]
pub struct DeciKelvins<V: Value>(pub V);
impl <V: Value> Quantity<V> for DeciKelvins<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"dK"
}
}
//----- Deci Kelvins ----------------------------------
quantity_type! {DeciKelvins, "dK"}
impl<V: Value> DeciKelvins<V> {
#[inline]
@ -67,25 +33,8 @@ impl<V: Value> DeciKelvins<V> {
}
}
// ---------------------------------------------------------------------------------------------------------------------
// ----- Degrees Celsius ------------------------
// ---------------------------------------------------------------------------------------------------------------------
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
#[display(fmt = "{} {}", _0, "Self::symbol()")]
#[repr(transparent)]
pub struct DegreesCelsius<V: Value>(pub V);
impl <V: Value> Quantity<V> for DegreesCelsius<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
""
}
}
//----- Degrees Celsius ----------------------------------
quantity_type! {DegreesCelsius, ""}
impl <V: Value> DegreesCelsius<V> {
#[inline]
@ -102,25 +51,8 @@ impl <V: Value> DegreesCelsius<V> {
}
}
// ---------------------------------------------------------------------------------------------------------------------
// ----- Deci Degrees Celsius ------------------------
// ---------------------------------------------------------------------------------------------------------------------
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
#[display(fmt = "{} {}", _0, "Self::symbol()")]
#[repr(transparent)]
pub struct DeciDegreesCelsius<V: Value>(pub V);
impl <V: Value> Quantity<V> for DeciDegreesCelsius<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"d℃"
}
}
//----- Deci Degrees Celsius ----------------------------------
quantity_type! {DeciDegreesCelsius, "d℃"}
impl<V: Value> DeciDegreesCelsius<V> {
#[inline]

View File

@ -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<V: Value>(pub V);
impl <V: Value> Quantity<V> for Volts<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"V"
}
}
//----- Volts ----------------------------------
quantity_type! {Volts, "V"}
impl<V: Value> Volts<V> {
#[inline]
@ -31,25 +15,8 @@ impl<V: Value> Volts<V> {
}
}
// ---------------------------------------------------------------------------------------------------------------------
// ----- MilliVolts ------------------------
// ---------------------------------------------------------------------------------------------------------------------
#[derive(Copy, Clone, PartialEq, PartialOrd, Add, AddAssign, Sub, SubAssign, Display)]
#[display(fmt = "{} {}", _0, "Self::symbol()")]
#[repr(transparent)]
pub struct MilliVolts<V: Value>(pub V);
impl <V: Value> Quantity<V> for MilliVolts<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"mV"
}
}
//----- Milli Volts ----------------------------------
quantity_type! {MilliVolts, "mV"}
impl<V: Value> MilliVolts<V> {
pub fn to_volts(self) -> Volts<V> {

View File

@ -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<V: Value>(pub V);
impl <V: Value> Quantity<V> for LitersPerMinute<V> {
#[inline(always)]
fn value(self) -> V {
self.0
}
#[inline(always)]
fn symbol() -> &'static str {
"L/min"
}
}
//----- Liters per Minute ----------------------------------
quantity_type! {LitersPerMinute, "L/min"}