Skip to content

Commit

Permalink
Merge pull request #2 from Robbepop/rf-release-v0.2.0
Browse files Browse the repository at this point in the history
Prepare release of version `0.2.0`
  • Loading branch information
Robbepop committed Mar 24, 2023
2 parents 1f393a4 + 3cea7fb commit ededce0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
Additionally we have an `Internal` section for changes that are of interest to developers.

Dates in this file are formattes as `YYYY-MM-DD`.

## [`0.2.0`] - 2023-03-24

### Fixed

- Fixed a minor proc. macro hygiene issue with `derive` invokations generated by the `EnumTag` derive.

## [`0.1.0`] - 2023-03-23

Initial release of the `enum-tag` crate.
4 changes: 2 additions & 2 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "enum-tag"
version = "0.1.0"
version = "0.2.0"
documentation = "https://docs.rs/enum-tag/"
description = "Proc. macro for generating enum discriminant types."
authors.workspace = true
Expand All @@ -21,7 +21,7 @@ keywords = ["enum", "c-like", "tag", "discriminant", "tagged"]
categories = ["data-structures", "no-std"]

[dependencies]
enum-tag-macro = { version = "0.1.0", path = "macro" }
enum-tag-macro = { version = "0.2.0", path = "macro" }

[workspace]
members = ["macro"]
31 changes: 19 additions & 12 deletions README.md
Expand Up @@ -37,22 +37,27 @@ In this example the opcodes are the instruction `enum` tag.
use ::enum_tag::EnumTag;

#[derive(EnumTag)]
#[repr(u8)] // Rust needs this for `B = 42`
enum Foo {
A = 42,
B(i32),
C(i32, i64),
D { a: i32 },
E { a: i32, b: i64 },
A,
B = 42,
C(i32),
D(i32, i64),
E { a: i32 },
F { a: i32, b: i64 },
}

/// This is how we can access the generated C-like enum type and name it.
type FooTag = <Foo as EnumTag>::Tag;

assert_eq!(Foo::A.tag(), FooTag::A);
assert_eq!(Foo::B(1).tag(), FooTag::B);
assert_eq!(Foo::C(2, 3).tag(), FooTag::C);
assert_eq!(Foo::D { a: 4 }.tag(), FooTag::D);
assert_eq!(Foo::E { a: 5, b: 6 }.tag(), FooTag::E);
assert_eq!(FooTag::A, Foo::A.tag());
assert_eq!(FooTag::B, Foo::B.tag());
assert_eq!(FooTag::C, Foo::C(1).tag());
assert_eq!(FooTag::D, Foo::D(2, 3).tag());
assert_eq!(FooTag::E, Foo::E { a: 4 }.tag());
assert_eq!(FooTag::F, Foo::F { a: 5, b: 6 }.tag());

assert_eq!(FooTag::B as u8, 42);
```

The above `#[derive(EnumTag)]` generates the following Rust code:
Expand All @@ -70,11 +75,12 @@ const _: () = {
::core::hash::Hash,
)]
pub enum FooTag {
A = 42,
B,
A,
B = 42,
C,
D,
E,
F,
}

impl ::enum_tag::EnumTag for Foo {
Expand All @@ -87,6 +93,7 @@ const _: () = {
Self::C { .. } => Self::Tag::C,
Self::D { .. } => Self::Tag::D,
Self::E { .. } => Self::Tag::E,
Self::F { .. } => Self::Tag::F,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions macro/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "enum-tag-macro"
version = "0.1.0"
version = "0.2.0"
documentation = "https://docs.rs/enum-tag-macro/"
description = "Proc. macro implementation for the `enum-tag` crate."
authors.workspace = true
Expand All @@ -18,7 +18,6 @@ proc-macro = true
syn = "2.0.0"
proc-macro2 = "1.0.0"
quote = "1.0.0"
heck = "0.4.0"

[dev-dependencies]
enum-tag = { path = ".." }
23 changes: 14 additions & 9 deletions macro/src/lib.rs
Expand Up @@ -20,22 +20,27 @@ mod derive;
/// use ::enum_tag::EnumTag;
///
/// #[derive(EnumTag)]
/// #[repr(u8)] // Rust needs this for `B = 42`
/// enum Foo {
/// A,
/// B(i32),
/// C(i32, i64),
/// D { a: i32 },
/// E { a: i32, b: i64 },
/// B = 42,
/// C(i32),
/// D(i32, i64),
/// E { a: i32 },
/// F { a: i32, b: i64 },
/// }
///
/// /// This is how we can access the generated C-like enum type and name it.
/// type FooTag = <Foo as EnumTag>::Tag;
///
/// assert_eq!(Foo::A.tag(), FooTag::A);
/// assert_eq!(Foo::B(1).tag(), FooTag::B);
/// assert_eq!(Foo::C(2, 3).tag(), FooTag::C);
/// assert_eq!(Foo::D { a: 4 }.tag(), FooTag::D);
/// assert_eq!(Foo::E { a: 5, b: 6 }.tag(), FooTag::E);
/// assert_eq!(FooTag::A, Foo::A.tag());
/// assert_eq!(FooTag::B, Foo::B.tag());
/// assert_eq!(FooTag::C, Foo::C(1).tag());
/// assert_eq!(FooTag::D, Foo::D(2, 3).tag());
/// assert_eq!(FooTag::E, Foo::E { a: 4 }.tag());
/// assert_eq!(FooTag::F, Foo::F { a: 5, b: 6 }.tag());
///
/// assert_eq!(FooTag::B as u8, 42);
/// ```
#[proc_macro_derive(EnumTag)]
pub fn enum_tag(input: TokenStream) -> TokenStream {
Expand Down

0 comments on commit ededce0

Please sign in to comment.