struct Foo {
}
impl Bar for Foo {
fn random_function() -> bool {
false
}
}
trait Bar {
fn random_function() -> bool;
}
impl<T> Debug for T where T: Bar {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if T::random_function() {
// do stuff
};
todo!()
}
}
I created the minimal example above, which is rejected by the compiler with:
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
--> FILE
|
190 | impl<T> Debug for T where T: Bar {
| ^ type parameter `T` must be used as the type parameter for some local type
|
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
= note: only traits defined in the current crate can be implemented for a type parameter
Does anyone know, if it is possible to achieve this thing somehow?
(The actual implementation is more code, but the trait still does only return true or false, and the result depends on the actual type of the generic)