Skip to content
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

Implement From trait #58

Open
iliekturtles opened this issue Mar 23, 2018 · 8 comments
Open

Implement From trait #58

iliekturtles opened this issue Mar 23, 2018 · 8 comments

Comments

@iliekturtles
Copy link
Owner

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
Copy link
Owner Author

iliekturtles commented Mar 23, 2018

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
Copy link
Owner Author

@Aehmlo
Copy link
Contributor

Aehmlo commented Apr 21, 2018

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
Copy link
Owner Author

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
Copy link

dunmatt commented May 1, 2019

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

@Aehmlo
Copy link
Contributor

Aehmlo commented May 1, 2019

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

@dunmatt
Copy link

dunmatt commented May 2, 2019

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
Copy link
Contributor

Aehmlo commented May 2, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants