Skip to content

Commit f7418cf

Browse files
Add compile_fail tests for repr macros on non-C-like enums
1 parent 51247ac commit f7418cf

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/lib.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ pub fn iter_variants_derive(input: TokenStream) -> TokenStream {
237237
unwrap_errors(iter::derive(&ast)).into()
238238
}
239239

240-
/// Derives [`TryFrom<Repr>`] for an enum, where `Repr` is a [primitive representation] specified
241-
/// in `#[repr(...)]`.
240+
/// Derives [`TryFrom<Repr>`] for a C-like enum, where `Repr` is a [primitive representation]
241+
/// specified in `#[repr(...)]`.
242242
///
243243
/// [`TryFrom<Repr>`]: https://doc.rust-lang.org/std/convert/trait.TryFrom.html
244244
/// [primitive representation]: https://doc.rust-lang.org/reference/type-layout.html#primitive-representations
@@ -263,13 +263,25 @@ pub fn iter_variants_derive(input: TokenStream) -> TokenStream {
263263
/// assert_eq!(Err(()), Direction::try_from(0u8));
264264
/// assert_eq!(Err(()), Direction::try_from(5u8));
265265
/// ```
266+
///
267+
/// This macro only works on C-like enums.
268+
///
269+
/// ```compile_fail
270+
/// #[derive(Debug, Clone, enum_utils::TryFromRepr)]
271+
/// #[repr(u8)]
272+
/// pub enum Letter {
273+
/// A,
274+
/// B,
275+
/// Other(u8),
276+
/// }
277+
/// ```
266278
#[proc_macro_derive(TryFromRepr, attributes(enumeration))]
267279
pub fn try_from_repr_derive(input: TokenStream) -> TokenStream {
268280
let ast = parse_macro_input!(input as DeriveInput);
269281
unwrap_errors(conv::derive_try_from_repr(&ast)).into()
270282
}
271283

272-
/// Derives [`From<Enum>`] for the [primitive representation] specified in `#[repr(...)]`.
284+
/// Derives [`From<CLikeEnum>`] for the [primitive representation] specified in `#[repr(...)]`.
273285
///
274286
/// [`From<Enum>`]: https://doc.rust-lang.org/std/convert/trait.From.html
275287
/// [primitive representation]: https://doc.rust-lang.org/reference/type-layout.html#primitive-representations
@@ -290,6 +302,18 @@ pub fn try_from_repr_derive(input: TokenStream) -> TokenStream {
290302
/// assert_eq!(1u8, North.into());
291303
/// assert_eq!(4u8, West.into());
292304
/// ```
305+
///
306+
/// This macro only works on C-like enums.
307+
///
308+
/// ```compile_fail
309+
/// #[derive(Debug, Clone, enum_utils::TryFromRepr)]
310+
/// #[repr(u8)]
311+
/// pub enum Letter {
312+
/// A,
313+
/// B,
314+
/// Other(u8),
315+
/// }
316+
/// ```
293317
#[proc_macro_derive(ReprFrom, attributes(enumeration))]
294318
pub fn repr_from_derive(input: TokenStream) -> TokenStream {
295319
let ast = parse_macro_input!(input as DeriveInput);

0 commit comments

Comments
 (0)