Switched custom quantities to using a macro for generation.
This commit is contained in:
@ -6,10 +6,12 @@ members = [
|
|||||||
# Device types
|
# Device types
|
||||||
"node",
|
"node",
|
||||||
"commander",
|
"commander",
|
||||||
|
# Meta
|
||||||
|
"generate-quantity"
|
||||||
]
|
]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.3.5"
|
version = "0.3.6"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
repository = "https://git.bfpower.io/BFPOWER/physical"
|
repository = "https://git.bfpower.io/BFPOWER/physical"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -89,6 +91,8 @@ version = "2.0.*"
|
|||||||
features = ["extra-traits", "parsing"]
|
features = ["extra-traits", "parsing"]
|
||||||
[workspace.dependencies.quote]
|
[workspace.dependencies.quote]
|
||||||
version = "1.0.*"
|
version = "1.0.*"
|
||||||
|
[workspace.dependencies.proc-macro2]
|
||||||
|
version = "1.0.*"
|
||||||
[workspace.dependencies.trybuild]
|
[workspace.dependencies.trybuild]
|
||||||
version = "1.0.*"
|
version = "1.0.*"
|
||||||
|
|
||||||
@ -114,6 +118,8 @@ lm35 = []
|
|||||||
pid = []
|
pid = []
|
||||||
stm32 = []
|
stm32 = []
|
||||||
|
|
||||||
|
[dependencies.generate-quantity]
|
||||||
|
path = "generate-quantity"
|
||||||
[dependencies.num-traits]
|
[dependencies.num-traits]
|
||||||
workspace = true
|
workspace = true
|
||||||
[dependencies.derive_more]
|
[dependencies.derive_more]
|
||||||
|
18
generate-quantity/Cargo.toml
Normal file
18
generate-quantity/Cargo.toml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
[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
|
||||||
|
[dependencies.proc-macro2]
|
||||||
|
workspace = true
|
101
generate-quantity/src/lib.rs
Normal file
101
generate-quantity/src/lib.rs
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
extern crate proc_macro;
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||||
|
use quote::quote;
|
||||||
|
use syn::parse::{Parse, ParseStream};
|
||||||
|
use syn::{parse_macro_input, Ident, LitStr, Token};
|
||||||
|
|
||||||
|
const NUMBER_TYPES: &[&str] = &[
|
||||||
|
"u8", "i8", "u16", "i16", "u32", "i32", "u64", "i64", "u128", "i128", "usize", "isize", "f32",
|
||||||
|
"f64",
|
||||||
|
];
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
//----- Conversion impls ----------------------------------
|
||||||
|
let mut impls: Vec<TokenStream2> = Vec::new();
|
||||||
|
for ¤t_type in NUMBER_TYPES {
|
||||||
|
// Generate conversion methods for all primitive types except the current_type
|
||||||
|
let conversions = NUMBER_TYPES
|
||||||
|
.iter()
|
||||||
|
.filter(|&&t| t != current_type)
|
||||||
|
.map(|&target_type| {
|
||||||
|
let method_name = Ident::new(&format!("as_{}", target_type), Span::call_site());
|
||||||
|
let target_type = Ident::new(target_type, Span::call_site());
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
/// Directly [as] cast this quantities value while maintaining the type of the quantity.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn #method_name(self) -> #struct_name<#target_type> {
|
||||||
|
#struct_name(self.0 as #target_type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<TokenStream2>>();
|
||||||
|
|
||||||
|
let current_type = Ident::new(current_type, Span::call_site());
|
||||||
|
// Generate the impl block for the struct with the current_type
|
||||||
|
let expanded = quote! {
|
||||||
|
impl #struct_name<#current_type> {
|
||||||
|
#(#conversions)*
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
impls.push(expanded);
|
||||||
|
}
|
||||||
|
|
||||||
|
//----- 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#(#impls)*
|
||||||
|
};
|
||||||
|
|
||||||
|
expanded.into()
|
||||||
|
}
|
@ -1,22 +1,6 @@
|
|||||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
|
||||||
use crate::quantity::{Quantity, Value};
|
use crate::quantity::{Quantity, Value};
|
||||||
|
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||||
|
use generate_quantity::quantity_type;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Watter per Square Meter ----------------------------------
|
||||||
// ----- Watts per Square Meter ------------------------
|
quantity_type! {WattsPerSquareMeter, "W/m²"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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²"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -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> {
|
impl<Q: Quantity<i8>> Format for Fmt<i8, Q> {
|
||||||
fn format(&self, fmt: Formatter) {
|
fn format(&self, fmt: Formatter) {
|
||||||
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
|
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> {
|
impl<Q: Quantity<f32>> Format for Fmt<f32, Q> {
|
||||||
fn format(&self, fmt: Formatter) {
|
fn format(&self, fmt: Formatter) {
|
||||||
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
|
write!(fmt, "{} {}", self.quantity.value(), Q::symbol());
|
||||||
|
@ -1,22 +1,6 @@
|
|||||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||||
|
use generate_quantity::quantity_type;
|
||||||
use crate::quantity::{Quantity, Value};
|
use crate::quantity::{Quantity, Value};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Ohms ----------------------------------
|
||||||
// ----- Ohms ------------------------
|
quantity_type! {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 {
|
|
||||||
"Ω"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,28 +1,11 @@
|
|||||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||||
use num_traits::float::FloatCore;
|
use generate_quantity::quantity_type;
|
||||||
use crate::quantity::{DECI, Quantity, Value};
|
use crate::quantity::{DECI, Quantity, Value};
|
||||||
|
|
||||||
const KELVIN_CELSIUS_OFFSET: f64 = 273.15;
|
const KELVIN_CELSIUS_OFFSET: f64 = 273.15;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Kelvins ----------------------------------
|
||||||
// ----- Kelvins ------------------------
|
quantity_type! {Kelvins, "K"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl <V: Value> Kelvins<V> {
|
impl <V: Value> Kelvins<V> {
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -39,25 +22,8 @@ impl <V: Value> Kelvins<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Deci Kelvins ----------------------------------
|
||||||
// ----- Deci Kelvins ------------------------
|
quantity_type! {DeciKelvins, "dK"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Value> DeciKelvins<V> {
|
impl<V: Value> DeciKelvins<V> {
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -67,25 +33,8 @@ impl<V: Value> DeciKelvins<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Degrees Celsius ----------------------------------
|
||||||
// ----- Degrees Celsius ------------------------
|
quantity_type! {DegreesCelsius, "℃"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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 {
|
|
||||||
"℃"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl <V: Value> DegreesCelsius<V> {
|
impl <V: Value> DegreesCelsius<V> {
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -102,25 +51,8 @@ impl <V: Value> DegreesCelsius<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Deci Degrees Celsius ----------------------------------
|
||||||
// ----- Deci Degrees Celsius ------------------------
|
quantity_type! {DeciDegreesCelsius, "d℃"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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℃"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Value> DeciDegreesCelsius<V> {
|
impl<V: Value> DeciDegreesCelsius<V> {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1,27 +1,11 @@
|
|||||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||||
|
use generate_quantity::quantity_type;
|
||||||
use crate::quantity::{Quantity, Value};
|
use crate::quantity::{Quantity, Value};
|
||||||
|
|
||||||
const VOLT_MV_RATIO: u16 = 1_000;
|
const VOLT_MV_RATIO: u16 = 1_000;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Volts ----------------------------------
|
||||||
// ----- Volts ------------------------
|
quantity_type! {Volts, "V"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Value> Volts<V> {
|
impl<V: Value> Volts<V> {
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -31,25 +15,8 @@ impl<V: Value> Volts<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Milli Volts ----------------------------------
|
||||||
// ----- MilliVolts ------------------------
|
quantity_type! {MilliVolts, "mV"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: Value> MilliVolts<V> {
|
impl<V: Value> MilliVolts<V> {
|
||||||
pub fn to_volts(self) -> Volts<V> {
|
pub fn to_volts(self) -> Volts<V> {
|
||||||
|
@ -1,22 +1,6 @@
|
|||||||
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
|
||||||
use crate::quantity::{Quantity, Value};
|
use crate::quantity::{Quantity, Value};
|
||||||
|
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
|
||||||
|
use generate_quantity::quantity_type;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
//----- Liters per Minute ----------------------------------
|
||||||
// ----- Liters per Minute------------------------
|
quantity_type! {LitersPerMinute, "L/min"}
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
#[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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user