buidl_derive/
lib.rs

1// Copyright (c) Jeeyong Um <conr2d@proton.me>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Custom derive for buidl.
11
12use crate::fixed_bytes::expand_fixed_bytes;
13use proc_macro::TokenStream;
14use syn::{parse_macro_input, DeriveInput};
15
16mod fixed_bytes;
17mod utils;
18
19/// Derives traits for a fixed-size byte array.
20///
21/// This macro derives traits for structs or tuples where the first field is a fixed-size array of
22/// `u8`. The target type can have additional fields, which will be initialized with their default
23/// values. A custom default value can be specified by using the `#[buidl(default = $expr)]`
24/// attribute.
25///
26/// ```
27/// # use buidl_derive::FixedBytes;
28/// #[derive(FixedBytes)]
29/// struct Bytes32 {
30///   data: [u8; 32],
31///   #[buidl(default = true)]
32///   dirty: bool,
33/// }
34/// ```
35///
36/// ## The derived traits
37///
38/// ### Core traits
39/// - [`Clone`]
40/// - [`PartialEq`], [`Eq`], [`PartialOrd`], [`Ord`]
41/// - [`From`], [`TryFrom`], [`AsRef`], [`AsMut`]
42/// - [`Hash`]
43/// - [`Deref`], [`DerefMut`]
44///
45/// [`Hash`]: core::hash::Hash
46/// [`Deref`]: core::ops::Deref
47/// [`DerefMut`]: core::ops::DerefMut
48///
49/// ### Polkadot SDK traits (Optional)
50///
51/// Polkadot SDK traits are derived when the `derive(Substrate)` attribute is specified.
52///
53/// ```
54/// # use buidl_derive::FixedBytes;
55/// # use core::marker::PhantomData;
56/// #[derive(FixedBytes)]
57/// #[buidl(derive(Substrate))]
58/// struct CryptoBytes<const N: usize, T = ()>([u8; N], PhantomData<fn() -> T>);
59/// ```
60///
61/// - [`ByteArray`], [`UncheckedFrom`]
62/// - `PassBy`: [`PassBy`], [`PassByInner`]
63/// - `Codec`: [`Encode`], [`EncodeLike`], [`Decode`], `MaxEncodedLen`, [`FromEntropy`]
64/// - `TypeInfo`: [`TypeInfo`]
65///
66/// [`ByteArray`]: https://docs.rs/sp-core/32.0.0/sp_core/crypto/trait.ByteArray.html
67/// [`UncheckedFrom`]: https://docs.rs/sp-core/32.0.0/sp_core/crypto/trait.UncheckedFrom.html
68/// [`FromEntropy`]: https://docs.rs/sp-core/32.0.0/sp_core/crypto/trait.FromEntropy.html
69/// [`PassBy`]: https://docs.rs/sp-runtime-interface/27.0.0/sp_runtime_interface/pass_by/trait.PassBy.html
70/// [`PassByInner`]: https://docs.rs/sp-runtime-interface/27.0.0/sp_runtime_interface/pass_by/trait.PassByInner.html
71/// [`Decode`]: https://docs.rs/parity-scale-codec/3.6.12/parity_scale_codec/trait.Decode.html
72/// [`Encode`]: https://docs.rs/parity-scale-codec/3.6.12/parity_scale_codec/trait.Encode.html
73/// [`EncodeLike`]: https://docs.rs/parity-scale-codec/3.6.12/parity_scale_codec/trait.EncodeLike.html
74/// [`TypeInfo`]: https://docs.rs/scale-info/2.11.3/scale_info/trait.TypeInfo.html
75#[proc_macro_derive(FixedBytes, attributes(buidl))]
76pub fn derive_fixed_bytes(input: TokenStream) -> TokenStream {
77	let input = parse_macro_input!(input as DeriveInput);
78	expand_fixed_bytes(input).into()
79}