Impl trait method on generic and vec of generic within a struct

52 views Asked by At

I'm trying to implement the approx crate traits for a struct and it's fine with the values that are f64, but I don't know how to implement for the Vec values.

The struct is

pub struct Body<T> {
    pub id: u8,
    pub label: String,
    pub mass: T,
    pub pos: Vec<T>,
    pub vel: Vec<T>,
    pub obj_col: T,
}

I've used generics in the struct as the crate instructions use generics, but everywhere that is T I know will be f64.

An example of the first impl block

impl<T: approx::AbsDiffEq> approx::AbsDiffEq for Body<T> where
    T::Epsilon: Copy,
{
    type Epsilon = T::Epsilon;

    fn default_epsilon() -> T::Epsilon {
        T::default_epsilon()
    }

    fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
        T::abs_diff_eq(&self.mass, &other.mass, epsilon) &&
        T::abs_diff_eq(&self.pos, &other.pos, epsilon)
    }
}

Basically it throws errors at the references to pos. The error states it is because mismatched types, expecting &T and getting &Vec. I understand that it's an unexpected type but I don't know how to solve it. I'm still relatively new to rust.

Also if it's possible to implement this on the concrete type f64 instead that would be good.

Edit: I've worked around the problem by implementing PartialEq on the struct and then using approx crate only on the field I need it for.

0

There are 0 answers