This commit is contained in:
Max Känner 2023-02-21 20:24:18 +01:00
parent 0dcb7b2722
commit 408a205e11
3 changed files with 27 additions and 3 deletions

View File

@ -2,6 +2,12 @@
name = "units" name = "units"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
description = "Si units for typesafe calculations"
license = "MIT or Apache-2.0"
repository = "https://git.mkaenner.de/max/units-rs.git"
readme = "README.md"
keywords = ["si", "units"]
categories = ["Science", "Simulation"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -2,8 +2,20 @@ use std::fmt::{Display, Formatter};
use typenum::Integer; use typenum::Integer;
use crate::{types::*, SiUnit}; use crate::{
types::{
AmperePerMeter, AmperePerSquareMeter, Coulomb, CoulombPerCubicMeter, CoulombPerKilogram,
CoulombPerSquareMeter, CubicMeterPerKilogram, Farad, FaradPerMeter, Gray, GrayPerSecond,
Henry, HenryPerMeter, Herz, Joule, JoulePerKelvin, JoulePerKilogramKelvin, JoulePerMole,
JoulePerMoleKelvin, Katal, KatalPerCubicMeter, KilogramPerCubicMeter,
KilogramPerSquareMeter, Lux, MeterPerSecond, MeterPerSquareSecond, MolePerCubicMeter,
Newton, NewtonPerMeter, Ohm, Pascal, PascalSecond, Siemens, Tesla, Volt, VoltPerMeter,
Watt, WattPerMeterKelvin, WattPerSquareMeter, Weber,
},
SiUnit,
};
#[cfg(feature = "defmt")]
macro_rules! display_unit_defmt { macro_rules! display_unit_defmt {
($formatter: ident, $param: ident, $symbol: literal, $e1: ident, $e2: ident, $e3: ident, $e4: ident, $e5: ident, $e6: ident) => { ($formatter: ident, $param: ident, $symbol: literal, $e1: ident, $e2: ident, $e3: ident, $e4: ident, $e5: ident, $e6: ident) => {
if $param::I64 != 0 { if $param::I64 != 0 {
@ -25,6 +37,7 @@ macro_rules! display_unit_defmt {
}; };
} }
#[cfg(feature = "defmt")]
macro_rules! display_special_unit_defmt { macro_rules! display_special_unit_defmt {
($formatter: ident, $self: ident, $(($symbol: literal, $other: ty)),* $(,)?) => { ($formatter: ident, $self: ident, $(($symbol: literal, $other: ty)),* $(,)?) => {
$( $(
@ -49,6 +62,7 @@ where
Candela: Integer, Candela: Integer,
T: defmt::Format + 'static, T: defmt::Format + 'static,
{ {
#[allow(clippy::cognitive_complexity)]
fn format(&self, f: defmt::Formatter<'_>) { fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f, "{}", self.value); defmt::write!(f, "{}", self.value);
// derived units with special symbols // derived units with special symbols
@ -221,6 +235,8 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::types::{Ampere, Candela, Kelvin, Kilogram, Meter, Mole, Second, Unit};
use super::*; use super::*;
#[test] #[test]
@ -230,6 +246,7 @@ mod test {
} }
#[test] #[test]
#[allow(clippy::cognitive_complexity)]
fn display() { fn display() {
let unit = Unit::new(2); let unit = Unit::new(2);
let second = Second::new(2); let second = Second::new(2);

View File

@ -2,7 +2,6 @@ mod display;
pub mod types; pub mod types;
use std::{ use std::{
fmt::{Display, Formatter},
marker::PhantomData, marker::PhantomData,
ops::{ ops::{
Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub,
@ -556,7 +555,9 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::types::*; use super::types::{
Ampere, Coulomb, CubicMeter, Meter, ReciprocalMeter, Second, SquareMeter, Unit, Volt, Watt,
};
use num_traits::{Num, One, Zero}; use num_traits::{Num, One, Zero};
#[test] #[test]