Skip to content

Commit

Permalink
Remove lifetimes from some traits
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed Oct 10, 2023
1 parent a982733 commit c2ed1b2
Show file tree
Hide file tree
Showing 23 changed files with 69 additions and 73 deletions.
4 changes: 2 additions & 2 deletions api/src/measurement/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion api/src/measurement/v2/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl convert::ToScalar<f64> for Measurement {
}
}

impl convert::ToMagnitude<'_, f64> for Measurement {
impl convert::ToMagnitude<f64> for Measurement {
fn to_magnitude(&self) -> f64 {
// Just delegate to the old trait impl for now.
crate::UcumUnit::magnitude(self)
Expand Down
4 changes: 2 additions & 2 deletions api/src/measurement/v2/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Error> {
Expand Down Expand Up @@ -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<Self, Self::Error> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/parser/atom/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{

use super::Atom;

impl<'a> type_traits::Atom<'a, f64> for Atom {
impl type_traits::Atom<f64> for Atom {
type String = &'static str;
type Names = Vec<&'static str>;
type Property = crate::Property;
Expand Down
10 changes: 5 additions & 5 deletions api/src/parser/definition/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64> for Definition {
type Unit = Vec<crate::Term>;

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())
}
}
Expand All @@ -20,8 +20,8 @@ impl convert::ToScalar<f64> for Definition {
}
}

impl<'a> convert::ToMagnitude<'a, f64> for Definition {
fn to_magnitude(&'a self) -> f64 {
impl convert::ToMagnitude<f64> for Definition {
fn to_magnitude(&self) -> f64 {
self.terms.to_magnitude()
}
}
2 changes: 1 addition & 1 deletion api/src/parser/prefix/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
UcumSymbol,
};

impl<'a> TPrefix<'a, f64> for Prefix {
impl TPrefix<f64> for Prefix {
type String = &'static str;
type Names = Vec<&'static str>;
type Class = Classification;
Expand Down
2 changes: 1 addition & 1 deletion api/src/parser/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//-----------------------------------------------------------------------------

#[cfg(feature = "v2")]
impl<'a> crate::v2::type_traits::Property<'a, f64> for Property {
impl crate::v2::type_traits::Property<f64> for Property {
type Atom = Atom;

fn atoms(&self) -> Vec<Self::Atom> {
Expand Down
8 changes: 4 additions & 4 deletions api/src/parser/term/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ impl<'a> TTerm<'a, f64> for Term {
type Atom = Atom;
type Annotation = &'a str;

fn factor(&'a self) -> Option<u32> {
fn factor(&self) -> Option<u32> {
self.factor
}

fn prefix_symbol(&'a self) -> Option<Self::Prefix> {
fn prefix_symbol(&self) -> Option<Self::Prefix> {
self.prefix
}

fn atom_symbol(&'a self) -> Option<Self::Atom> {
fn atom_symbol(&self) -> Option<Self::Atom> {
self.atom
}

fn exponent(&'a self) -> Option<i32> {
fn exponent(&self) -> Option<i32> {
self.exponent
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/parser/term/v2/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl convert::ToScalar<f64> for Term {
}
}

impl convert::ToMagnitude<'_, f64> for Term {
impl convert::ToMagnitude<f64> for Term {
fn to_magnitude(&self) -> f64 {
// Just delegate to existing impl for now.
crate::UcumUnit::magnitude(self)
Expand Down
4 changes: 2 additions & 2 deletions api/src/parser/terms/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl convert::ToScalar<f64> for Vec<Term> {
}
}

impl<'a> convert::ToMagnitude<'a, f64> for Vec<Term> {
fn to_magnitude(&'a self) -> f64 {
impl convert::ToMagnitude<f64> for Vec<Term> {
fn to_magnitude(&self) -> f64 {
self.iter()
.map(convert::ToMagnitude::to_magnitude)
.product()
Expand Down
12 changes: 6 additions & 6 deletions api/src/unit/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::ParseError> {
fn parse(string: Self::InputString) -> Result<Self, Self::ParseError> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion api/src/unit/v2/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl convert::ToScalar<f64> for Unit {
}
}

impl convert::ToMagnitude<'_, f64> for Unit {
impl convert::ToMagnitude<f64> for Unit {
fn to_magnitude(&self) -> f64 {
// Just delegate to the old trait impl for now.
crate::UcumUnit::magnitude(self)
Expand Down
4 changes: 2 additions & 2 deletions api/src/unit/v2/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Error> {
Expand All @@ -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<Self, Self::Error> {
Expand Down
4 changes: 2 additions & 2 deletions api/src/v2/behavior_traits/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub trait ToScalar<V> {
fn to_scalar(&self) -> V;
}

pub trait ToMagnitude<'a, T> {
fn to_magnitude(&'a self) -> T;
pub trait ToMagnitude<T> {
fn to_magnitude(&self) -> T;
}

// NOTE: This is the next version of `AsFraction`, which was incorrectly named, according to Rust
Expand Down
15 changes: 7 additions & 8 deletions api/src/v2/behavior_traits/ops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
cmp::Ordering,
ops::{Div, Mul},
};
use std::cmp::Ordering;

use super::{convert::ToScalar, ucum::Dimensionable};

Expand Down Expand Up @@ -103,7 +100,8 @@ pub trait DivRef<Rhs = Self, O = Self> {
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<Rhs = Self, O = Self> {
type Error;

/// # Errors
Expand All @@ -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<O, Self::Error>;
fn try_div_ref(&self, rhs: &Rhs) -> Result<O, Self::Error>;
}

pub trait CheckedDivRef<Rhs = Self, O = Self>: Sized {
Expand All @@ -134,7 +132,8 @@ pub trait MulRef<Rhs = Self, O = Self> {
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<Rhs = Self, O = Self> {
type Error;

/// # Errors
Expand All @@ -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<O, Self::Error>;
fn try_mul_ref(&self, rhs: &Rhs) -> Result<O, Self::Error>;
}

pub trait CheckedMulRef<Rhs = Self, O = Self>: Sized {
Expand Down
11 changes: 5 additions & 6 deletions api/src/v2/measurement/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ where
//-----------------------------------------------------------------------------
// ToMagnitude
//-----------------------------------------------------------------------------
impl<'a, S, V, U> ToMagnitude<'a, S> for Measurement<V, U>
impl<S, V, U> ToMagnitude<S> for Measurement<V, U>
where
V: 'a,
&'a V: Mul<S, Output = S>,
U: ToMagnitude<'a, S>,
V: Copy + Mul<S, Output = S>,
U: ToMagnitude<S>,
{
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()
}
}
4 changes: 2 additions & 2 deletions api/src/v2/type_traits/atom.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{definition::Definition, dimension::Dimension};

pub trait Atom<'a, V> {
pub trait Atom<V> {
type String;
type Names;
type Property;
type Class;
type Dimension: Dimension;
type Definition: Definition<'a, V>;
type Definition: Definition<V>;

fn primary_code(&self) -> Self::String;
fn secondary_code(&self) -> Option<Self::String>;
Expand Down
6 changes: 3 additions & 3 deletions api/src/v2/type_traits/definition.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::v2::behavior_traits::convert::{ToMagnitude, ToScalar};

pub trait Definition<'a, V>: ToScalar<V> + ToMagnitude<'a, V> {
pub trait Definition<V>: ToScalar<V> + ToMagnitude<V> {
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>;
}
10 changes: 5 additions & 5 deletions api/src/v2/type_traits/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ pub trait Measurement<'a, V>:
Sized
+ convert::Invert
+ convert::ToInverse
+ convert::ToMagnitude<'a, V>
+ convert::ToMagnitude<V>
+ convert::ToScalar<V>
+ 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
V: PartialOrd,
{
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;
}
4 changes: 2 additions & 2 deletions api/src/v2/type_traits/prefix.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::v2::type_traits;

pub trait Prefix<'a, V> {
pub trait Prefix<V> {
type String;
type Names;
type Class;
type Definition: type_traits::Definition<'a, V>;
type Definition: type_traits::Definition<V>;

fn primary_code(&self) -> Self::String;
fn secondary_code(&self) -> Option<Self::String>;
Expand Down
4 changes: 2 additions & 2 deletions api/src/v2/type_traits/property.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Atom;

pub trait Property<'a, V> {
type Atom: Atom<'a, V>;
pub trait Property<V> {
type Atom: Atom<V>;

fn atoms(&self) -> Vec<Self::Atom>;
}
14 changes: 7 additions & 7 deletions api/src/v2/type_traits/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ pub trait Term<'a, V>:
+ convert::Invert
+ convert::ToInverse
+ convert::ToScalar<V>
+ convert::ToMagnitude<'a, V>
+ convert::ToMagnitude<V>
+ ops::DimEq
where
V: PartialOrd,
{
type Prefix: Prefix<'a, V>;
type Atom: Atom<'a, V>;
type Prefix: Prefix<V>;
type Atom: Atom<V>;
type Annotation;

fn factor(&'a self) -> Option<u32>;
fn prefix_symbol(&'a self) -> Option<Self::Prefix>;
fn atom_symbol(&'a self) -> Option<Self::Atom>;
fn exponent(&'a self) -> Option<i32>;
fn factor(&self) -> Option<u32>;
fn prefix_symbol(&self) -> Option<Self::Prefix>;
fn atom_symbol(&self) -> Option<Self::Atom>;
fn exponent(&self) -> Option<i32>;
fn annotation(&'a self) -> Option<Self::Annotation>;
}
Loading

0 comments on commit c2ed1b2

Please sign in to comment.