From 94f5887207af4abfb85e266985f9bd260ee09e04 Mon Sep 17 00:00:00 2001 From: Guillaume Thiolliere Date: Wed, 24 Nov 2021 09:07:43 +0900 Subject: [PATCH] Add some doc for the derive macro (#301) * some doc * Update derive/src/lib.rs Co-authored-by: David * fmt Co-authored-by: David --- derive/Cargo.toml | 2 +- derive/src/lib.rs | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 33ff281a..0d312ad6 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -17,7 +17,7 @@ proc-macro2 = "1.0.6" proc-macro-crate = "1.0.0" [dev-dependencies] -parity-scale-codec = { path = ".." } +parity-scale-codec = { path = "..", features = ["max-encoded-len"] } [features] # Enables the new `MaxEncodedLen` trait. diff --git a/derive/src/lib.rs b/derive/src/lib.rs index baed14dd..38248929 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -75,6 +75,15 @@ fn wrap_with_dummy_const(input: DeriveInput, impl_block: proc_macro2::TokenStrea /// Derive `parity_scale_codec::Encode` and `parity_scale_codec::EncodeLike` for struct and enum. /// +/// # Top level attributes +/// +/// By default the macro will add [`Encode`] and [`Decode`] bounds to all types, but the bounds can +/// be specified manually with the top level attributes: +/// * `#[codec(encode_bound(T: Encode))]`: a custom bound added to the `where`-clause when deriving +/// the `Encode` trait, overriding the default. +/// * `#[codec(decode_bound(T: Decode))]`: a custom bound added to the `where`-clause when deriving +/// the `Decode` trait, overriding the default. +/// /// # Struct /// /// A struct is encoded by encoding each of its fields successively. @@ -87,8 +96,6 @@ fn wrap_with_dummy_const(input: DeriveInput, impl_block: proc_macro2::TokenStrea /// type must implement `parity_scale_codec::EncodeAsRef<'_, $FieldType>` with $FieldType the /// type of the field with the attribute. This is intended to be used for types implementing /// `HasCompact` as shown in the example. -/// * `#[codec(encode_bound(T: Encode))]`: a custom where bound that will be used when deriving the `Encode` trait. -/// * `#[codec(decode_bound(T: Encode))]`: a custom where bound that will be used when deriving the `Decode` trait. /// /// ``` /// # use parity_scale_codec_derive::Encode; @@ -360,7 +367,19 @@ pub fn compact_as_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStr wrap_with_dummy_const(input, impl_block) } -/// Derive `MaxEncodedLen`. +/// Derive `parity_scale_codec::MaxEncodedLen` for struct and enum. +/// +/// # Top level attribute +/// +/// By default the macro will try to bound the types needed to implement `MaxEncodedLen`, but the +/// bounds can be specified manually with the top level attribute: +/// ``` +/// # use parity_scale_codec_derive::Encode; +/// # use parity_scale_codec::MaxEncodedLen; +/// # #[derive(Encode, MaxEncodedLen)] +/// #[codec(mel_bound(T: MaxEncodedLen))] +/// # struct MyWrapper(T); +/// ``` #[cfg(feature = "max-encoded-len")] #[proc_macro_derive(MaxEncodedLen, attributes(max_encoded_len_mod))] pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {