Skip to content

Commit

Permalink
Allow setting attributes on the generated impl block (#9)
Browse files Browse the repository at this point in the history
This commit adds a new syntax to allow setting attributes directly on
the generated impl block. The main use case I need it for is to set
\#[allow(missing_docs)] on the enum items without needing to
specifically add it to every single variant.

This commit also includes a changelog update and the relevant docs
changes.
  • Loading branch information
Phantomical authored Oct 23, 2023
1 parent c568104 commit 39b3917
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is 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).

## Unreleased
### Added
- `c_enum!` now has some extra syntax to allow directly applying attributes to
the generated impl block.

### Changed
- It is now only possible to define a single enum instance within a `c_enum!`.
This is to allow additional syntax in the future that is not supported when
Expand Down
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@
//! that the variants are converted to constants so macros expecting an enum
//! variant will not work.
//!
//! ## Applying Attributes to Generated `impl` Blocks
//! Sometimes you need to apply attributes to the generated `impl` blocks (e.g.
//! to silence warnings). `c_enum!` supports adding an extra `impl` block at the
//! end and will apply those attributes to the generated `impl` block.
//! ```
//! #![deny(missing_docs)]
//! # //! crate docs...
//! # use c_enum::c_enum;
//!
//! c_enum! {
//! /// This crate requires that all items be documented.
//! ///
//! /// However, we don't want to document the members of this enum for one
//! /// reason or another.
//! pub enum SomeCEnum : u32 {
//! A = 0,
//! B
//! }
//!
//! // So we attach the #[allow] directive to this extra impl block here
//! // and it will be added to the one generated by the macro.
//! #[allow(missing_docs)]
//! impl {}
//! }
//! ```
//!
//! # Representation
//! It is valid to add a `#[repr(C)]` or `#[repr(transparent)]` attribute to the
//! generated type. The generated type is guaranteed to be a newtype whose only
Expand Down Expand Up @@ -222,6 +248,11 @@ macro_rules! c_enum {
$field:ident $( = $value:expr )?
),* $(,)?
}

$(
$( #[$iattr:meta] )*
impl {}
)?
} => {
$crate::__c_enum_no_debug! {
$( #[$attr] )*
Expand All @@ -231,6 +262,11 @@ macro_rules! c_enum {
$field $( = $value )?
),*
}

$(
$( #[$iattr] )*
impl {}
)?
}

impl ::core::fmt::Debug for $name
Expand Down Expand Up @@ -279,11 +315,17 @@ macro_rules! __c_enum_no_debug {
$field:ident $( = $value:expr )?
),* $(,)?
}

$(
$( #[$iattr:meta] )*
impl {}
)?
} => {
$( #[$attr] )*
$vis struct $name(pub $inner);

#[allow(non_upper_case_globals)]
$( $( #[$iattr] )* )?
impl $name {
$crate::__c_enum_impl!(
impl(decl_variants, $name, $inner, 0)
Expand Down

0 comments on commit 39b3917

Please sign in to comment.