iliekturtles / uom

Units of measurement -- type-safe zero-cost dimensional analysis
Apache License 2.0
1.03k stars 98 forks source link

Unable to `clamp` quantity to a range. #440

Closed DJDuque closed 1 year ago

DJDuque commented 1 year ago

I am not sure if this is the expected behavior, but the following fails for me (I would expect it to work):

use uom::si::angle::radian;
use uom::si::f64::Angle;

fn main() {
    let x = Angle::new::<radian>(5.0);
    let clamped = x.clamp(-Angle::HALF_TURN, Angle::HALF_TURN);
}

I am not very familiar with the code under the hood, but looking here it looks like this should work (?).

DJDuque commented 1 year ago

Ok, I just noticed that what I linked requires V (in this case f64) to implement the Ord trait. Now I understand why it doesn't work.

Maybe it is worth adding this as a separate method (?) given that the clamp method does exist for f64. Maybe it could be useful for some people

iliekturtles commented 1 year ago

Closing as a duplicate of #239. There is a draft PR in #377 but it hasn't gotten past the draft stage unfortunately.