Derive no bound macros (to be also used in pallet macro) (#7280)

* derive no bound macros

* explicit different variant for partialeq

* fix ui for 1.47

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* move test to frame-support-test

* renames, code organization and remove expect as suggested

* better doc

* remove DebugStripped introduce RuntimeDebugNoBound

* rename

* fix test

* fix ui test

* fix line width

* Update frame/support/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/support/procedural/src/clone_no_bound.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fix confusing dead code

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Guillaume Thiolliere
2020-10-19 11:11:51 +02:00
committed by GitHub
parent 87c18598fc
commit 7ca9baf9b6
17 changed files with 760 additions and 2 deletions
@@ -22,6 +22,9 @@
mod storage;
mod construct_runtime;
mod transactional;
mod debug_no_bound;
mod clone_no_bound;
mod partial_eq_no_bound;
use proc_macro::TokenStream;
@@ -326,6 +329,75 @@ pub fn transactional(attr: TokenStream, input: TokenStream) -> TokenStream {
transactional::transactional(attr, input).unwrap_or_else(|e| e.to_compile_error().into())
}
/// Derive [`Clone`] but do not bound any generic. Docs are at `frame_support::CloneNoBound`.
#[proc_macro_derive(CloneNoBound)]
pub fn derive_clone_no_bound(input: TokenStream) -> TokenStream {
clone_no_bound::derive_clone_no_bound(input)
}
/// Derive [`Debug`] but do not bound any generics. Docs are at `frame_support::DeriveNoBounds`.
#[proc_macro_derive(DebugNoBound)]
pub fn derive_debug_no_bound(input: TokenStream) -> TokenStream {
debug_no_bound::derive_debug_no_bound(input)
}
/// Derive [`Debug`], if `std` is enabled it uses `frame_support::DebugNoBound`, if `std` is not
/// enabled it just returns `"<stripped>"`.
/// This behaviour is useful to prevent bloating the runtime WASM blob from unneeded code.
#[proc_macro_derive(RuntimeDebugNoBound)]
pub fn derive_runtime_debug_no_bound(input: TokenStream) -> TokenStream {
#[cfg(not(feature = "std"))]
{
let input: syn::DeriveInput = match syn::parse(input) {
Ok(input) => input,
Err(e) => return e.to_compile_error().into(),
};
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
quote::quote!(
const _: () = {
impl #impl_generics core::fmt::Debug for #name #ty_generics #where_clause {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
fmt.write_str("<stripped>")
}
}
};
).into()
}
#[cfg(feature = "std")]
{
debug_no_bound::derive_debug_no_bound(input)
}
}
/// Derive [`PartialEq`] but do not bound any generic. Docs are at
/// `frame_support::PartialEqNoBound`.
#[proc_macro_derive(PartialEqNoBound)]
pub fn derive_partial_eq_no_bound(input: TokenStream) -> TokenStream {
partial_eq_no_bound::derive_partial_eq_no_bound(input)
}
/// derive Eq but do no bound any generic. Docs are at `frame_support::EqNoBound`.
#[proc_macro_derive(EqNoBound)]
pub fn derive_eq_no_bound(input: TokenStream) -> TokenStream {
let input: syn::DeriveInput = match syn::parse(input) {
Ok(input) => input,
Err(e) => return e.to_compile_error().into(),
};
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
quote::quote_spanned!(name.span() =>
const _: () = {
impl #impl_generics core::cmp::Eq for #name #ty_generics #where_clause {}
};
).into()
}
#[proc_macro_attribute]
pub fn require_transactional(attr: TokenStream, input: TokenStream) -> TokenStream {
transactional::require_transactional(attr, input).unwrap_or_else(|e| e.to_compile_error().into())