Ah thats your issue here? That’s very easy to explain! Here, let’s run
use anyhow::Error as anyhowError;
use serde::{Serialize};
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum Error {
InvalidInput {
cause: Option<anyhowError>,
},
}
through “expand macros” in the playground, focusing on the part that derive(Deserialize)
generated:
#[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
#[allow(unused_macros)]
macro_rules! try {
($__expr : expr) =>
{
match $__expr
{
_serde :: __private :: Ok(__val) => __val, _serde ::
__private :: Err(__err) =>
{ return _serde :: __private :: Err(__err) ; }
}
}
}
#[automatically_derived]
impl _serde::Serialize for Error {
fn serialize<__S>(&self, __serializer: __S)
-> _serde::__private::Result<__S::Ok, __S::Error> where
__S: _serde::Serializer {
match *self {
Error::InvalidInput { ref cause } => {
let mut __serde_state =
match _serde::Serializer::serialize_struct(__serializer,
"Error", 0 + 1) {
_serde::__private::Ok(__val) => __val,
_serde::__private::Err(__err) => {
return _serde::__private::Err(__err);
}
};
match _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
"cause", cause) {
_serde::__private::Ok(__val) => __val,
_serde::__private::Err(__err) => {
return _serde::__private::Err(__err);
}
};
_serde::ser::SerializeStruct::end(__serde_state)
}
}
}
}
};
As you can see, there is a extern crate serde as _serde;
involved that allows the derived crate to (relatively) reliably refer to the root of the actual crate “serde”, even if some things were renamed, or another thing called serde
is in scope. (I’m not sure off the top of my head, just how reliably this approach works… but anyways…)
This will thus, in your case, mean that the _serde
from that extern crate
line is scoped in a longer path – i.e. the path to your current module, and then the const
(represented as _
) presumably – which explains the meaning of phone::_::_serde::ser::SerializeStruct::serialize_field
.
It’s ultimately the call to
_serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
"cause", cause)
that causes the compilation error, as that function does require its last argument to be (a reference to) a value of a Serialize
type.
The error message becomes better if the macro expansion is inserted manually:
Rust Playground
now featuring
error[E0277]: the trait bound `anyhow::Error: Serialize` is not satisfied
--> src/lib.rs:32:42
|
31 | match _serde::ser::SerializeStruct::serialize_field(&mut __serde_state,
| --------------------------------------------- required by a bound introduced by this call
32 | "cause", cause) {
| ^^^^^ the trait `Serialize` is not implemented for `anyhow::Error`
|
fully explaining the connection. But with macro-expansions, it’s hard to blame the compile for being unable to point to any source code directly responsible for the error, and at least serde_derive
is nice enough to set it up to point to at least the right field (as @quinedot already demonstrated).
Also, I’m wondering how you’re managing to actually make is show this phone::_
prefix. I’ve tried a few things and it didn’t do it for me, in the playground, for current stable or nightly compilers. What is your surrounding module structure like and what’s the compiler version?