From 8cdabec7418ecaa6725a8001d8c83be67b806499 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 30 Oct 2023 22:01:17 +0100 Subject: [PATCH 1/8] Add `#[allow(clippy::eq_op)]` for `unit!` macro. Without this, `clippy` would give a lot of errors, because of the `prefix!(kilo) / prefix!(kilo)` and other similar macro calls which have result in 1 which is okay for our use case. Therefore the `#[allow(...)]` seems reasonable here. --- src/unit.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/unit.rs b/src/unit.rs index cf86fc7b..b6251243 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -283,8 +283,8 @@ macro_rules! unit { #[derive(Clone, Copy, Debug, Hash)] pub struct $unit; }; - (@coefficient $factor:expr, $const:expr) => { $factor }; - (@coefficient $factor:expr) => { $factor }; + (@coefficient $factor:expr, $const:expr) => { #[allow(clippy::eq_op)] {$factor} }; + (@coefficient $factor:expr) => { #[allow(clippy::eq_op)] {$factor} }; (@constant $op:ident $factor:expr, $const:expr) => { $const }; (@constant $op:ident $factor:expr) => { match $op { From 37ce1e895be3e626aa17ecf6ef5abd8ab715775d Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 30 Oct 2023 22:03:57 +0100 Subject: [PATCH 2/8] Implement clone properly for copy types. This fixes another `clippy` warning: ``` warning: non-canonical implementation of `clone` on a `Copy` type --> src/system.rs:1468:41 | 1468 | fn clone(&self) -> Self { | _________________________________________^ 1469 | | Self { 1470 | | dimension: $crate::lib::marker::PhantomData, 1471 | | unit: self.unit.clone(), 1472 | | style: self.style.clone(), 1473 | | } 1474 | | } | |_________________^ help: change this to: `{ *self }` ``` --- src/system.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/system.rs b/src/system.rs index d73ff3c1..5228e929 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1466,11 +1466,7 @@ macro_rules! system { N: Unit, { fn clone(&self) -> Self { - Self { - dimension: $crate::lib::marker::PhantomData, - unit: self.unit.clone(), - style: self.style.clone(), - } + *self } } From fef20c244f3b3ec28c87a2add8d9f5fd7e65fb61 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 30 Oct 2023 22:34:06 +0100 Subject: [PATCH 3/8] Fix documentation related warnings. --- src/lib.rs | 4 ++-- src/quantity.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2f9864b0..3b434dff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -431,8 +431,8 @@ pub trait Conversion { /// converting the given unit. To convert to the base unit for the quantity use `(value + /// constant()) * coefficient()`. To convert from the base unit, `(value / coefficient()) - /// constant()` is used. Implementation should return the additive identity (`Self::T::zero()`) - /// if no constant exists. See [ConstantOp](enum.ConstantOp.html) documentation for details - /// about parameter use to ensure the method optimizes correctly. + /// if no constant exists. See [`ConstantOp`] documentation for details about parameter use to + /// ensure the method optimizes correctly. #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] #[allow(unused_variables)] diff --git a/src/quantity.rs b/src/quantity.rs index 4e1bccb4..b6513718 100644 --- a/src/quantity.rs +++ b/src/quantity.rs @@ -1,6 +1,6 @@ /// Macro to implement a [quantity][quantity] and associated [measurement units][measurement]. Note -/// that this macro must be executed in direct submodules of the module where the -/// [`system!`](macro.system.html) macro was executed. `@...` match arms are considered private. +/// that this macro must be executed in direct submodules of the module where the [`system!`] macro +/// was executed. `@...` match arms are considered private. /// /// * `$quantity_attr`: Quantity attributes. Generally used to set documentation comments for the /// quantity. @@ -13,7 +13,7 @@ /// represented as a `typenum` type-level integer (e.g. `N1`, `Z0`, `P1`, `P2`, ...). /// * `$kind`: [Kind][kind] of the quantity. Optional. This variable should only be specified when /// defining a quantity that has the same dimensions as another quantity but isn't comparable. -/// When not specified [`uom::Kind`](trait.Kind.html) is used. +/// When not specified [`crate::Kind`] is used. /// * `$unit`: Unit name (e.g. `meter`, `foot`). /// * `$conversion`: Conversion (coefficient and constant factor) from the unit to the base unit of /// the quantity (e.g. `3.048_E-1` to convert `foot` to `meter`. `1.0_E0, 273.15_E0` to convert @@ -125,7 +125,7 @@ macro_rules! quantity { pub trait Unit: __system::Unit {} /// Trait to identify [units][units] which have a [conversion factor][factor] for the - /// `Quantity`. See [`Conversion`](../../trait.Conversion.html). + /// `Quantity`. See [`crate::Conversion`]. /// /// ## Generic Parameters /// * `V`: Underlying storage type trait is implemented for. From dd89552d05df60fa0520b3bb20b375b9af9ee362 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 30 Oct 2023 22:34:57 +0100 Subject: [PATCH 4/8] Fix warnings regarding glob import. --- src/lib.rs | 2 +- src/si/ratio.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3b434dff..32356ced 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -738,7 +738,7 @@ pub mod str { impl Display for ParseQuantityError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - use ParseQuantityError::*; + use ParseQuantityError::{NoSeparator, UnknownUnit, ValueParseError}; match *self { NoSeparator => write!(f, "no space between quantity and units"), diff --git a/src/si/ratio.rs b/src/si/ratio.rs index bd789871..5790d10a 100644 --- a/src/si/ratio.rs +++ b/src/si/ratio.rs @@ -140,13 +140,13 @@ where /// operations were performed separately. #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn ln_1p(self) -> Ratio { + pub fn ln_1p(self) -> Self { Ratio::new::(self.value.ln_1p()) } } mod convert { - use super::*; + use super::Ratio; impl From for Ratio where @@ -163,7 +163,7 @@ mod convert { } storage_types! { - use super::*; + use super::Ratio; impl From> for V where From e06a5e82043e7750676d4b69eb7d50834fb75725 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 30 Oct 2023 22:35:23 +0100 Subject: [PATCH 5/8] Use more `Self`. --- src/lib.rs | 10 +++++----- src/si/ratio.rs | 22 +++++++++++----------- src/si/time.rs | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 32356ced..91dfebf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -515,8 +515,8 @@ pub trait Kind: storage_types! { types: Float; - impl crate::Conversion for V { - type T = V; + impl crate::Conversion for V { + type T = Self; #[inline(always)] fn constant(op: crate::ConstantOp) -> Self::T { @@ -532,14 +532,14 @@ storage_types! { } } - impl crate::ConversionFactor for V { + impl crate::ConversionFactor for V { #[inline(always)] fn powi(self, e: i32) -> Self { - ::powi(self, e) + ::powi(self, e) } #[inline(always)] - fn value(self) -> V { + fn value(self) -> Self { self } } diff --git a/src/si/ratio.rs b/src/si/ratio.rs index 5790d10a..9ea65371 100644 --- a/src/si/ratio.rs +++ b/src/si/ratio.rs @@ -86,21 +86,21 @@ where /// Returns `e^(self)`, (the exponential function). #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn exp(self) -> Ratio { + pub fn exp(self) -> Self { Ratio::new::(self.value.exp()) } /// Returns 2^(self). #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn exp2(self) -> Ratio { + pub fn exp2(self) -> Self { Ratio::new::(self.value.exp2()) } /// Returns the natural logarithm of the number. #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn ln(self) -> Ratio { + pub fn ln(self) -> Self { Ratio::new::(self.value.ln()) } @@ -111,28 +111,28 @@ where /// self.log10() can produce more accurate results for base 10. #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn log(self, base: V) -> Ratio { + pub fn log(self, base: V) -> Self { Ratio::new::(self.value.log(base)) } /// Returns the base 2 logarithm of the number. #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn log2(self) -> Ratio { + pub fn log2(self) -> Self { Ratio::new::(self.value.log2()) } /// Returns the base 10 logarithm of the number. #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn log10(self) -> Ratio { + pub fn log10(self) -> Self { Ratio::new::(self.value.log10()) } /// Returns e^(self) - 1 in a way that is accurate even if the number is close to zero. #[must_use = "method returns a new number and does not mutate the original value"] #[inline(always)] - pub fn exp_m1(self) -> Ratio { + pub fn exp_m1(self) -> Self { Ratio::new::(self.value.exp_m1()) } @@ -165,12 +165,12 @@ mod convert { storage_types! { use super::Ratio; - impl From> for V + impl From> for V where - U: crate::si::Units + ?Sized, - V: crate::num::Num + crate::Conversion, + U: crate::si::Units + ?Sized, + Self: crate::num::Num + crate::Conversion, { - fn from(t: Ratio) -> Self { + fn from(t: Ratio) -> Self { t.value } } diff --git a/src/si/time.rs b/src/si/time.rs index 5f03de91..a5c8124f 100644 --- a/src/si/time.rs +++ b/src/si/time.rs @@ -96,7 +96,7 @@ where let nanos = (time % Time::::new::(V::one())).get::().to_u32(); match (secs, nanos) { - (Some(secs), Some(nanos)) => Ok(Duration::new(secs, nanos)), + (Some(secs), Some(nanos)) => Ok(Self::new(secs, nanos)), _ => Err(TryFromError::Overflow), } } From 499b3c3c2d6304f0e3cb62d631c800b1af5f0af7 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 30 Oct 2023 22:36:08 +0100 Subject: [PATCH 6/8] Make description a `const fn`. --- src/quantity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/quantity.rs b/src/quantity.rs index b6513718..0a75fbca 100644 --- a/src/quantity.rs +++ b/src/quantity.rs @@ -147,7 +147,7 @@ macro_rules! quantity { #[must_use = "method returns a static value"] #[allow(dead_code)] #[inline(always)] - pub fn description() -> &'static str { + pub const fn description() -> &'static str { $description } From 093375560fc1221d341eba1f62858969e5c41534 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 30 Oct 2023 22:36:20 +0100 Subject: [PATCH 7/8] Use digit separator. --- src/si/electric_permittivity.rs | 2 +- src/si/magnetic_moment.rs | 4 ++-- src/si/mass_rate.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/si/electric_permittivity.rs b/src/si/electric_permittivity.rs index 69d0f9c5..c417c616 100644 --- a/src/si/electric_permittivity.rs +++ b/src/si/electric_permittivity.rs @@ -15,7 +15,7 @@ quantity! { Z0>; // luminous intensity units { @farad_per_meter: prefix!(none); "F/m", "farad per meter", "farads per meter"; - @vacuum_electric_permittivity: 8.854_187_8128_E-12; "ε₀", "vacuum electric permittivity", + @vacuum_electric_permittivity: 8.854_187_812_8_E-12; "ε₀", "vacuum electric permittivity", "vacuum electric permittivity"; } } diff --git a/src/si/magnetic_moment.rs b/src/si/magnetic_moment.rs index bb5b7102..dcc6362c 100644 --- a/src/si/magnetic_moment.rs +++ b/src/si/magnetic_moment.rs @@ -25,8 +25,8 @@ quantity! { "statA · cm²", "statampere square centimeter", "statampere square centimeters"; @erg_per_gauss: 1.0_E-7 / 1.0_E-4; "erg/G", "erg per gauss", "ergs per gauss"; - @bohr_magneton: 9.274_010_0783_E-24; "µ(Bohr)", "Bohr magneton", "Bohr magnetons"; - @nuclear_magneton: 5.050_783_7461_E-27; "μ(Nuclear)", "nuclear magneton", + @bohr_magneton: 9.274_010_078_3_E-24; "µ(Bohr)", "Bohr magneton", "Bohr magnetons"; + @nuclear_magneton: 5.050_783_746_1_E-27; "μ(Nuclear)", "nuclear magneton", "nuclear magnetons"; @atomic_unit_of_magnetic_dipole_moment: 1.854_802_015_66_E-23; "ħ · e/mₑ", "atomic unit of magnetic dipole moment", " atomic units of magnetic dipole moment"; diff --git a/src/si/mass_rate.rs b/src/si/mass_rate.rs index 55b1a600..8e440c19 100644 --- a/src/si/mass_rate.rs +++ b/src/si/mass_rate.rs @@ -100,7 +100,7 @@ quantity! { /// Ton per minute, metric. @ton_per_minute: 1.666_666_666_666_666_6_E1; "t/min", "ton per minute", "tons per minute"; /// Ton per hour, metric. - @ton_per_hour: 2.777777777777778_E-1; "t/h", "ton per hour", "tons per hour"; + @ton_per_hour: 2.777_777_777_777_778_E-1; "t/h", "ton per hour", "tons per hour"; /// Ton per day, metric. @ton_per_day: 1.157_407_407_407_407_4_E-2; "t/d", "ton per day", "tons per day"; } From 35e0cfafef2fc20974f36fb65177457f2f51b9a1 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Fri, 3 Nov 2023 21:23:37 +0100 Subject: [PATCH 8/8] Make unit a unused phantom data in `fmt::Arguments`. The value of that type is never use, only the actual type parameter `N` --- src/quantity.rs | 8 ++++---- src/system.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/quantity.rs b/src/quantity.rs index 0a75fbca..a58d332a 100644 --- a/src/quantity.rs +++ b/src/quantity.rs @@ -337,7 +337,7 @@ macro_rules! quantity { /// * `N`: Unit. #[must_use = "method returns a new object"] pub fn format_args( - unit: N, + _unit: N, style: $crate::fmt::DisplayStyle ) -> __system::fmt::Arguments where @@ -345,7 +345,7 @@ macro_rules! quantity { { __system::fmt::Arguments { dimension: $crate::lib::marker::PhantomData, - unit, + unit: $crate::lib::marker::PhantomData, style, } } @@ -378,7 +378,7 @@ macro_rules! quantity { #[must_use = "method returns a new object and does not mutate the original one"] pub fn into_format_args( self, - unit: N, + _unit: N, style: $crate::fmt::DisplayStyle ) -> __system::fmt::QuantityArguments where @@ -387,7 +387,7 @@ macro_rules! quantity { __system::fmt::QuantityArguments { arguments: __system::fmt::Arguments { dimension: $crate::lib::marker::PhantomData, - unit, + unit: $crate::lib::marker::PhantomData, style, }, quantity: self, diff --git a/src/system.rs b/src/system.rs index 5228e929..2a0e0c2a 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1423,10 +1423,10 @@ macro_rules! system { pub struct Arguments where D: Dimension + ?Sized, - N: Unit, + N: Unit + ?Sized, { pub(super) dimension: $crate::lib::marker::PhantomData, - pub(super) unit: N, + pub(super) unit: $crate::lib::marker::PhantomData, pub(super) style: DisplayStyle, }