From cef64bbd451b0945f6fc055b13d7afb7bf0ea15b Mon Sep 17 00:00:00 2001 From: Steve Loveless Date: Tue, 14 Nov 2023 16:35:56 -0800 Subject: [PATCH] Add TryToScalar, TryToMagnitude traits --- api/src/v2/behavior_traits/convert.rs | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/api/src/v2/behavior_traits/convert.rs b/api/src/v2/behavior_traits/convert.rs index efae7036..c4e21949 100644 --- a/api/src/v2/behavior_traits/convert.rs +++ b/api/src/v2/behavior_traits/convert.rs @@ -31,8 +31,32 @@ pub trait ToScalar { fn to_scalar(&self) -> V; } -pub trait ToMagnitude { - fn to_magnitude(&self) -> T; +pub trait TryToScalar { + type Error; + + /// # Errors + /// + /// An error returned here should indicate that `self` could not be coerced to a `V`. One + /// example of this would be converting a `f64` to a `num_rational::Rational`, where + /// `f64::INFINITY`, `f64::NEG_INFINITY`, and `f64::NAN` can't be converted. + /// + fn try_to_scalar(&self) -> Result; +} + +pub trait ToMagnitude { + fn to_magnitude(&self) -> V; +} + +pub trait TryToMagnitude { + type Error; + + /// # Errors + /// + /// An error returned here should indicate that `self` could not be coerced to a `V`. One + /// example of this would be converting a `f64` to a `num_rational::Rational`, where + /// `f64::INFINITY`, `f64::NEG_INFINITY`, and `f64::NAN` can't be converted. + /// + fn try_to_magnitude(&self) -> Result; } // NOTE: This is the next version of `AsFraction`, which was incorrectly named, according to Rust