Below I will describe the problem. Question is, exists there a way to solve it (or how to do it).
I would like to have the Point type for i32 or f32 coordinates:
struct Point<T> {
x: T,
y: T,
};
where T is i32 or f32.
And now i would like to have something like this:
From <Point<T>> for Point<U> { // Point<i32> -> Point<f32>
...
}
and
From <Point<U>> for Point<T> {. // Point<f32> -> Point<i32>
...
}
Is there a way to solve it (or how to do it)?
piotrpsz
if you know you want only i32 and f32, you should just write them both.
note that
impl<T,U> From <Point<T>> for Point<U> and impl<T,U> From <Point<U>> for Point<T> are the exact same thing.
impl From <Point<i32>> for Point<f32> {
// TODO
}
impl From <Point<f32>> for Point<i32> {
// TODO
}
I would also consider restricting the types, if necessary:
trait Coordinate {}
impl Coordinate for i32 {}
impl Coordinate for f32 {}
struct Point<T: Coordinate> {
x: T,
y: T,
}
impl From<Point<f32>> for Point<i32> {
fn from(value: Point<f32>) -> Self {
todo!()
}
}
impl From<Point<i32>> for Point<f32> {
fn from(value: Point<i32>) -> Self {
todo!()
}
}
Bingo. This is exactly what I was looking for.
Thank you so much. You're 'marlez' greate 
piotrpsz
either
trait NumericConversion<T>{
fn from(surce:T)->Self;
}
impl NumericConversion<i32> for f32{
.....
}
impl NumericConversion<f32> for i32{
.....
}
.... //other conversion you want to support
or
trait CoordType{
fn from_f32(surce:f32)->Self;
fn to_f32(self)->f32;
}
impl CoordType for f32{
.....
}
impl CoordType for i32{
.....
}
....//other types you want to support
then you use the traits for the conversion of the whole object
it is strongly advised not to put trait bounds on struct definition, as they will infect everything else and lead to a lot of noise.
bounds belong on impls