Skip to content

Commit acd71dd

Browse files
committed
Switch to an attribute macro
1 parent 72b1b35 commit acd71dd

28 files changed

+135
-115
lines changed

enumflags_derive/src/lib.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,31 @@ use syn::{Data, Ident, DeriveInput, DataEnum, spanned::Spanned};
77
use proc_macro2::TokenStream;
88
use proc_macro2::Span;
99

10-
/// Shorthand for a quoted `compile_error!`.
11-
macro_rules! error {
12-
($span:expr => $($x:tt)*) => {
13-
quote_spanned!($span => compile_error!($($x)*);)
14-
};
15-
($($x:tt)*) => {
16-
quote!(compile_error!($($x)*);)
17-
};
18-
}
19-
20-
#[proc_macro_derive(BitFlags_internal)]
21-
pub fn derive_enum_flags(input: proc_macro::TokenStream)
22-
-> proc_macro::TokenStream
23-
{
10+
#[proc_macro_attribute]
11+
pub fn bitflags_internal(
12+
_attr: proc_macro::TokenStream,
13+
input: proc_macro::TokenStream,
14+
) -> proc_macro::TokenStream {
2415
let ast: DeriveInput = syn::parse(input).unwrap();
2516

26-
match ast.data {
17+
let impls = match ast.data {
2718
Data::Enum(ref data) => {
2819
gen_enumflags(&ast.ident, &ast, data)
29-
.unwrap_or_else(|err| err.to_compile_error())
30-
.into()
3120
}
3221
Data::Struct(ref data) => {
33-
error!(data.struct_token.span => "BitFlags can only be derived on enums").into()
22+
Err(syn::Error::new_spanned(data.struct_token, "#[bitflags] requires an enum"))
3423
}
3524
Data::Union(ref data) => {
36-
error!(data.union_token.span => "BitFlags can only be derived on enums").into()
25+
Err(syn::Error::new_spanned(data.union_token, "#[bitflags] requires an enum"))
3726
}
38-
}
27+
};
28+
29+
let impls = TokenStream::from(impls.unwrap_or_else(|err| err.to_compile_error()));
30+
let combined = quote! {
31+
#ast
32+
#impls
33+
};
34+
combined.into()
3935
}
4036

4137
/// Try to evaluate the expression given.
@@ -232,6 +228,6 @@ fn gen_enumflags(ident: &Ident, item: &DeriveInput, data: &DataEnum)
232228
}
233229
}
234230

235-
impl ::enumflags2::RawBitFlags for #ident {}
231+
impl ::enumflags2::BitFlag for #ident {}
236232
})
237233
}

src/fallible.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::convert::TryFrom;
22
use core::fmt;
33
use super::BitFlags;
4-
use super::RawBitFlags;
4+
use super::BitFlag;
55

66
// Coherence doesn't let us use a generic type here. Work around by implementing
77
// for each integer type manually.
@@ -10,7 +10,7 @@ macro_rules! impl_try_from {
1010
$(
1111
impl<T> TryFrom<$ty> for BitFlags<T>
1212
where
13-
T: RawBitFlags<Type=$ty>,
13+
T: BitFlag<Type=$ty>,
1414
{
1515
type Error = FromBitsError<T>;
1616

@@ -32,8 +32,9 @@ impl_try_from! {
3232
///
3333
/// ```
3434
/// # use std::convert::TryInto;
35-
/// # use enumflags2::BitFlags;
36-
/// #[derive(Clone, Copy, Debug, BitFlags)]
35+
/// # use enumflags2::{bitflags, BitFlags};
36+
/// #[bitflags]
37+
/// #[derive(Clone, Copy, Debug)]
3738
/// #[repr(u8)]
3839
/// enum MyFlags {
3940
/// A = 0b0001,
@@ -49,12 +50,12 @@ impl_try_from! {
4950
/// assert_eq!(error.invalid_bits(), 0b10000);
5051
/// ```
5152
#[derive(Debug, Copy, Clone)]
52-
pub struct FromBitsError<T: RawBitFlags> {
53+
pub struct FromBitsError<T: BitFlag> {
5354
pub(crate) flags: BitFlags<T>,
5455
pub(crate) invalid: T::Type,
5556
}
5657

57-
impl<T: RawBitFlags> FromBitsError<T> {
58+
impl<T: BitFlag> FromBitsError<T> {
5859
/// Return the truncated result of the conversion.
5960
pub fn truncate(self) -> BitFlags<T> {
6061
self.flags
@@ -66,14 +67,14 @@ impl<T: RawBitFlags> FromBitsError<T> {
6667
}
6768
}
6869

69-
impl<T: RawBitFlags + fmt::Debug> fmt::Display for FromBitsError<T> {
70+
impl<T: BitFlag + fmt::Debug> fmt::Display for FromBitsError<T> {
7071
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
7172
write!(fmt, "Invalid bits for {:?}: {:#b}", self.flags, self.invalid)
7273
}
7374
}
7475

7576
#[cfg(feature = "std")]
76-
impl<T: RawBitFlags + fmt::Debug> std::error::Error for FromBitsError<T> {
77+
impl<T: BitFlag + fmt::Debug> std::error::Error for FromBitsError<T> {
7778
fn description(&self) -> &str {
7879
"invalid bitflags representation"
7980
}

src/formatting.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use core::fmt::{self, Debug, Binary};
2-
use crate::{BitFlags, RawBitFlags};
2+
use crate::{BitFlags, BitFlag};
33

44
impl<T> fmt::Debug for BitFlags<T>
55
where
6-
T: RawBitFlags + fmt::Debug,
6+
T: BitFlag + fmt::Debug,
77
{
88
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
99
let name = T::bitflags_type_name();
@@ -37,7 +37,7 @@ where
3737

3838
impl<T> fmt::Binary for BitFlags<T>
3939
where
40-
T: RawBitFlags,
40+
T: BitFlag,
4141
T::Type: fmt::Binary,
4242
{
4343
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -47,7 +47,7 @@ where
4747

4848
impl<T> fmt::Octal for BitFlags<T>
4949
where
50-
T: RawBitFlags,
50+
T: BitFlag,
5151
T::Type: fmt::Octal,
5252
{
5353
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -57,7 +57,7 @@ where
5757

5858
impl<T> fmt::LowerHex for BitFlags<T>
5959
where
60-
T: RawBitFlags,
60+
T: BitFlag,
6161
T::Type: fmt::LowerHex,
6262
{
6363
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -67,7 +67,7 @@ where
6767

6868
impl<T> fmt::UpperHex for BitFlags<T>
6969
where
70-
T: RawBitFlags,
70+
T: BitFlag,
7171
T::Type: fmt::UpperHex,
7272
{
7373
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)