I would like to make something like the following work:
use std::ops::Add;
trait CanBeAdded: Sized where f64: Add<Self> {}
fn add2<X: CanBeAdded>(x: X) {}
fn main() {}
The above fails to compile:
error[E0277]: the trait bound `f64: std::ops::Add<X>` is not satisfied
--> src/main.rs:5:1
|
5 | fn add2<X: CanBeAdded>(x: X) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::ops::Add<X>` is not implemented for `f64`
|
= help: consider adding a `where f64: std::ops::Add<X>` bound
= note: required by `CanBeAdded`
I'm trying to assert the existence of certain impl
s via a trait: i.e. X: CanBeAdded
implies f64: Add<X>
. While I can add the bounds to the where clause of the function like this:
fn add2<X>(x: X) where f64: Add<X> { }
I have many of them so it gets unwieldy and I would prefer to not repeat the bounds over and over. Is there a good solution to this? For example, is it possible to include a macro that expands to a series of where
clauses?