Hello. I need some help with this. I read the description of E0207, but it's not clear to me how to apply it to my case and how to make the code work. Despite Arg1 and Res being used in self type it's not enough seems.
error[E0207]: the type parameter `Arg1` is not constrained by the impl trait, self type, or predicates
--> src/main.rs:60:7
|
60 | impl< Arg1, Res, Ro > IntoRoutine
| ^^^^ unconstrained type parameter
error[E0207]: the type parameter `Res` is not constrained by the impl trait, self type, or predicates
--> src/main.rs:60:13
|
60 | impl< Arg1, Res, Ro > IntoRoutine
| ^^^ unconstrained type parameter
For more information about this error, try `rustc --explain E0207`.
The point of this error is that, in this context, an "unconstrained" type parameter is one that does not participate in the construction of the type. In your case, even though you technically write down Arg1 and Res, those types don't cause the trait or Ro, the type you are implementing the trait for, to change, i.e. neither the identity of Ro nor the identity of IntoRoutine depends on either Arg1 or Res.
If you resolve the universal quantifiers, your code essentially means this in plain English: "implement IntoRoutine for all types Ro as long as those types implement Fn with argument type Arg1 and return type Res".
Since a particular type might implement Fn with many different combinations of Arg1 and Res, this could cause the same trait (IntoRoutine) to be implemented multiple times for the same type, which is not allowed.
What you probably meant to do is declare IntoRoutine with a type parameter and an associated type. That does indeed make more sense: a particular object may implement differentIntoRoutine<Arg> traits, because it may be able to accept more than one kind of argument. And the type of the argument probably determines the return type, so generally that would be an associated type. Playground.
As an aside, please look at some real-world Rust code and/or examples in The Book or the std documentation in order to learn idiomatic code formatting.
Thanks for the answer, but I don't understand how to apply your advice to the original problem . My goal to have an answer to the question "is that a function implementing trait Fn".
Original problem.
Does any kind of a solution to the problem exist in Rust?
You still did not add Add as a type parameter to the trait, and you are still using Self::Res1 in the where clause constraining the Fn itself, instead of the Res type parameter. This compiles. Again, this is the exact same code I gave before.
Those two impls overlap. A type parameter means universal quantification (<T> can be read as "for all types T"). Thus, if you substitute Arg2 = () into the second impl, you have the <Arg1, ()> impl twice. This doesn't make sense. You should probably implement a single parameter only, and use tuples when necessary.