Skip to content

Commit

Permalink
Merge pull request #4 from Robbepop/rf-update-readme
Browse files Browse the repository at this point in the history
Update readme & proc. macro docs
  • Loading branch information
Robbepop committed Mar 28, 2023
2 parents 53fafd0 + 6c0e682 commit ae33ddc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -86,14 +86,14 @@ const _: () = {
impl ::enum_tag::EnumTag for Foo {
type Tag = FooTag;

fn tag(&self) -> Self::Tag {
fn tag(&self) -> <Self as ::enum_tag::EnumTag>::Tag {
match self {
Self::A { .. } => Self::Tag::A,
Self::B { .. } => Self::Tag::B,
Self::C { .. } => Self::Tag::C,
Self::D { .. } => Self::Tag::D,
Self::E { .. } => Self::Tag::E,
Self::F { .. } => Self::Tag::F,
Self::A { .. } => <Self as ::enum_tag::EnumTag>::Tag::A,
Self::B { .. } => <Self as ::enum_tag::EnumTag>::Tag::B,
Self::C { .. } => <Self as ::enum_tag::EnumTag>::Tag::C,
Self::D { .. } => <Self as ::enum_tag::EnumTag>::Tag::D,
Self::E { .. } => <Self as ::enum_tag::EnumTag>::Tag::E,
Self::F { .. } => <Self as ::enum_tag::EnumTag>::Tag::F,
}
}
}
Expand Down
52 changes: 51 additions & 1 deletion macro/src/lib.rs
Expand Up @@ -17,7 +17,7 @@ mod derive;
/// # Example
///
/// ```
/// use ::enum_tag::EnumTag;
/// use enum_tag::EnumTag;
///
/// #[derive(EnumTag)]
/// #[repr(u8)] // Rust needs this for `B = 42`
Expand All @@ -42,6 +42,56 @@ mod derive;
///
/// assert_eq!(FooTag::B as u8, 42);
/// ```
///
/// The above `#[derive(EnumTag)]` proc. macro will expand to roughly the following Rust code:
///
/// ```
/// # #[repr(u8)] // Rust needs this for `B = 42`
/// # enum Foo {
/// # A,
/// # B = 42,
/// # C(i32),
/// # D(i32, i64),
/// # E { a: i32 },
/// # F { a: i32, b: i64 },
/// # }
/// #
/// const _: () = {
/// #[derive(
/// ::core::fmt::Debug,
/// ::core::clone::Clone,
/// ::core::marker::Copy,
/// ::core::cmp::PartialEq,
/// ::core::cmp::Eq,
/// ::core::cmp::PartialOrd,
/// ::core::cmp::Ord,
/// ::core::hash::Hash,
/// )]
/// pub enum FooTag {
/// A,
/// B = 42,
/// C,
/// D,
/// E,
/// F,
/// }
///
/// impl ::enum_tag::EnumTag for Foo {
/// type Tag = FooTag;
///
/// fn tag(&self) -> <Self as ::enum_tag::EnumTag>::Tag {
/// match self {
/// Self::A { .. } => <Self as ::enum_tag::EnumTag>::Tag::A,
/// Self::B { .. } => <Self as ::enum_tag::EnumTag>::Tag::B,
/// Self::C { .. } => <Self as ::enum_tag::EnumTag>::Tag::C,
/// Self::D { .. } => <Self as ::enum_tag::EnumTag>::Tag::D,
/// Self::E { .. } => <Self as ::enum_tag::EnumTag>::Tag::E,
/// Self::F { .. } => <Self as ::enum_tag::EnumTag>::Tag::F,
/// }
/// }
/// }
/// };
/// ```
#[proc_macro_derive(EnumTag)]
pub fn enum_tag(input: TokenStream) -> TokenStream {
derive::enum_tag(parse_macro_input!(input as DeriveInput)).into()
Expand Down

0 comments on commit ae33ddc

Please sign in to comment.