uom
uom copied to clipboard
Implement From trait
impl<D, Ul, Ur, V> From<Quantity<D, Ur, V>> for Quantity<D, Ul, V> ...
Allow for explicit conversion between quantities with different base units.
May require specialization. May require RFC 1834. I don't think there is a way to constrain typeof(Ul) <> typeof(Ur)
currently. Code ~available in dev-from branch~ below.
impl<D, Ul, Ur, V> $crate::lib::convert::From<Quantity<D, Ur, V>> for Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
Ul: Units<V> + ?Sized,
Ur: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V>,
{
fn from (t: Quantity<D, Ur, V>) -> Quantity<D, Ul, V> {
Quantity {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: change_base::<D, Ul, Ur, V>(&t.value)
}
}
}
error[E0119]: conflicting implementations of trait `std::convert::From<si::Quantity<_, _, _>>` for type `si::Quantity<_, _, _>`:
--> src\system.rs:864:9
|
864 | / impl<D, Ul, Ur, V> $crate::lib::convert::From<Quantity<D, Ur, V>> for Quantity<D, Ul, V>
865 | | where
866 | | D: Dimension + ?Sized,
867 | | Ul: Units<V> + ?Sized,
... |
877 | | }
878 | | }
| |_________^
|
::: src\si\mod.rs
|
10 | / system! {
11 | | /// [International System of Quantities](http://jcgm.bipm.org/vim/en/1.6.html) (ISQ).
12 | | quantities: ISQ {
13 | | length: meter, L;
... |
37 | | }
38 | | }
| |_- in this macro invocation
|
= note: conflicting implementation in crate `core`:
- impl<T> std::convert::From<T> for T;
https://www.reddit.com/r/rust/comments/87l21n/conflicting_implementation_when_implementing_from/
I also would love to see, simple though it is, an impl From<Q> for Duration
, where Q
must be a time (though I'm not sure offhand how to express that with the type system).
impl<U, V> From<Time<U, V>> for Duration
should do it. See https://github.com/iliekturtles/uom/blob/dev-ratio/src/si/ratio.rs#L32 for an example from the dev-ratio
branch.
Definitely agree with the above two comments. Shall I put together a PR?
I'd love that; I totally forgot to implement this! I'd be happy to help if you hit any roadblocks, as well.
I am indeed hitting a roadblock; I don't know a good-enough way to convert a Time into a number of seconds. t.get::<second>()
doesn't do it because second somehow doesn't implement the appropriate Conversion (so it doesn't build).
I’ll take a look at your work later tonight or tomorrow and see if I can’t help out.