Skip to content

Commit

Permalink
Handwrite ToTokens impl for Meta
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 16, 2024
1 parent 5dbfeae commit ae8c84a
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ ast_enum! {
}
}

ast_enum_of_structs! {
ast_enum! {
/// Content of a compile-time structured attribute.
///
/// ## Path
Expand Down Expand Up @@ -625,6 +625,24 @@ impl<'a> FilterAttrs<'a> for &'a [Attribute] {
}
}

impl From<Path> for Meta {
fn from(meta: Path) -> Meta {
Meta::Path(meta)
}
}

impl From<MetaList> for Meta {
fn from(meta: MetaList) -> Meta {
Meta::List(meta)
}
}

impl From<MetaNameValue> for Meta {
fn from(meta: MetaNameValue) -> Meta {
Meta::NameValue(meta)
}
}

#[cfg(feature = "parsing")]
pub(crate) mod parsing {
use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
Expand Down Expand Up @@ -757,7 +775,7 @@ pub(crate) mod parsing {

#[cfg(feature = "printing")]
mod printing {
use crate::attr::{AttrStyle, Attribute, MetaList, MetaNameValue};
use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
use proc_macro2::TokenStream;
use quote::ToTokens;

Expand All @@ -774,6 +792,17 @@ mod printing {
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for Meta {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Meta::Path(meta) => meta.to_tokens(tokens),
Meta::List(meta) => meta.to_tokens(tokens),
Meta::NameValue(meta) => meta.to_tokens(tokens),
}
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for MetaList {
fn to_tokens(&self, tokens: &mut TokenStream) {
Expand Down

0 comments on commit ae8c84a

Please sign in to comment.