Hi Rustaceans,
I'm trying to implement (for learning purposes) 2D vector in homogeneous coordinates.
#[derive(Debug, Copy, Clone)]
pub struct Vector2D<T> {
pub x: T,
pub y: T,
pub w: T, // 0 or 1. 0 = Non translatable.
}
I'm trying to provide a a new() method with only 2 parameters, as w, can only take two values (0 or 1). I want new(..,..) to build a 2D vector with x,y, and 1.
For this I created an Identity trait:
pub trait Identity {
type Output;
fn one() -> Self::Output;
}
I implement this trait for few basic types:
impl Identity for f64 {
type Output = f64;
fn one() -> Self::Output {
1.0_f64
}
}
Now my problem arise. When I try to implement my new() method:
impl<T> Vector2D<T> where
T: Identity<Output=T>
{
// Create Vector from values.
pub const fn new(x1: T, y1: T) -> Self {
Vector2D { x: x1, y: y1, w: Identity::one() }
}
}
I got the following error:
--> src/vector.rs:110:37
|
110 | Vector2D { x: x1, y: y1, w: Identity::one() }
| ^^^^^^^^^^^^^ cannot infer type
|
= note: cannot satisfy `_: Identity`
I cannot figure out how to use Identity::one() as parameter. I also don´t understand the note. Can anyone explain me what I'm doing wrong ?
Regards