Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 2001d32

Browse files
Move MaxBoundedLen into its own crate (#8814)
* move MaxEncodedLen into its own crate * remove MaxEncodedLen impl from frame-support * add to assets and balances * try more fixes * fix compile Co-authored-by: Shawn Tabrizi <[email protected]>
1 parent 43edcac commit 2001d32

File tree

28 files changed

+143
-60
lines changed

28 files changed

+143
-60
lines changed

Cargo.lock

Lines changed: 31 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ members = [
9090
"frame/im-online",
9191
"frame/indices",
9292
"frame/lottery",
93+
"frame/max-encoded-len",
94+
"frame/max-encoded-len/derive",
9395
"frame/membership",
9496
"frame/merkle-mountain-range",
9597
"frame/merkle-mountain-range/primitives",

bin/node/runtime/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ pallet-transaction-payment = { version = "3.0.0", default-features = false, path
8787
pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" }
8888
pallet-vesting = { version = "3.0.0", default-features = false, path = "../../../frame/vesting" }
8989

90+
max-encoded-len = { version = "3.0.0", default-features = false, path = "../../../frame/max-encoded-len", features = [ "derive" ] }
91+
9092
[build-dependencies]
9193
substrate-wasm-builder = { version = "4.0.0", path = "../../../utils/wasm-builder" }
9294

@@ -159,6 +161,7 @@ std = [
159161
"log/std",
160162
"frame-try-runtime/std",
161163
"sp-npos-elections/std",
164+
"max-encoded-len/std",
162165
]
163166
runtime-benchmarks = [
164167
"frame-benchmarking",

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use frame_support::{
3333
},
3434
traits::{
3535
Currency, Imbalance, KeyOwnerProofSystem, OnUnbalanced, LockIdentifier,
36-
U128CurrencyToVote,
36+
U128CurrencyToVote, MaxEncodedLen,
3737
},
3838
};
3939
use frame_system::{
@@ -252,7 +252,7 @@ parameter_types! {
252252
}
253253

254254
/// The type used to represent the kinds of proxying allowed.
255-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug)]
255+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, MaxEncodedLen)]
256256
pub enum ProxyType {
257257
Any,
258258
NonTransfer,

frame/assets/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ frame-support = { version = "3.0.0", default-features = false, path = "../suppor
2222
# `system` module provides us with all sorts of useful stuff and macros depend on it being around.
2323
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
2424
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
25+
max-encoded-len = { version = "3.0.0", default-features = false, path = "../max-encoded-len", features = [ "derive" ] }
2526

2627
[dev-dependencies]
2728
sp-core = { version = "3.0.0", path = "../../primitives/core" }
@@ -38,6 +39,7 @@ std = [
3839
"frame-support/std",
3940
"frame-system/std",
4041
"frame-benchmarking/std",
42+
"max-encoded-len/std",
4143
]
4244
runtime-benchmarks = [
4345
"frame-benchmarking",

frame/balances/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b
2020
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
2121
frame-system = { version = "3.0.0", default-features = false, path = "../system" }
2222
log = { version = "0.4.14", default-features = false }
23+
max-encoded-len = { version = "3.0.0", default-features = false, path = "../max-encoded-len", features = [ "derive" ] }
2324

2425
[dev-dependencies]
2526
sp-io = { version = "3.0.0", path = "../../primitives/io" }
@@ -36,6 +37,7 @@ std = [
3637
"frame-support/std",
3738
"frame-system/std",
3839
"log/std",
40+
"max-encoded-len/std",
3941
]
4042
runtime-benchmarks = ["frame-benchmarking"]
4143
try-runtime = ["frame-support/try-runtime"]

frame/max-encoded-len/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "max-encoded-len"
3+
version = "3.0.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2018"
6+
license = "Apache-2.0"
7+
homepage = "https://substrate.dev"
8+
repository = "https://github.com/paritytech/substrate/"
9+
description = "Trait MaxEncodedLen bounds the max encoded length of an item."
10+
11+
12+
[dependencies]
13+
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
14+
impl-trait-for-tuples = "0.2.1"
15+
max-encoded-len-derive = { package = "max-encoded-len-derive", version = "3.0.0", path = "derive", default-features = false, optional = true }
16+
primitive-types = { version = "0.9.0", default-features = false, features = ["codec"] }
17+
18+
[dev-dependencies]
19+
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [ "derive" ] }
20+
rustversion = "1.0.4"
21+
trybuild = "1.0.42"
22+
23+
[features]
24+
default = [
25+
"derive",
26+
"std",
27+
]
28+
derive = [
29+
"max-encoded-len-derive",
30+
]
31+
std = [
32+
"codec/std",
33+
"max-encoded-len-derive/std",
34+
"primitive-types/std",
35+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "max-encoded-len-derive"
3+
version = "3.0.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2018"
6+
license = "Apache-2.0"
7+
homepage = "https://substrate.dev"
8+
repository = "https://github.com/paritytech/substrate/"
9+
description = "Derive support for MaxEncodedLen"
10+
11+
[package.metadata.docs.rs]
12+
targets = ["x86_64-unknown-linux-gnu"]
13+
14+
[lib]
15+
proc-macro = true
16+
17+
[dependencies]
18+
frame-support-procedural-tools = { version = "3.0.0", path = "../../support/procedural/tools" }
19+
proc-macro2 = "1.0.6"
20+
quote = "1.0.3"
21+
syn = { version = "1.0.58", features = ["full"] }
22+
23+
[features]
24+
default = ["std"]
25+
std = []

frame/support/procedural/src/max_encoded_len.rs renamed to frame/max-encoded-len/derive/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use syn::{
2222
parse_quote, spanned::Spanned,
2323
};
2424

25-
/// impl for `#[derive(MaxEncodedLen)]`
25+
26+
/// Derive `MaxEncodedLen`.
27+
#[proc_macro_derive(MaxEncodedLen)]
2628
pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
2729
let input: DeriveInput = match syn::parse(input) {
2830
Ok(input) => input,
@@ -53,8 +55,8 @@ pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::Tok
5355
}
5456

5557
fn max_encoded_len_trait() -> syn::Result<TraitBound> {
56-
let frame_support = generate_crate_access_2018("frame-support")?;
57-
Ok(parse_quote!(#frame_support::traits::MaxEncodedLen))
58+
let mel = generate_crate_access_2018("max-encoded-len")?;
59+
Ok(parse_quote!(#mel::MaxEncodedLen))
5860
}
5961

6062
// Add a bound `T: MaxEncodedLen` to every type parameter T.

frame/support/src/traits/max_encoded_len.rs renamed to frame/max-encoded-len/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of Substrate.
22

3-
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
3+
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
44
// SPDX-License-Identifier: Apache-2.0
55

66
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,10 +15,17 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18+
//! `trait MaxEncodedLen` bounds the max encoded length of items.
19+
20+
#![cfg_attr(not(feature = "std"), no_std)]
21+
1822
use codec::{Compact, Encode};
1923
use impl_trait_for_tuples::impl_for_tuples;
20-
use sp_std::{mem, marker::PhantomData};
21-
use sp_core::{H160, H256, H512};
24+
use core::{mem, marker::PhantomData};
25+
use primitive_types::{H160, H256, H512};
26+
27+
#[cfg(feature = "derive")]
28+
pub use max_encoded_len_derive::MaxEncodedLen;
2229

2330
/// Items implementing `MaxEncodedLen` have a statically known maximum encoded size.
2431
///

frame/support/test/tests/max_encoded_len.rs renamed to frame/max-encoded-len/tests/max_encoded_len.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
//! Tests for MaxEncodedLen derive macro
1919
20-
use frame_support::traits::MaxEncodedLen;
20+
#![cfg(feature = "derive")]
21+
22+
use max_encoded_len::MaxEncodedLen;
2123
use codec::{Compact, Encode};
2224

2325
// These structs won't even compile if the macro isn't working right.

frame/support/test/tests/max_encoded_len_ui.rs renamed to frame/max-encoded-len/tests/max_encoded_len_ui.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18+
#[cfg(feature = "derive")]
1819
#[rustversion::attr(not(stable), ignore)]
1920
#[test]
2021
fn derive_no_bound_ui() {

frame/support/test/tests/max_encoded_len_ui/not_encode.rs renamed to frame/max-encoded-len/tests/max_encoded_len_ui/not_encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use frame_support::traits::MaxEncodedLen;
1+
use max_encoded_len::MaxEncodedLen;
22

33
#[derive(MaxEncodedLen)]
44
struct NotEncode;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0277]: the trait bound `NotEncode: WrapperTypeEncode` is not satisfied
1+
error[E0277]: the trait bound `NotEncode: parity_scale_codec::codec::WrapperTypeEncode` is not satisfied
22
--> $DIR/not_encode.rs:3:10
33
|
44
3 | #[derive(MaxEncodedLen)]
5-
| ^^^^^^^^^^^^^ the trait `WrapperTypeEncode` is not implemented for `NotEncode`
5+
| ^^^^^^^^^^^^^ the trait `parity_scale_codec::codec::WrapperTypeEncode` is not implemented for `NotEncode`
66
|
7-
::: $WORKSPACE/frame/support/src/traits/max_encoded_len.rs
7+
::: $WORKSPACE/frame/max-encoded-len/src/lib.rs
88
|
99
| pub trait MaxEncodedLen: Encode {
1010
| ------ required by this bound in `MaxEncodedLen`
1111
|
12-
= note: required because of the requirements on the impl of `frame_support::dispatch::Encode` for `NotEncode`
12+
= note: required because of the requirements on the impl of `parity_scale_codec::codec::Encode` for `NotEncode`
1313
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

frame/support/test/tests/max_encoded_len_ui/not_mel.rs renamed to frame/max-encoded-len/tests/max_encoded_len_ui/not_mel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use codec::Encode;
2-
use frame_support::traits::MaxEncodedLen;
2+
use max_encoded_len::MaxEncodedLen;
33

44
#[derive(Encode)]
55
struct NotMel;

frame/support/test/tests/max_encoded_len_ui/union.rs renamed to frame/max-encoded-len/tests/max_encoded_len_ui/union.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use codec::Encode;
2-
use frame_support::traits::MaxEncodedLen;
2+
use max_encoded_len::MaxEncodedLen;
33

44
#[derive(Encode, MaxEncodedLen)]
55
union Union {

frame/support/test/tests/max_encoded_len_ui/unsupported_variant.rs renamed to frame/max-encoded-len/tests/max_encoded_len_ui/unsupported_variant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use codec::Encode;
2-
use frame_support::traits::MaxEncodedLen;
2+
use max_encoded_len::MaxEncodedLen;
33

44
#[derive(Encode)]
55
struct NotMel;

frame/proxy/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ sp-core = { version = "3.0.0", default-features = false, path = "../../primitive
2020
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
2121
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
2222
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
23+
max-encoded-len = { version = "3.0.0", default-features = false, path = "../max-encoded-len", features = [ "derive" ] }
2324

2425
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
2526

@@ -36,7 +37,8 @@ std = [
3637
"frame-support/std",
3738
"frame-system/std",
3839
"sp-std/std",
39-
"sp-io/std"
40+
"sp-io/std",
41+
"max-encoded-len/std",
4042
]
4143
runtime-benchmarks = [
4244
"frame-benchmarking",

frame/support/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1616
serde = { version = "1.0.101", optional = true, features = ["derive"] }
1717
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
1818
frame-metadata = { version = "13.0.0", default-features = false, path = "../metadata" }
19+
max-encoded-len = { version = "3.0.0", default-features = false, path = "../max-encoded-len", features = [ "derive" ] }
1920
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
2021
sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/io" }
2122
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
@@ -55,6 +56,7 @@ std = [
5556
"sp-state-machine",
5657
"frame-support-procedural/std",
5758
"log/std",
59+
"max-encoded-len/std",
5860
]
5961
runtime-benchmarks = []
6062
try-runtime = []

0 commit comments

Comments
 (0)