-
Notifications
You must be signed in to change notification settings - Fork 101
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
added torque as an alias of energy #124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I did a very quick first pass through the changes and will need to do a more detailed pass this week.
@@ -113,6 +113,25 @@ macro_rules! quantity { | |||
} | |||
} | |||
}; | |||
( | |||
$(#[$quantity_attr:meta])* quantity: $quantity:ident; $description:expr; | |||
alias_of: $alias_of:ident; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why add the alias? In my first brief look at the code it looks like it allows the alias's units to be used?
Quantities with the same dimension and kind are already type aliases although I'm they can't share units currently.
type Energy = Quantity<m^2 * kg * s^-2>;
type Torque = Quantity<m^2 * kg * s^-2>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's so the Conversion
can extend the Unit
of both the original type and the alias. Or to take it at a deeper level it's because "I don't fully understand how this library hangs together, but I had to do this in order to get test
to compile."
src/si/mod.rs
Outdated
@@ -49,6 +49,7 @@ system! { | |||
temperature_interval::TemperatureInterval, | |||
thermodynamic_temperature::ThermodynamicTemperature, | |||
time::Time, | |||
torque_magnitude::TorqueMagnitude, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined towards Torque
instead of TorqueMagnitude
. See Force
as an existing example. Additionally I'd like to get support for mathematical vectors as the underlying storage type some day (e.g. Quantity<V = Vec3f32>
rather than Vec3f32<T = Quantity>
). Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done the renaming. In terms of planning for vectors, I'll spend some time thinking about it and let you know what would be a best case scenario for my uses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I spent some time poking around and it seems like nobody has written the vector library I want, yet. It's very tempting to launch yet another hobby project. What I really want is compile time checking of transform matrix math, to prevent nonsense matrix combinations (ie if Tab transforms a point or vector from a to b, I want Tab*Tbc == Tac
and both Tab*Tcb
and Tbc*Tab
should break the build)... but I don't strictly need them to be quantities, all I really care about is compile time frame of reference checking.
I'm curious though, what's your motivation for wanting a vector quantity (as opposed to a vectory of quantities)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured a Quantity<V = Vec3f32>
would be more natural to use than Vec3<T = Quantity>
. No actual usage experience and I'm not looking to attempt to implement now unless someone actually has a use case.
src/si/torque_magnitude.rs
Outdated
@@ -0,0 +1,155 @@ | |||
//! Torque (aka moment of force) (base unit newton meter, kg<sup>1</sup> · m<sup>2</sup> · |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List base quantities in the same order as specified in the system!
macro: length, mass, time, .... Superscript isn't necessary when equal to 1
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall I update Force as well? (that's where I copy pasted from)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added #127 regarding units. Reply there / submit a new PR if you're interested. Otherwise I'll do in the next few days. We'll also want to make kg superscript consistent with that issue. Leaning towards leaving the 1 right now.
Changes in |
Finally got to look at this with the compiler and I understand what is happening now. A short-term solution could be to revert the Another possible solution would be to change the error[E0119]: conflicting implementations of trait `std::str::FromStr` for type `si::Quantity<(dyn si::D
imension<M=typenum::PInt<typenum::UInt<typenum::UTerm, typenum::B1>>, J=typenum::Z0, L=typenum::PInt<typ
enum::UInt<typenum::UInt<typenum::UTerm, typenum::B1>, typenum::B0>>, T=typenum::NInt<typenum::UInt<type
num::UInt<typenum::UTerm, typenum::B1>, typenum::B0>>, Th=typenum::Z0, N=typenum::Z0, Kind=(dyn Kind + '
static), I=typenum::Z0> + 'static), _, f32>`:
--> src\quantity.rs:348:17
|
348 | impl<U> FromStr for super::super::$quantity<U, V>
| _________________^
| |_________________|
| |
349 | | where
350 | | U: super::super::super::Units<V> + ?Sized,
351 | | {
... |
364 | | }
365 | | }
| | ^
| |_________________|
| |_________________first implementation here
| conflicting implementation for `si::Quantity<(dyn si::Dimension<M=typenum::PInt<
typenum::UInt<typenum::UTerm, typenum::B1>>, J=typenum::Z0, L=typenum::PInt<typenum::UInt<typenum::UInt<
typenum::UTerm, typenum::B1>, typenum::B0>>, T=typenum::NInt<typenum::UInt<typenum::UInt<typenum::UTerm,
typenum::B1>, typenum::B0>>, Th=typenum::Z0, N=typenum::Z0, Kind=(dyn Kind + 'static), I=typenum::Z0> +
'static), _, f32>`
|
::: src\si\torque.rs:3:1
|
3 | / quantity! {
4 | | /// Torque magnitude (base unit newton meter, kg<sup>1</sup> · m<sup>2</sup> · s<sup>-2</sup
>).
5 | | ///
6 | | /// Torques are moments, which means they inherently depend on a distance to a frame of
... |
75 | | }
76 | | }
| |_- in this macro invocation |
At least for my edification, what's the problem with the way it's implemented now? Just that things can't be parsed into torques? |
With the current We need to find some way to separate |
How would you feel about a blanket From implementation that can switch between |
A blanket implementation probably makes sense. I've been thinking about this a little bit today and I'm not sure if it makes sense to ever have different quantities, even if they are the same dimension to be strict aliases of themselves. let e: Energy = ...;
energy_fn(e);
torque_fn(e);
fn energy_fn(e: Energy) {}
fn torque_fn(t: Torque) {} The above works without any warnings and could easily lead to mixing up quantities of the same dimension. A blanket implementation would at least require a My thoughts aren't fully formed yet, but likely more design effort needs to be put into what is called kind right now. |
I'm working at the edge of my skill here, can you sanity check this?
Edit: I'm running into some problems due to the lack of things being
Any ideas / suggestions? Why does uom use |
Sat down to look at this and realized a blanket impl isn't possible. See #58. However an explicit #[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(all(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::f32::*;
/// # use uom::si::thermodynamic_temperature::kelvin;
/// let tt: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(1.0);
/// let ti: TemperatureInterval = tt.iinto();
/// //let f: Frequency = ti.into();
/// ```
#[inline(always)]
pub fn iinto<Dr, Ur>(self) -> Quantity<Dr, Ur, V>
where
$(D::$symbol: $crate::lib::ops::IsEqual<Dr::$symbol>,)+
Dr: Dimension + ?Sized,
Ur: Units<V> + ?Sized,
{
Quantity {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: change_base::<Dr, U, Ur, V>(&self.value),
}
} |
I don't understand what #58 has to do with this change. My From impl above compiles just fine, the problems only arise at the call site because the compiler doesn't know what size anything is. Can you help me understand the core of the issue? Is there a way I could solve the specific problem to allow the Torque tests to work so that I can get those in (and start using them) and worry about the more general case later? |
Regarding #58 I was thinking about a blanket impl in the You should also add two base units parameters ( I just skimmed the code so far but will plan to look more in depth tomorrow evening. |
Ok, so adding As to your Ul and Ur idea. I don't think it's a good idea to allow converting both of those things as once, if I'm using |
I was afraid you would get the same conflicting implementation error. Explicitly specifying For changing base units |
…turtles#117 (and finishes the request in iliekturtles#114).
After a lot of experimentation I'm coming around to the idea that Moments are more trouble than they're worth. So in that vein, I'm following your suggestion in #117 to not use a different
Kind
for torque, and to add it as a quantity.To get that to work I had to refactor the quantity macro a bit to split the parts that must be repeated between aliases from the parts that cannot be. I'm rather new to macros (I still find them very hard to read), so please bear with me if it takes some back and forth to clean these changes up.