How to implement trait for uom::si:Quantity

Hi I would like to use the Battery crate to read info about battery, if I try to multiply battery.state_of_battery() I get this error:
^ no implementation for uom::si::Quantity<dyn uom::si::Dimension<I = typenum::int::Z0, M = typenum::int::Z0, L = typenum::int::Z0, Th = typenum::int::Z0, J = typenum::int::Z0, N = typenum::int::Z0, Kind = (dyn uom::Kind + 'static), T = typenum::int::Z0>, dyn uom::si::Units<f32, length = uom::si::length::meter, thermodynamic_temperature = battery::units::thermodynamic_temperature::kelvin, electric_current = battery::units::electric_current::ampere, amount_of_substance = uom::si::amount_of_substance::mole, luminous_intensity = uom::si::luminous_intensity::candela, time = battery::units::time::second, mass = uom::si::mass::kilogram>, f32> * {integer}

You probably want to multiply a Quantity<_, f32> * f32 not Quantity<_, f32> * {integer}. I'm gussing you have somethng like some_value * 10 (or some other integer literal), you need some_value * 10.0 (or some other float literal)

1 Like

yes exactly, I do not know how to do it, is my first attempt in rust, thanks in advance

Could you share more code? I can't help more with just a partial error message :slight_smile:

Please follow the Forum Code Formatting and Syntax Highlighting guidelines when doing so

You could write some_value * 10.0 instead of some_value * 10. The decimal point tells Rust that you've written a floating point number and not an integer.

as far it is only a test, as you can see I tried to implement Mul, without success

    let manager = battery::Manager::new()?;
    //let percentage =

 //impl Mul for uom::si::Quantity{
    // fn Mul(fattore:i32){
    //     println!("moltiplico per {}",fattore );

    for (idx, maybe_battery) in manager.batteries()?.enumerate() {
        let battery = maybe_battery?;
        println!("Battery #{}:", idx);
        println!("Vendor: {:?}", battery.vendor());
        println!("Model: {:?}", battery.model());
        println!("State: {:?}", battery.state());
        println!("Time to full charge: {:?}", battery.time_to_full());
        println!("percentuale di carica: {:?}",battery.state_of_charge()*10);
        //if battery.state_of_charge()>0.5{
        //    println!("piรน del 50%")




Replace this with

println!("percentuale di carica: {:?}",battery.state_of_charge() * 10.0);

(note the number format).

1 Like

thanks it works

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.