iliekturtles / uom

Units of measurement -- type-safe zero-cost dimensional analysis
Apache License 2.0
1.01k stars 91 forks source link

Implement From trait #58

Open iliekturtles opened 6 years ago

iliekturtles commented 6 years ago

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.

iliekturtles commented 6 years ago

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;
iliekturtles commented 6 years ago

https://www.reddit.com/r/rust/comments/87l21n/conflicting_implementation_when_implementing_from/

Aehmlo commented 6 years ago

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).

iliekturtles commented 6 years ago

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.

dunmatt commented 5 years ago

Definitely agree with the above two comments. Shall I put together a PR?

Aehmlo commented 5 years ago

I'd love that; I totally forgot to implement this! I'd be happy to help if you hit any roadblocks, as well.

dunmatt commented 5 years ago

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).

Aehmlo commented 5 years ago

I’ll take a look at your work later tonight or tomorrow and see if I can’t help out.