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!
Append additional #[derive(..)]
s.
This works for function-like or attribute proc macros.
```rust,ignore extern crate proc_macro;
use procmacro::TokenStream; use procmacroroids::DeriveInputDeriveExt; use quote::quote; use syn::{parsemacroinput, parsequote, DeriveInput};
pub fn copy(args: TokenStream, item: TokenStream) -> TokenStream { // Example input: // // #[derive(Debug)] // struct Struct; let mut ast = parsemacro_input!(item as DeriveInput);
// Append the derives.
let derives = parse_quote!(Clone, Copy);
ast.append_derives(derives);
// Example output:
//
// #[derive(Debug, Clone, Copy)]
// struct Struct;
TokenStream::from(quote! { #ast })
} ```
Append named fields.
This works for structs with named fields or unit structs.
```rust,ignore extern crate proc_macro;
use procmacro::TokenStream; use procmacroroids::FieldsNamedAppend; use quote::quote; use syn::{parsemacroinput, parsequote, DeriveInput, FieldsNamed};
/// Example usage:
///
/// rust
/// use macro_crate::append_cd;
///
/// #[append_cd]
/// struct StructNamed { a: u32, b: i32 }
///
pub fn appendcd(args: TokenStream, item: TokenStream) -> TokenStream { // Example input: // // struct StructNamed { a: u32, b: i32 } let mut ast = parsemacroinput!(item as DeriveInput);
// Append the fields.
let fields_additional: FieldsNamed = parse_quote!({ c: i64, d: usize });
ast.append_named(fields_additional);
// Example output:
//
// struct StructNamed { a: u32, b: i32, c: i64, d: usize }
TokenStream::from(quote! { #ast })
} ```
Append unnamed fields (tuples).
This works for structs with unnamed fields or unit structs.
```rust,ignore extern crate proc_macro;
use procmacro::TokenStream; use procmacroroids::FieldsUnnamedAppend; use quote::quote; use syn::{parsemacroinput, parsequote, DeriveInput, FieldsUnnamed};
/// Example usage:
///
/// rust
/// use macro_crate::append_i64_usize;
///
/// #[append_i64_usize]
/// struct StructUnit;
///
pub fn appendi64usize(args: TokenStream, item: TokenStream) -> TokenStream { // Example input: // // struct StructUnit; let mut ast = parsemacro_input!(item as DeriveInput);
// Append the fields.
let fields_additional: FieldsUnnamed = parse_quote!((i64, usize));
ast.append_unnamed(fields_additional);
// Example output:
//
// struct StructUnit(i64, usize);
TokenStream::from(quote! { #ast })
} ```
Get newtype inner Field
.
This works for structs with unnamed fields or unit structs.
```rust,ignore extern crate proc_macro;
use procmacro::TokenStream; use procmacroroids::DeriveInputNewtypeExt; use quote::quote; use syn::{parsemacroinput, parsequote, DeriveInput, Type};
pub fn derivederef(item: TokenStream) -> TokenStream { // Example input: // // #[derive(Deref)] // struct Newtype(u32); let mut ast = parsemacro_input!(item as DeriveInput);
// Get the inner field.
let inner_field = ast.inner_type();
// Implement `Deref`
let type_name = &ast.ident;
let token_stream_2 = quote! {
#ast
impl std::ops::Deref for #type_name {
type Target = #inner_type;
fn deref(&self) -> &Self::Target {
&self.0
}
}
}
TokenStream::from(token_stream_2)
} ```
Licensed under either of
at your option.
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.