Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedFloat does not implement Mul #197

Open
robfitzgerald opened this issue Apr 30, 2024 · 1 comment
Open

OrderedFloat does not implement Mul #197

robfitzgerald opened this issue Apr 30, 2024 · 1 comment
Assignees
Labels
bug Something isn't working rust Applies to the rust code

Comments

@robfitzgerald
Copy link
Collaborator

While deriving derive_more numeric traits for Cost (or other types that wrap OrderedFloat) compiles, when we attempt to use the methods, we get a failure:

let x = Cost::ZERO * Cost::ONE;
cannot multiply `ordered_float::OrderedFloat<f64>` by `model::unit::cost::Cost`
the trait `std::ops::Mul<model::unit::cost::Cost>` is not implemented for `ordered_float::OrderedFloat<f64>`
the following other types implement trait `std::ops::Mul<Rhs>`:
  <ordered_float::OrderedFloat<T> as std::ops::Mul>
  <ordered_float::OrderedFloat<T> as std::ops::Mul<T>>
  <ordered_float::OrderedFloat<T> as std::ops::Mul<&'a T>>
  <ordered_float::OrderedFloat<T> as std::ops::Mul<&'a ordered_float::OrderedFloat<T>>>
  <&'a ordered_float::OrderedFloat<T> as std::ops::Mul<ordered_float::OrderedFloat<T>>>
  <&'a ordered_float::OrderedFloat<T> as std::ops::Mul<T>>
  <&'a ordered_float::OrderedFloat<T> as std::ops::Mul<&'a T>>
1 redundant requirement hidden
required for `model::unit::cost::Cost` to implement `std::ops::Mul`
@robfitzgerald robfitzgerald added the bug Something isn't working label Apr 30, 2024
@nreinicke nreinicke added the rust Applies to the rust code label May 18, 2024
@kylecarow kylecarow self-assigned this May 20, 2024
@kylecarow
Copy link
Collaborator

kylecarow commented May 20, 2024

@nreinicke This issue seems to be relevant: JelteF/derive_more#168

Adding #[mul(forward)] to InternalFloat creates an impl for e.g. InternalFloat::ZERO * InternalFloat::ONE;, but does not generate an impl for InternalFloat::ZERO * 1.0; removing that attribute does the opposite.

Until that issue is resolved, it seems like we either keep it as is, or, as the compiler suggests, add .into() calls wherever we have internal floats multiplied by primitive floats (gross):

error[E0308]: mismatched types
  --> routee-compass-core/src/model/unit/weight_unit.rs:24:44
   |
24 |             (S::Kg, S::Pounds) => *value * 2.20462,
   |                                            ^^^^^^^ expected `InternalFloat`, found floating-point number
   |
help: call `Into::into` on this expression to convert `{float}` into `unit::internal_float::InternalFloat`
   |
24 |             (S::Kg, S::Pounds) => *value * 2.20462.into(),
   |                                                   +++++++

Leaving this here for now, but it should still be possible to get the contained primitive float out of one of the objects and use that, i.e. by sequential .0 accesses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rust Applies to the rust code
Projects
None yet
Development

No branches or pull requests

3 participants