Comments (6)
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.
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.
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.
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.
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.
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)
- Unable to `clamp` quantity to a range. HOT 2
- Unexpected inequality with large conversion factor HOT 6
- Allow subtracting two thermodynamic temperatures HOT 3
- Deserialize references to quantities HOT 2
- New unit `ElectricalSusceptance` HOT 1
- Change of base_unit of Ratio quantity HOT 1
- Conversion to/from complex storage types is wrong
- Quantity-Aware access parts of quantity with complex storage type HOT 2
- Consider rust-specific cache action HOT 1
- Built-In SI system has no alias for Angular Momentum
- Unit conversion problem using BigRational
- Unclear about how to create arbitrary quantities with arbitrary units HOT 6
- Conversion to Watts HOT 1
- Newbie Feedback HOT 1
- New release HOT 1
- NIST Special Publication link
- Add a feature for bytemuck and/or zerocopy to `Quantity`
- Support fixed-point storage type(s)
- Implement clamp for f64 Quantities
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uom.