I want to always call a macro on any struct, but only implement TryInto<u8>
in the case where such struct has a field named ˋstatusˋ:
#[macro_export]
macro_rules! impl_status {
(
struct $name:ident {
status: $status_type:ty,
$($rest:tt)*
}
) => {
#[allow(dead_code)]
impl TryInto<u8> for $name {
type Error = ();
fn try_into(
self,
) -> Result<u8, Self::Error> {
todo!()
}
}
};
(
struct $name:ident {
$($field:ident : $type:ty),*
}
) => {
impl TryInto<()> for $name {
type Error = ();
fn try_into(self) -> Result<(), Self::Error> {
Ok(())
}
}
};
}
pub struct HasStatusStruct {
status: u8
}
impl_status!{HasStatusStruct}
I know why this does not work: ˋHasStatusStructˋ is purely a name and not a struct definition.
How do I solve this?