Giter Site home page Giter Site logo

Comments (6)

pos-ki avatar pos-ki commented on July 18, 2024

I am trying to make the workaround solution in the meantime by basically combining two of the example files - creating a new unit and using it to define a different set of base units.

I currently have the following draft test code:

#[macro_use]
extern crate uom;

use uom::fmt::DisplayStyle::*;
use uom::si::f64::*;
use uom::si::length::meter;

unit! {
    system: uom::si;
    quantity: uom::si::length;

    @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
}

mod solar_system_units {
    use crate::au;

    ISQ!(
        uom::si,
        f64,
        (au, kilogram, day, ampere, kelvin, mole, candela)
    );
}

fn main() {
    let l1 = Length::new::<meter>(1000.0);
    let l2 = Length::new::<au>(1.0);

    println!(
        "{} = {}",
        l1.into_format_args(meter, Abbreviation),
        l1.into_format_args(au, Abbreviation)
    );
    println!(
        "{} = {}",
        l2.into_format_args(au, Abbreviation),
        l2.into_format_args(meter, Abbreviation)
    );
}

which produces a rather confusing compiler error, telling me to import crate::au which is already imported on that very line, while simultaneously telling me that this import is unused.

error[E0412]: cannot find type `au` in module `__system::length`
  --> src/main.rs:21:10
   |
21 |         (au, kilogram, day, ampere, kelvin, mole, candela)
   |          ^^ not found in `__system::length`
   |
help: consider importing this struct
   |
16 +     use crate::au;
   |

warning: unused import: `crate::au`
  --> src/main.rs:16:9
   |
16 |     use crate::au;
   |         ^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0412`.

I am unfortunately not too familiar with macros yet, so I am not sure whether what I am trying to do is even remotely supposed to work.

from uom.

yacinelakel avatar yacinelakel commented on July 18, 2024

If all your doing is extending an existing quantity with units, then you don't need to create a new set of quantities. Deleting the solar_system_units mod should make your example code compile.

For ease of use you could create a custom si module to access your custom units with the same import as base units:

mod si {
  pub use uom::si::*;
  
  pub mod length {
      pub use uom::si::length::*;
      
      unit! {
         system: uom::si;
         quantity: uom::si::length;

        @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
       }
   }
}
      

from uom.

pos-ki avatar pos-ki commented on July 18, 2024

My impression was that I had to create a new set of quantities to adjust the units in which each quantity is stored under the hood. My goal is to store distances as AU, mass as zettagrams (did not give that in the example, but have added it since), and time as days, in order to maintain best precision with large values.

ISQ!(uom::si, f64, (au, zettagram, day, ampere, kelvin, mole, candela)

While also redefining the AU as in the example. To achieve all of this, I believe I have to create the new set of quantities, right?

from uom.

yacinelakel avatar yacinelakel commented on July 18, 2024

Try this:

#[macro_use]
extern crate uom;

use uom::fmt::DisplayStyle::*;

mod si {
    pub use uom::si::*;
    
    pub mod length {
        pub use uom::si::length::*;
        
        unit! {
           system: uom::si;
           quantity: uom::si::length;
  
          @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
         }
     }
  }


mod custom_base {
    ISQ!(super::si, f64, (au, zettagram, day, ampere, kelvin, mole, candela));
}



fn main() {
    let l1 = custom_base::Length::new::<si::length::meter>(1000.0);
    let l2 = custom_base::Length::new::<si::length::au>(1.0);

    println!(
        "{} = {}",
        l1.into_format_args(si::length::meter, Abbreviation),
        l1.into_format_args(si::length::au, Abbreviation)
    );
    println!(
        "{} = {}",
        l2.into_format_args(si::length::au, Abbreviation),
        l2.into_format_args(si::length::meter, Abbreviation)
    );
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn base_unit_of_length_should_be_au() {
        let l = custom_base::Length::new::<si::length::au>(1.0);

        assert_eq!(l.value, 1.0);
    }

    #[test]
    fn base_unit_of_time_should_be_day() {
        let t_base = custom_base::Time::new::<si::time::day>(1.0);
        let t_year = custom_base::Time::new::<si::time::year>(1.0);

        assert_eq!(t_base.value, 1.0);

        assert_eq!(t_year.value, 365.0);
    }
}

from uom.

yacinelakel avatar yacinelakel commented on July 18, 2024

Just for more context, you don't need to create a new set of qunatities, i.e like the mks example, but create an alias to the ISQ system using the ISQ! macro and changing the base units, i.e like the base example.

If you want to pack all this into one module, then you can do the following:

// Custom system of units
mod csi {

    ISQ!(si, f64, (au, zettagram, day, ampere, kelvin, mole, candela));

    mod si {
        pub use uom::si::*;

        pub mod length {
            pub use uom::si::length::*;

            unit! {
              system: uom::si;
              quantity: uom::si::length;

             @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
            }
        }
    }

    pub use si::*;
}

fn main() {
    let l1 = csi::Length::new::<csi::length::meter>(1000.0);
    let l2 = csi::Length::new::<csi::length::au>(1.0);

    println!(
        "{} = {}",
        l1.into_format_args(csi::length::meter, Abbreviation),
        l1.into_format_args(csi::length::au, Abbreviation)
    );
    println!(
        "{} = {}",
        l2.into_format_args(csi::length::au, Abbreviation),
        l2.into_format_args(csi::length::meter, Abbreviation)
    );
}

from uom.

pos-ki avatar pos-ki commented on July 18, 2024

Okay, thanks, the workaround works like that! Still hope for the AU abbreviation and the exact value to be adjusted, though.

from uom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.