mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 11:07:56 +00:00
#[derive(MaxEncodedLen)] (#8737)
* impl #[derive(MaxEncodedLen)] for structs * impl #[derive(MaxEncodedLen)] for enums, unions * break long comments onto multiple lines * add doc for public item * add examples to macro documentation * move MaxEncodedLen macro docs, un-ignore doc-tests
This commit is contained in:
committed by
GitHub
parent
e1caa2979f
commit
17a1997d18
@@ -0,0 +1,149 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Tests for MaxEncodedLen derive macro
|
||||
|
||||
use frame_support::traits::MaxEncodedLen;
|
||||
use codec::{Compact, Encode};
|
||||
|
||||
// These structs won't even compile if the macro isn't working right.
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct Primitives {
|
||||
bool: bool,
|
||||
eight: u8,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn primitives_max_length() {
|
||||
assert_eq!(Primitives::max_encoded_len(), 2);
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct Composites {
|
||||
fixed_size_array: [u8; 128],
|
||||
tuple: (u128, u128),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composites_max_length() {
|
||||
assert_eq!(Composites::max_encoded_len(), 128 + 16 + 16);
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct Generic<T> {
|
||||
one: T,
|
||||
two: T,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_max_length() {
|
||||
assert_eq!(Generic::<u8>::max_encoded_len(), u8::max_encoded_len() * 2);
|
||||
assert_eq!(Generic::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct TwoGenerics<T, U> {
|
||||
t: T,
|
||||
u: U,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_generics_max_length() {
|
||||
assert_eq!(
|
||||
TwoGenerics::<u8, u16>::max_encoded_len(),
|
||||
u8::max_encoded_len() + u16::max_encoded_len()
|
||||
);
|
||||
assert_eq!(
|
||||
TwoGenerics::<Compact<u64>, [u16; 8]>::max_encoded_len(),
|
||||
Compact::<u64>::max_encoded_len() + <[u16; 8]>::max_encoded_len()
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct UnitStruct;
|
||||
|
||||
#[test]
|
||||
fn unit_struct_max_length() {
|
||||
assert_eq!(UnitStruct::max_encoded_len(), 0);
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct TupleStruct(u8, u32);
|
||||
|
||||
#[test]
|
||||
fn tuple_struct_max_length() {
|
||||
assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len());
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct TupleGeneric<T>(T, T);
|
||||
|
||||
#[test]
|
||||
fn tuple_generic_max_length() {
|
||||
assert_eq!(TupleGeneric::<u8>::max_encoded_len(), u8::max_encoded_len() * 2);
|
||||
assert_eq!(TupleGeneric::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[allow(unused)]
|
||||
enum UnitEnum {
|
||||
A,
|
||||
B,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unit_enum_max_length() {
|
||||
assert_eq!(UnitEnum::max_encoded_len(), 1);
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[allow(unused)]
|
||||
enum TupleEnum {
|
||||
A(u32),
|
||||
B,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tuple_enum_max_length() {
|
||||
assert_eq!(TupleEnum::max_encoded_len(), 1 + u32::max_encoded_len());
|
||||
}
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[allow(unused)]
|
||||
enum StructEnum {
|
||||
A { sixty_four: u64, one_twenty_eight: u128 },
|
||||
B,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn struct_enum_max_length() {
|
||||
assert_eq!(StructEnum::max_encoded_len(), 1 + u64::max_encoded_len() + u128::max_encoded_len());
|
||||
}
|
||||
|
||||
// ensure that enums take the max of variant length, not the sum
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[allow(unused)]
|
||||
enum EnumMaxNotSum {
|
||||
A(u32),
|
||||
B(u32),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enum_max_not_sum_max_length() {
|
||||
assert_eq!(EnumMaxNotSum::max_encoded_len(), 1 + u32::max_encoded_len());
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#[rustversion::attr(not(stable), ignore)]
|
||||
#[test]
|
||||
fn derive_no_bound_ui() {
|
||||
// As trybuild is using `cargo check`, we don't need the real WASM binaries.
|
||||
std::env::set_var("SKIP_WASM_BUILD", "1");
|
||||
|
||||
let t = trybuild::TestCases::new();
|
||||
t.compile_fail("tests/max_encoded_len_ui/*.rs");
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use frame_support::traits::MaxEncodedLen;
|
||||
|
||||
#[derive(MaxEncodedLen)]
|
||||
struct NotEncode;
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,13 @@
|
||||
error[E0277]: the trait bound `NotEncode: WrapperTypeEncode` is not satisfied
|
||||
--> $DIR/not_encode.rs:3:10
|
||||
|
|
||||
3 | #[derive(MaxEncodedLen)]
|
||||
| ^^^^^^^^^^^^^ the trait `WrapperTypeEncode` is not implemented for `NotEncode`
|
||||
|
|
||||
::: $WORKSPACE/frame/support/src/traits/max_encoded_len.rs
|
||||
|
|
||||
| pub trait MaxEncodedLen: Encode {
|
||||
| ------ required by this bound in `MaxEncodedLen`
|
||||
|
|
||||
= note: required because of the requirements on the impl of `frame_support::dispatch::Encode` for `NotEncode`
|
||||
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -0,0 +1,14 @@
|
||||
use codec::Encode;
|
||||
use frame_support::traits::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode)]
|
||||
struct NotMel;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
struct Generic<T> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = Generic::<NotMel>::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
error[E0599]: the function or associated item `max_encoded_len` exists for struct `Generic<NotMel>`, but its trait bounds were not satisfied
|
||||
--> $DIR/not_mel.rs:13:29
|
||||
|
|
||||
5 | struct NotMel;
|
||||
| -------------- doesn't satisfy `NotMel: MaxEncodedLen`
|
||||
...
|
||||
8 | struct Generic<T> {
|
||||
| -----------------
|
||||
| |
|
||||
| function or associated item `max_encoded_len` not found for this
|
||||
| doesn't satisfy `Generic<NotMel>: MaxEncodedLen`
|
||||
...
|
||||
13 | let _ = Generic::<NotMel>::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item cannot be called on `Generic<NotMel>` due to unsatisfied trait bounds
|
||||
|
|
||||
= note: the following trait bounds were not satisfied:
|
||||
`NotMel: MaxEncodedLen`
|
||||
which is required by `Generic<NotMel>: MaxEncodedLen`
|
||||
= help: items from traits can only be used if the trait is implemented and in scope
|
||||
= note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it:
|
||||
candidate #1: `MaxEncodedLen`
|
||||
@@ -0,0 +1,10 @@
|
||||
use codec::Encode;
|
||||
use frame_support::traits::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
union Union {
|
||||
a: u8,
|
||||
b: u16,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,11 @@
|
||||
error: Union types are not supported
|
||||
--> $DIR/union.rs:5:1
|
||||
|
|
||||
5 | union Union {
|
||||
| ^^^^^
|
||||
|
||||
error: Union types are not supported.
|
||||
--> $DIR/union.rs:5:1
|
||||
|
|
||||
5 | union Union {
|
||||
| ^^^^^
|
||||
@@ -0,0 +1,12 @@
|
||||
use codec::Encode;
|
||||
use frame_support::traits::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode)]
|
||||
struct NotMel;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
enum UnsupportedVariant {
|
||||
NotMel(NotMel),
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,12 @@
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `NotMel` in the current scope
|
||||
--> $DIR/unsupported_variant.rs:9:9
|
||||
|
|
||||
5 | struct NotMel;
|
||||
| -------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | NotMel(NotMel),
|
||||
| ^^^^^^ function or associated item not found in `NotMel`
|
||||
|
|
||||
= help: items from traits can only be used if the trait is implemented and in scope
|
||||
= note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it:
|
||||
candidate #1: `MaxEncodedLen`
|
||||
Reference in New Issue
Block a user