Is there any way convert Serialize into auto traits?

I tried to defined Debug and Serialize as a Type, but got wrong ,
The reason seems to be that Serialize isn't belong to auto traits .

type ErrorData = dyn std::fmt::Debug + Serialize;

error[E0225]: only auto traits can be used as additional traits in a trait object

is there any way convert Serialize into auto traits ?

It's not possible, and it would be entirely the wrong solution anyway. That's not what marker traits are for. The right solution is not always/necessarily the immediate thing that would make the compiler error go away.

You got the syntax wrong; what you seem to want is a dyn Trait that is both dyn Debug and dyn Serialize. That's not possible for multiple reasons:

  • dyn Trait doesn't support multiple traits; and
  • Serialize is not object-safe because it's generic.

What you probably actually want is erased_serde.

However, that still doesn't let you generate a trait object from multiple traits. For that, you'll need to create your own trait with both Debug and erased_serde::Serialize as its supertraits, then blanket-impl it for all types that meet the bounds.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.