From c2ed1b256a9d35292d4e0fe516dc95c6191ab6ea Mon Sep 17 00:00:00 2001 From: Steve Loveless Date: Tue, 10 Oct 2023 13:52:43 -0700 Subject: [PATCH] Remove lifetimes from some traits --- api/src/measurement/v2.rs | 4 ++-- api/src/measurement/v2/convert.rs | 2 +- api/src/measurement/v2/ops.rs | 4 ++-- api/src/parser/atom/v2.rs | 2 +- api/src/parser/definition/v2.rs | 10 +++++----- api/src/parser/prefix/v2.rs | 2 +- api/src/parser/property.rs | 2 +- api/src/parser/term/v2.rs | 8 ++++---- api/src/parser/term/v2/convert.rs | 2 +- api/src/parser/terms/v2.rs | 4 ++-- api/src/unit/v2.rs | 12 ++++++------ api/src/unit/v2/convert.rs | 2 +- api/src/unit/v2/ops.rs | 4 ++-- api/src/v2/behavior_traits/convert.rs | 4 ++-- api/src/v2/behavior_traits/ops.rs | 15 +++++++-------- api/src/v2/measurement/convert.rs | 11 +++++------ api/src/v2/type_traits/atom.rs | 4 ++-- api/src/v2/type_traits/definition.rs | 6 +++--- api/src/v2/type_traits/measurement.rs | 10 +++++----- api/src/v2/type_traits/prefix.rs | 4 ++-- api/src/v2/type_traits/property.rs | 4 ++-- api/src/v2/type_traits/term.rs | 14 +++++++------- api/src/v2/type_traits/unit.rs | 12 +++++------- 23 files changed, 69 insertions(+), 73 deletions(-) diff --git a/api/src/measurement/v2.rs b/api/src/measurement/v2.rs index d6adf42d..b33ce112 100644 --- a/api/src/measurement/v2.rs +++ b/api/src/measurement/v2.rs @@ -8,11 +8,11 @@ use crate::{v2::type_traits, Measurement}; impl<'a> type_traits::Measurement<'a, f64> for Measurement { type Unit = crate::Unit; - fn value(&'a self) -> &'a f64 { + fn value(&self) -> &f64 { &self.value } - fn unit(&'a self) -> &'a Self::Unit { + fn unit(&self) -> &Self::Unit { &self.unit } } diff --git a/api/src/measurement/v2/convert.rs b/api/src/measurement/v2/convert.rs index 3e6ce19d..b9ef0aeb 100644 --- a/api/src/measurement/v2/convert.rs +++ b/api/src/measurement/v2/convert.rs @@ -50,7 +50,7 @@ impl convert::ToScalar for Measurement { } } -impl convert::ToMagnitude<'_, f64> for Measurement { +impl convert::ToMagnitude for Measurement { fn to_magnitude(&self) -> f64 { // Just delegate to the old trait impl for now. crate::UcumUnit::magnitude(self) diff --git a/api/src/measurement/v2/ops.rs b/api/src/measurement/v2/ops.rs index 0ad9fddd..00772ea9 100644 --- a/api/src/measurement/v2/ops.rs +++ b/api/src/measurement/v2/ops.rs @@ -34,7 +34,7 @@ impl ops::MulRef for Measurement { } } -impl ops::TryMulRef<'_> for Measurement { +impl ops::TryMulRef for Measurement { type Error = Infallible; fn try_mul_ref(&self, rhs: &Self) -> Result { @@ -78,7 +78,7 @@ impl ops::DivRef for Measurement { } } -impl ops::TryDivRef<'_> for Measurement { +impl ops::TryDivRef for Measurement { type Error = Infallible; fn try_div_ref(&self, rhs: &Self) -> Result { diff --git a/api/src/parser/atom/v2.rs b/api/src/parser/atom/v2.rs index fd5f792a..8619d354 100644 --- a/api/src/parser/atom/v2.rs +++ b/api/src/parser/atom/v2.rs @@ -8,7 +8,7 @@ use crate::{ use super::Atom; -impl<'a> type_traits::Atom<'a, f64> for Atom { +impl type_traits::Atom for Atom { type String = &'static str; type Names = Vec<&'static str>; type Property = crate::Property; diff --git a/api/src/parser/definition/v2.rs b/api/src/parser/definition/v2.rs index 2f46e164..0f3d306a 100644 --- a/api/src/parser/definition/v2.rs +++ b/api/src/parser/definition/v2.rs @@ -2,14 +2,14 @@ use crate::v2::{behavior_traits::convert, type_traits}; use super::Definition; -impl<'a> type_traits::Definition<'a, f64> for Definition { +impl type_traits::Definition for Definition { type Unit = Vec; - fn value(&'a self) -> &'a f64 { + fn value(&self) -> &f64 { &self.value } - fn unit(&'a self) -> Option<&'a Self::Unit> { + fn unit(&self) -> Option<&Self::Unit> { Some(self.terms()) } } @@ -20,8 +20,8 @@ impl convert::ToScalar for Definition { } } -impl<'a> convert::ToMagnitude<'a, f64> for Definition { - fn to_magnitude(&'a self) -> f64 { +impl convert::ToMagnitude for Definition { + fn to_magnitude(&self) -> f64 { self.terms.to_magnitude() } } diff --git a/api/src/parser/prefix/v2.rs b/api/src/parser/prefix/v2.rs index cc5ca2ac..47a3fae0 100644 --- a/api/src/parser/prefix/v2.rs +++ b/api/src/parser/prefix/v2.rs @@ -3,7 +3,7 @@ use crate::{ UcumSymbol, }; -impl<'a> TPrefix<'a, f64> for Prefix { +impl TPrefix for Prefix { type String = &'static str; type Names = Vec<&'static str>; type Class = Classification; diff --git a/api/src/parser/property.rs b/api/src/parser/property.rs index 47c2e5c0..12211294 100644 --- a/api/src/parser/property.rs +++ b/api/src/parser/property.rs @@ -4,7 +4,7 @@ //----------------------------------------------------------------------------- #[cfg(feature = "v2")] -impl<'a> crate::v2::type_traits::Property<'a, f64> for Property { +impl crate::v2::type_traits::Property for Property { type Atom = Atom; fn atoms(&self) -> Vec { diff --git a/api/src/parser/term/v2.rs b/api/src/parser/term/v2.rs index fadba2dc..ea74bdf5 100644 --- a/api/src/parser/term/v2.rs +++ b/api/src/parser/term/v2.rs @@ -9,19 +9,19 @@ impl<'a> TTerm<'a, f64> for Term { type Atom = Atom; type Annotation = &'a str; - fn factor(&'a self) -> Option { + fn factor(&self) -> Option { self.factor } - fn prefix_symbol(&'a self) -> Option { + fn prefix_symbol(&self) -> Option { self.prefix } - fn atom_symbol(&'a self) -> Option { + fn atom_symbol(&self) -> Option { self.atom } - fn exponent(&'a self) -> Option { + fn exponent(&self) -> Option { self.exponent } diff --git a/api/src/parser/term/v2/convert.rs b/api/src/parser/term/v2/convert.rs index 5721800d..cdaf8020 100644 --- a/api/src/parser/term/v2/convert.rs +++ b/api/src/parser/term/v2/convert.rs @@ -22,7 +22,7 @@ impl convert::ToScalar for Term { } } -impl convert::ToMagnitude<'_, f64> for Term { +impl convert::ToMagnitude for Term { fn to_magnitude(&self) -> f64 { // Just delegate to existing impl for now. crate::UcumUnit::magnitude(self) diff --git a/api/src/parser/terms/v2.rs b/api/src/parser/terms/v2.rs index ebab4fb4..626d3d36 100644 --- a/api/src/parser/terms/v2.rs +++ b/api/src/parser/terms/v2.rs @@ -20,8 +20,8 @@ impl convert::ToScalar for Vec { } } -impl<'a> convert::ToMagnitude<'a, f64> for Vec { - fn to_magnitude(&'a self) -> f64 { +impl convert::ToMagnitude for Vec { + fn to_magnitude(&self) -> f64 { self.iter() .map(convert::ToMagnitude::to_magnitude) .product() diff --git a/api/src/unit/v2.rs b/api/src/unit/v2.rs index 62c0e185..b5b692ee 100644 --- a/api/src/unit/v2.rs +++ b/api/src/unit/v2.rs @@ -7,19 +7,19 @@ use std::borrow::Cow; use crate::{v2::type_traits::Unit as TUnit, UcumUnit, Unit}; -impl TUnit<'_, f64> for Unit { - type InputString = str; +impl<'a> TUnit<'a, f64> for Unit { + type InputString = &'a str; type ParseError = crate::Error; - type Expression = String; + type Expression = Cow<'a, str>; - fn parse(string: &Self::InputString) -> Result { + fn parse(string: Self::InputString) -> Result { use std::str::FromStr; Self::from_str(string) } - fn expression(&self) -> Cow<'_, Self::Expression> { - Cow::Owned(Self::expression(self)) + fn expression(&'a self) -> Self::Expression { + self.as_str() } fn is_special(&self) -> bool { diff --git a/api/src/unit/v2/convert.rs b/api/src/unit/v2/convert.rs index ae886a92..2b65646b 100644 --- a/api/src/unit/v2/convert.rs +++ b/api/src/unit/v2/convert.rs @@ -38,7 +38,7 @@ impl convert::ToScalar for Unit { } } -impl convert::ToMagnitude<'_, f64> for Unit { +impl convert::ToMagnitude for Unit { fn to_magnitude(&self) -> f64 { // Just delegate to the old trait impl for now. crate::UcumUnit::magnitude(self) diff --git a/api/src/unit/v2/ops.rs b/api/src/unit/v2/ops.rs index 88ce9897..f6c90480 100644 --- a/api/src/unit/v2/ops.rs +++ b/api/src/unit/v2/ops.rs @@ -8,7 +8,7 @@ impl ops::MulRef for Unit { } } -impl ops::TryMulRef<'_> for Unit { +impl ops::TryMulRef for Unit { type Error = Infallible; fn try_mul_ref(&self, rhs: &Self) -> Result { @@ -22,7 +22,7 @@ impl ops::DivRef for Unit { } } -impl ops::TryDivRef<'_> for Unit { +impl ops::TryDivRef for Unit { type Error = Infallible; fn try_div_ref(&self, rhs: &Self) -> Result { diff --git a/api/src/v2/behavior_traits/convert.rs b/api/src/v2/behavior_traits/convert.rs index 93a002cc..efae7036 100644 --- a/api/src/v2/behavior_traits/convert.rs +++ b/api/src/v2/behavior_traits/convert.rs @@ -31,8 +31,8 @@ pub trait ToScalar { fn to_scalar(&self) -> V; } -pub trait ToMagnitude<'a, T> { - fn to_magnitude(&'a self) -> T; +pub trait ToMagnitude { + fn to_magnitude(&self) -> T; } // NOTE: This is the next version of `AsFraction`, which was incorrectly named, according to Rust diff --git a/api/src/v2/behavior_traits/ops.rs b/api/src/v2/behavior_traits/ops.rs index 2173e50e..82cb4054 100644 --- a/api/src/v2/behavior_traits/ops.rs +++ b/api/src/v2/behavior_traits/ops.rs @@ -1,7 +1,4 @@ -use std::{ - cmp::Ordering, - ops::{Div, Mul}, -}; +use std::cmp::Ordering; use super::{convert::ToScalar, ucum::Dimensionable}; @@ -103,7 +100,8 @@ pub trait DivRef { fn div_ref(&self, rhs: &Rhs) -> O; } -pub trait TryDivRef<'a, Rhs = Self, O = Self>: Sized + Div<&'a Self, Output = O> + 'a { +// pub trait TryDivRef<'a, Rhs = Self, O = Self>: Sized + Div<&'a Self, Output = O> + 'a { +pub trait TryDivRef { type Error; /// # Errors @@ -117,7 +115,7 @@ pub trait TryDivRef<'a, Rhs = Self, O = Self>: Sized + Div<&'a Self, Output = O> /// This trait mainly exists to provide a trait bounds for other traits like /// `v2::type_traits::Measurement`, where you _should_ have some sdfasdlfkjas;dlfkjas;dflj /// - fn try_div_ref(&'a self, rhs: &'a Rhs) -> Result; + fn try_div_ref(&self, rhs: &Rhs) -> Result; } pub trait CheckedDivRef: Sized { @@ -134,7 +132,8 @@ pub trait MulRef { fn mul_ref(&self, rhs: &Rhs) -> O; } -pub trait TryMulRef<'a, Rhs = Self, O = Self>: Sized + Mul<&'a Self, Output = O> + 'a { +// pub trait TryMulRef<'a, Rhs = Self, O = Self>: Sized + Mul<&'a Self, Output = O> + 'a { +pub trait TryMulRef { type Error; /// # Errors @@ -148,7 +147,7 @@ pub trait TryMulRef<'a, Rhs = Self, O = Self>: Sized + Mul<&'a Self, Output = O> /// This trait mainly exists to provide a trait bounds for other traits like /// `v2::type_traits::Measurement`, where you _should_ have some an implementation /// - fn try_mul_ref(&'a self, rhs: &'a Rhs) -> Result; + fn try_mul_ref(&self, rhs: &Rhs) -> Result; } pub trait CheckedMulRef: Sized { diff --git a/api/src/v2/measurement/convert.rs b/api/src/v2/measurement/convert.rs index 6dbb6492..4b51870e 100644 --- a/api/src/v2/measurement/convert.rs +++ b/api/src/v2/measurement/convert.rs @@ -91,13 +91,12 @@ where //----------------------------------------------------------------------------- // ToMagnitude //----------------------------------------------------------------------------- -impl<'a, S, V, U> ToMagnitude<'a, S> for Measurement +impl ToMagnitude for Measurement where - V: 'a, - &'a V: Mul, - U: ToMagnitude<'a, S>, + V: Copy + Mul, + U: ToMagnitude, { - fn to_magnitude(&'a self) -> S { - Mul::mul(&self.value, self.unit.to_magnitude()) + fn to_magnitude(&self) -> S { + self.value * self.unit.to_magnitude() } } diff --git a/api/src/v2/type_traits/atom.rs b/api/src/v2/type_traits/atom.rs index a08a2e6d..f5d72e15 100644 --- a/api/src/v2/type_traits/atom.rs +++ b/api/src/v2/type_traits/atom.rs @@ -1,12 +1,12 @@ use super::{definition::Definition, dimension::Dimension}; -pub trait Atom<'a, V> { +pub trait Atom { type String; type Names; type Property; type Class; type Dimension: Dimension; - type Definition: Definition<'a, V>; + type Definition: Definition; fn primary_code(&self) -> Self::String; fn secondary_code(&self) -> Option; diff --git a/api/src/v2/type_traits/definition.rs b/api/src/v2/type_traits/definition.rs index 65b69881..46c36bc6 100644 --- a/api/src/v2/type_traits/definition.rs +++ b/api/src/v2/type_traits/definition.rs @@ -1,8 +1,8 @@ use crate::v2::behavior_traits::convert::{ToMagnitude, ToScalar}; -pub trait Definition<'a, V>: ToScalar + ToMagnitude<'a, V> { +pub trait Definition: ToScalar + ToMagnitude { type Unit; - fn value(&'a self) -> &'a V; - fn unit(&'a self) -> Option<&'a Self::Unit>; + fn value(&self) -> &V; + fn unit(&self) -> Option<&Self::Unit>; } diff --git a/api/src/v2/type_traits/measurement.rs b/api/src/v2/type_traits/measurement.rs index 8f3070f3..314d20c8 100644 --- a/api/src/v2/type_traits/measurement.rs +++ b/api/src/v2/type_traits/measurement.rs @@ -6,12 +6,12 @@ pub trait Measurement<'a, V>: Sized + convert::Invert + convert::ToInverse - + convert::ToMagnitude<'a, V> + + convert::ToMagnitude + convert::ToScalar + ops::DimEq + ops::TryAddRef<'a> - + ops::TryDivRef<'a> - + ops::TryMulRef<'a> + + ops::TryDivRef + + ops::TryMulRef + ops::TrySubRef<'a> + unit_conversion::TryConvertTo<'a, Self::Unit> where @@ -19,6 +19,6 @@ where { type Unit: Unit<'a, V>; - fn value(&'a self) -> &'a V; - fn unit(&'a self) -> &'a Self::Unit; + fn value(&self) -> &V; + fn unit(&self) -> &Self::Unit; } diff --git a/api/src/v2/type_traits/prefix.rs b/api/src/v2/type_traits/prefix.rs index 3543122e..ae3c4f0c 100644 --- a/api/src/v2/type_traits/prefix.rs +++ b/api/src/v2/type_traits/prefix.rs @@ -1,10 +1,10 @@ use crate::v2::type_traits; -pub trait Prefix<'a, V> { +pub trait Prefix { type String; type Names; type Class; - type Definition: type_traits::Definition<'a, V>; + type Definition: type_traits::Definition; fn primary_code(&self) -> Self::String; fn secondary_code(&self) -> Option; diff --git a/api/src/v2/type_traits/property.rs b/api/src/v2/type_traits/property.rs index 1e32cf8e..df748873 100644 --- a/api/src/v2/type_traits/property.rs +++ b/api/src/v2/type_traits/property.rs @@ -1,7 +1,7 @@ use super::Atom; -pub trait Property<'a, V> { - type Atom: Atom<'a, V>; +pub trait Property { + type Atom: Atom; fn atoms(&self) -> Vec; } diff --git a/api/src/v2/type_traits/term.rs b/api/src/v2/type_traits/term.rs index 685c1568..7474a30c 100644 --- a/api/src/v2/type_traits/term.rs +++ b/api/src/v2/type_traits/term.rs @@ -7,18 +7,18 @@ pub trait Term<'a, V>: + convert::Invert + convert::ToInverse + convert::ToScalar - + convert::ToMagnitude<'a, V> + + convert::ToMagnitude + ops::DimEq where V: PartialOrd, { - type Prefix: Prefix<'a, V>; - type Atom: Atom<'a, V>; + type Prefix: Prefix; + type Atom: Atom; type Annotation; - fn factor(&'a self) -> Option; - fn prefix_symbol(&'a self) -> Option; - fn atom_symbol(&'a self) -> Option; - fn exponent(&'a self) -> Option; + fn factor(&self) -> Option; + fn prefix_symbol(&self) -> Option; + fn atom_symbol(&self) -> Option; + fn exponent(&self) -> Option; fn annotation(&'a self) -> Option; } diff --git a/api/src/v2/type_traits/unit.rs b/api/src/v2/type_traits/unit.rs index 2320cfd6..fb214bff 100644 --- a/api/src/v2/type_traits/unit.rs +++ b/api/src/v2/type_traits/unit.rs @@ -1,5 +1,3 @@ -use std::borrow::Cow; - use crate::v2::behavior_traits::{convert, ops}; pub trait Unit<'a, V>: @@ -7,11 +5,11 @@ pub trait Unit<'a, V>: + convert::Invert + convert::ToFraction + convert::ToInverse - + convert::ToMagnitude<'a, V> + + convert::ToMagnitude + convert::ToScalar + ops::DimEq - + ops::TryDivRef<'a> - + ops::TryMulRef<'a> + + ops::TryDivRef + + ops::TryMulRef where V: PartialOrd, { @@ -26,12 +24,12 @@ where /// This should error if the `string` can't be parsed into a type that represents that /// combinations of units. /// - fn parse(string: &Self::InputString) -> Result; + fn parse(string: Self::InputString) -> Result; /// This is the string that was either a) parsed to instantiate the object, or b) the canonical /// string that would represent the Unit (if it wasn't instantiated via parsing). /// fn expression(&'a self) -> Cow<'a, Self::Expression>; - fn is_special(&'a self) -> bool; + fn is_special(&self) -> bool; }