I have a problem in which a child trait constraint on parent trait doesn't apply. Here is a simple example:
trait ValueGetter {
type ValueType;
fn get_value() -> Self::ValueType;
}
trait DebugValueGetter: ValueGetter
where
Self::ValueType: std::fmt::Debug,
{
}
fn function<DVG: DebugValueGetter>(debug_value_getter: DVG) {
todo!()
}
For some reason, rust says:
error[E0277]: `<DVG as ValueGetter>::ValueType` doesn't implement `Debug`
--> src\main.rs:13:18
|
13 | fn function<DVG: DebugValueGetter>(debug_value_getter: DVG) {
| ^^^^^^^^^^^^^^^^ `<DVG as ValueGetter>::ValueType` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
= help: the trait `Debug` is not implemented for `<DVG as ValueGetter>::ValueType`
note: required by a bound in `DebugValueGetter`
--> src\main.rs:9:22
|
7 | trait DebugValueGetter: ValueGetter
| ---------------- required by a bound in this trait
8 | where
9 | Self::ValueType: std::fmt::Debug,
| ^^^^^^^^^^^^^^^ required by this bound in `DebugValueGetter`
help: consider further restricting the associated type
|
13 | fn function<DVG: DebugValueGetter>(debug_value_getter: DVG) where <DVG as ValueGetter>::ValueType: Debug {
| ++++++++++++++++++++++++++++++++++++++++++++
My expectation was that the where
on DebugValueGetter
would constraint such that you cannot implement DebugValueGetter
for ValueGetter
if ValueGetter::ValueType
doesn't implement Debug
, but it seems the constraint falls separately on the usage of the trait.
Is there a proper way to get the outcome I desire? Is this intended?