pub trait Foo{
type Bar;
fn foobar(&self, args: &[Self::Bar]) -> bool;
}
impl<F> Foo for F
where F: Fn(u32) -> bool {
type Bar = u32;
fn foobar(&self, args: &[Self:Bar]) -> bool {
(self)(args[0].clone()) // assuming args.len() > 0
}
}
Now I want to implement Foo for all of Fn(Arg) -> bool, as long as Arg implements Clone.
First attempt:
impl<F, Arg> Foo for F
where F: Fn(Arg) -> bool, Arg: Clone {
type Bar = Arg;
fn foobar(&self, args: &[Self:Bar]) -> bool {
(self)(args[0].clone()) // assuming args.len() > 0
}
}
This gives
error[E0207]: the type parameter `Arg` is not constrained by the impl trait, self type, or predicates
Second attempt:
impl<F, Arg> Foo for F
where F: Fn<Arg>, Arg: Clone {
type Bar = Arg;
fn foobar(&self, args: &[Self:Bar]) -> bool {
(self)(args[0].clone()) // assuming args.len() > 0
}
}
This gives
error[E0658]: the precise format of `Fn`-family traits' type parameters is subject to change. Use parenthetical notation (Fn(Foo, Bar) -> Baz) instead
The three criterias shown in rustc --explain E0207 seem not applicable here:
it appears in the implementing type of the impl, e.g. impl<T> Foo<T>
Reason: We are not implementing a struct.
for a trait impl, it appears in the implemented trait, e.g. impl<T> SomeTrait<T> for Foo
Reason: The trait Foo (in my example) does not accept any type parameter. And I don't really want to add the type parameter to it because it would then require adding the type parameter to any struct that has a field which implements Foo. This would then force the use of PhantomData since the type parameter in the struct has to be used.
it is bound as an associated type, e.g. impl<T, U> SomeTrait for T where T: AnotherTrait<AssocType=U>
Reason: Fn does not have associate type (or I don't know how to add it)
It really depends on the actual use case (what Foo is actually intended to do.) I assume it's a property of predicate closures, since all it requires is a Fn(_) -> bool implementation. I'm not sure there's a better way of doing this with a trait.