I tried to write two traits where you want the other to be implemented and get this error:
error[E0277]: the trait bound `T: ValTrait` is not satisfied
--> src/main.rs:20:1
|
20 | / fn get<T: ValRequireTrait + std::fmt::Debug>(_t: T) {
21 | | println!("{:?}", T::VAL);
22 | | }
| |_^ the trait `ValTrait` is not implemented for `T`
|
= help: consider adding a `where T: ValTrait` bound
= note: required by `ValRequireTrait`
Code :
trait ValTrait<T = Self> {
const VAL: T;
}
trait ValRequireTrait<T: ValTrait = Self> {}
struct A {
field: u64,
}
impl ValTrait for A {
const VAL: A = A {
field: 0u64
};
}
impl ValRequireTrait for A {}
fn get<T: ValRequireTrait + std::fmt::Debug>(_t: T) {
println!("{:?}", T::VAL);
}
fn main() {
let a = A { field: 6u64 };
get(a);
}
How to do it right? If I do what the compiler says, I will not need it ValRequireTrait, because it will be useless. I wanted it to ValRequireTraitbe a mark that the structure implements sufficient methods as necessary.
In other words, I expect such a requirement to be transitive, so when a function get()requires one sign ( ValRequireTrait), others ( ValTrait) will be needed automatically without any specification in the code, as the compiler wants.
source
share