-
Notifications
You must be signed in to change notification settings - Fork 98
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
Allow defining new units for existing quantities #173
Comments
Good idea. Extracting the unit-specific setup should be possible. Most (all?) of the code is in the following block: Lines 153 to 278 in 3a236e5
The only thing that wouldn't work is the Lines 514 to 517 in 3a236e5
|
For future readers of this question: I managed to define a new unit for an existing quantity by reverse-engineering the For example, defining daltons as a unit of mass with base storage f64 would be done by: // Define daltons for mass (= 1.66053906660e-27 kg (CODATA 2018))
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, Hash)]
pub struct dalton;
impl uom::si::Unit for dalton {
#[inline(always)]
fn abbreviation() -> &'static str {
"Da"
}
#[inline(always)]
fn singular() -> &'static str {
"dalton"
}
#[inline(always)]
fn plural() -> &'static str {
"daltons"
}
}
impl uom::si::mass::Unit for dalton {}
impl uom::Conversion<f64> for dalton {
type T = f64;
#[inline(always)]
fn coefficient() -> Self::T {
1.66053906660_E-27
}
#[inline(always)]
#[allow(unused_variables)]
fn constant(op: uom::ConstantOp) -> Self::T {
match op {
uom::ConstantOp::Add => -0.0,
uom::ConstantOp::Sub => 0.0,
}
}
}
impl uom::si::mass::Conversion<V> for dalton {} This way, you can use the unit interchangeably with any other from @iliekturtles : Would it be possible to define the above in a friendlier user-facing macro? And thank you for this crate, it is a blessing for simulation code! |
@anicusan, extracting the relevant code from I'll take a look at doing this this weekend if no one else wants to work on a PR. |
I've started looking at this and run into the issue that there are multiple unit!(@meter: 1.0; "m", "meter", "meters"); // Setup struct
unit!@impls meter); // Setup impls that are currently defined in `storage_types!` blocks. |
I did some more work on this weekend and think I have it working. I haven't actually pushed the changes yet as I got side tracked on #206 which will make implementing this feature easier. |
Initial PR ready for some review. Documentation in src/unit.rs isn't updated yet and I have more changes to allow the |
@nakedible #209 is now ready for review/testing and I would be eternally grateful if you could take a look. A new I ended up stopping short of making the |
The new `unit!` macro allows for new units to be defined outside of the `quantity!` macro. Units defined using this macro will not be included in the quantity unit enum or associated functions, or in the `FromStr` implementation. Using this macro will create submodules for the underlying storage types that are enabled (e.g. `mod f32`). Resolves #173.
The new `unit!` macro allows for new units to be defined outside of the `quantity!` macro. Units defined using this macro will not be included in the quantity unit enum or associated functions, or in the `FromStr` implementation. Using this macro will create submodules for the underlying storage types that are enabled (e.g. `mod f32`). Resolves #173.
@iliekturtles It's been a while, and I managed to test this. Using the However, if I try to simply say Not sure if this is worth a bug, but I'd prefer being able to import the unit macro without importing anything else. |
Glad it's working! #232 exists for this issue. I just re-worded to better highlight the issue with the nested macros. The original wording made it just seem like an internal documentation issue rather than a problem for end-users. |
I've come up on several cases where a unit I was looking for was missing for a quantity. Most recently, I wanted to use "liters of ideal gas" as an amount of substance unit.
There doesn't seem to be any way to add any units to a quantity definition later on as the units are all defined through the
quantity!
macro. Not sure if is technically feasible as I don't really understand the implementation but something like this would be wonderful:The text was updated successfully, but these errors were encountered: