Skip to content

Commit

Permalink
Add some doc for the derive macro (#301)
Browse files Browse the repository at this point in the history
* some doc

* Update derive/src/lib.rs

Co-authored-by: David <dvdplm@gmail.com>

* fmt

Co-authored-by: David <dvdplm@gmail.com>
  • Loading branch information
gui1117 and dvdplm committed Nov 24, 2021
1 parent baa5863 commit 94f5887
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 22 additions & 3 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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>(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 {
Expand Down

0 comments on commit 94f5887

Please sign in to comment.