How to implement a characteristic for a parameterized characteristic

I have a design issue when using something like:

trait MyTrait<K: OtherTrait> { ... } impl<K: OtherTrait, M: MyTrait<K>> AnyTrait for M { ... } 

I cannot implement the trait for this trait due to error E207 ("the parameter of type K not limited to the sign of impl, self type or predicates").

It is impossible to get rid of this error, I am applying this not very good workaround (verbose and struct without any internal value):

 use std::fmt; use std::marker::PhantomData; pub trait MyTrait<K: fmt::Display> { fn get_some_k(&self) -> Option<K>; } /* // This is my target impl but results in E207 due to K not constrained impl<K: fmt::Display, S: MyTrait<K>> fmt::Display for S { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.get_some_k().unwrap()) } } */ pub struct Ugly<'a, K: fmt::Display, S: 'a + MyTrait<K>>(&'a S, PhantomData<K>); impl<'a, K: fmt::Display, S: MyTrait<K>> fmt::Display for Ugly<'a, K, S> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0.get_some_k().unwrap()) } } fn main() { } 

I think there must be some better way to implement the trait for this type of parameterized trait.

I did not find a good example in std (for example, there is no Display implementation in features with an associated type like Iterator )?

+6
source share
1 answer

Have an implementation using related types (this means that you can only implement MyTrait for one K for each type):

 use std::fmt; pub trait MyTrait { type K: fmt::Display; fn get_some_k(&self) -> Option<Self::K>; } impl<S: MyTrait> fmt::Display for S { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.get_some_k().unwrap()) } } fn main() { } 

However, when this is clarified, it becomes clear that this approach does not work either, because you implement Display for all types that implement MyTrait types, which can have their own implementation of Display . This is prohibited and you get E0210:

error: a type parameter S should be used as a type parameter for some local type (for example, MyStruct<T> ); only properties defined in the current box can be implemented for a parameter of type [E0210]

Wrapping it in something like Ugly done, for example, is the only way to allow such an implementation. Or implement the trait in your own box, and not in any of elses (e.g. Display is).

+2
source

Source: https://habr.com/ru/post/987866/


All Articles