I'm trying to implement some conversion between generics with different parameters. The "converter" can operate both ways at once, but I can't make Rust understand it. Consider the following code, with the Pair being the "converter":
use std::marker::PhantomData;
#[derive(Copy, Clone)]
struct Pair<T1: Copy, T2: Copy>(PhantomData<(T1, T2)>);
struct Data<T: Copy>(PhantomData<T>);
impl<T: Copy> Data<T> {
fn remake<T2: Copy>(self, _: Pair<T, T2>) -> Data<T2> {
Data(PhantomData)
}
}
fn main() {
let pair: Pair<u32, &str> = Pair(PhantomData);
let data: Data<u32> = Data(PhantomData);
let data2 = data.remake(pair);
// let _ = data2.remake(pair); - error, type mismatch
}
It it possible somehow to make the last line compile, too? Of course, I can add something like this:
impl<T1: Copy, T2: Copy> Pair<T1, T2> {
pub fn swap(self) -> Pair<T2, T1> {
Pair(PhantomData)
}
}
And use data2.remake(pair.swap()). But is it possible without an additional explicit call?
I'd prefer something that works on stable (since on nightly, it seems, I can work this around playing with Fn traits somehow, but this looks like something too complex).