Can you specify where T1!=T2

I have about 12 types representing time and I want to be able to convert between any of them. To avoid the n-squared problem I will be going through an interim type designed to avoid loss of precision.

I can define From back and forth to the interim types.

But I can't seem to generically define how to go from any of them to any other using that pathway. This doesn't work:

impl<T1: Time, T2: Time> From<T1> for T2 {
  fn from(t1: T1) -> T2 {
    let x: Interim = t1.into();
    From::from(x)
  }
}

Because T1 could equal T2 and that collides with

impl<T> std::convert::From<T> for T

in the standard library. And I dont know how to write "T1 != T2" in the where clause.

Also, 'error[E0210]: type parameter T2 must be used as the type parameter for some local type (e.g., MyStruct<T2>)'

No, it's not possible.

Instead, you can define your own method on the trait Time itself.

fn convert<T: Time>(self) -> T {
    Interim::from(self).into()
}
2 Likes

Thanks Heyonu.
I guess negative trait bounds have too many problems: https://github.com/rust-lang/rust/issues/42721
I might yet try this hack: https://github.com/rust-lang/rust/issues/42721#issuecomment-322095493

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.