Proc Macro Roids
Traits and functions to make writing proc macros more ergonomic.
The roids name is chosen because, although these functions make it easy to perform certain operation, they may not necessarily be a good idea =D!
Makes writing procedural macros much easier:
extern crate proc_macro;
use TokenStream;
use Span;
use ;
use quote;
use ;
/// Derives a `Super` enum with a variant for each struct field:
///
/// ```rust,edition2018
/// use std::marker::PhantomData;
/// use super_derive::Super;
///
/// #[derive(Super)]
/// pub struct Man<T> {
/// #[super_derive(skip)]
/// name: String,
/// power_level: u64,
/// marker: PhantomData<T>,
/// }
/// ```
///
/// Generates:
///
/// ```rust,ignore
/// pub enum SuperMan {
/// U64(u64),
/// }
/// ```
Examples
Contents:
-
Append additional
#[derive(..)]
s. -
Append named fields.
-
Append unnamed fields (tuples).
-
Get newtype inner
Field
. -
Ident
concatenation. -
Accessing struct fields.
-
Inspecting
Field
s. -
(De)constructing
Fields
. -
Append additional
#[derive(..)]
s.This works for function-like or attribute proc macros.
extern crate proc_macro; use TokenStream; use DeriveInputExt; use quote; use ;
-
Append named fields.
This works for structs with named fields or unit structs.
extern crate proc_macro; use TokenStream; use FieldsNamedAppend; use quote; use ; /// Example usage: /// /// ```rust /// use macro_crate::append_cd; /// /// #[append_cd] /// struct StructNamed { a: u32, b: i32 } /// ```
-
Append unnamed fields (tuples).
This works for structs with unnamed fields or unit structs.
extern crate proc_macro; use TokenStream; use FieldsUnnamedAppend; use quote; use ; /// Example usage: /// /// ```rust /// use macro_crate::append_i64_usize; /// /// #[append_i64_usize] /// struct StructUnit; /// ```
-
Get newtype inner
Field
.This works for structs with unnamed fields or unit structs.
extern crate proc_macro; use TokenStream; use DeriveInputNewtypeExt; use quote; use ;
-
Ident
concatenation.use IdentExt; use Span; use Ident; #
-
Accessing struct fields.
use DeriveInputStructExt; use ; #
-
Inspecting
Field
s.use FieldExt; use Span; use ; let fields_named: FieldsNamed = parse_quote! ; let fields = from; let field = fields.iter.next.expect; assert_eq!; assert!; assert!; assert_eq!;
-
(De)constructing
Fields
.# use FromStr; # use ; # use ; # use ; # use quote; # // Need to generate code that instantiates `MyEnum::Struct`: // enum MyEnum { // Struct { // field_0: u32, // field_1: u32, // } // } let ast: DeriveInput = parse_quote! ; let fields = ast.fields; let construction_form = fields.construction_form; let tokens = quote! ; let expected = from_str.unwrap; assert_eq!;
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.