0

I'm trying to make a test with generics and traits and hit a wall. I want to add a FromPoint method to my generic SphericalPoint<T> type but I don't understand how to provide a generic number operations trait:

struct Point<T> {
    x: T,
    y: T,
}

trait FloatTrait {
    fn my_cos(&self) -> Self;
    fn my_sin(&self) -> Self;
    fn my_sqrt(&self) -> Self;
    fn my_atan2(&self, v: Self) -> Self;
}

struct SphericalPoint<T: FloatTrait> {
    r: T,
    theta: T,
}

impl<T: Copy + FloatTrait + std::ops::Add<Output = T> + std::ops::Mul<Output = T>>
    SphericalPoint<T>
{
    fn to_point(&self) -> Point<T> {
        Point {
            x: self.r * self.theta.my_cos(),
            y: self.r * self.theta.my_sin(),
        }
    }

    fn from_point(p: &Point<T>) -> SphericalPoint<T> {
        SphericalPoint {
            r: (p.x + p.y).my_sqrt(),
            theta: p.y.my_atan2(p.x),
        }
    }
}

I tried to add a FloatTrait that would make sure the types have the methods I need, but then it doesn't get fulfilled automatically, so I would need to implement it with alias names for each methods (like fn mysin(&self) -> Self;, which is really silly).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dsthilaire
  • 21
  • 2
  • 2
    If you define a trait, you have to implement it for every type you want. To ease it for identical types you can either use a macro, or (maybe more idiomatic) implement the trait only once for type of trait `num::Float` from `num` crate – Alexey S. Larionov Jun 07 '21 at 14:04
  • @AlexeyLarionov, right. Ideally I don't want to implement the FloatTrait, I just used that because it's the only I saw how to make this happen. It does sound like the num::Float crate is what i was looking for. I would I go about finding something like that in the future? I searched everywhere in https://doc.rust-lang.org/stable/std/?search=cos, and coudln't find it...!) – dsthilaire Jun 07 '21 at 14:26
  • 1
    https://crates.io/ hosts user published crates. If you are looking for functionality not provided by the standard library, that is the best place to look. – EvilTak Jun 07 '21 at 14:54
  • 1
    @Netwave the trait declaration is correct -- [`Self`](https://doc.rust-lang.org/reference/paths.html#self-1) refers to the type _implementing_ the trait, not the trait itself. For instance, in `impl FloatTrait for f32`, `Self == f32` (i.e. all the implemented methods must return a `f32`). – EvilTak Jun 07 '21 at 14:59
  • 2
    It looks like your question might be answered by the answers of [Is there any trait that specifies numeric functionality?](https://stackoverflow.com/q/37296351/155423). If not, please **[edit] your question** to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 07 '21 at 14:59
  • ok thanks, it's very surprising this is not provided by std...! – dsthilaire Jun 07 '21 at 14:59
  • 1
    you're right @shepmaster, it seems this was already answered in https://stackoverflow.com/questions/37296351/is-there-any-trait-that-specifies-numeric-functionality. Thanks for your help. (ps I don't know how to mark this questions as "already answered") – dsthilaire Jun 07 '21 at 15:03

0 Answers0