mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 06:51:07 +00:00
Simple MaxBoundedLen Implementations (#8793)
* implement max_values + storages info * some formatting + doc * sudo sanity check * timestamp * assets (not working) * fix assets * impl for proxy * update balances * rename StoragesInfo -> PalletStorageInfo * merge both StorageInfoTrait and PalletStorageInfo I think it is more future proof. In the future some storage could make use of multiple prefix. Like one to store how much value has been inserted, etc... * Update frame/support/procedural/src/storage/parse.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * Update frame/support/procedural/src/storage/storage_struct.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * Fix max_size using hasher information hasher now expose `max_len` which allows to computes their maximum len. For hasher without concatenation, it is the size of the hash part, for hasher with concatenation, it is the size of the hash part + max encoded len of the key. * fix tests * fix ui tests * 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 <shawntabrizi@gmail.com> * nits * fix compile * line width * fix max-values-macro merge * Add some derive, needed for test and other purpose * use weak bounded vec in some cases * Update lib.rs * move max-encoded-len crate * fix * remove app crypto for now * width * Revert "remove app crypto for now" This reverts commit 73623e9933d50648e0e7fe90b6171a8e45d7f5a2. * unused variable * more unused variables * more fixes * Add #[max_encoded_len_crate(...)] helper attribute The purpose of this attribute is to reduce the surface area of max_encoded_len changes. Crates deriving `MaxEncodedLen` do not need to add it to `Cargo.toml`; they can instead just do ```rust \#[derive(Encode, MaxEncodedLen)] \#[max_encoded_len_crate(frame_support::max_encoded_len)] struct Example; ``` * fix a ui test * use #[max_encoded_len_crate(...)] helper in app_crypto * remove max_encoded_len import where not necessary * update lockfile * fix ui test * ui * newline * fix merge * try fix ui again * Update max-encoded-len/derive/src/lib.rs Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> * extract generate_crate_access_2018 * Update lib.rs * compiler isnt smart enough Co-authored-by: thiolliere <gui.thiolliere@gmail.com> Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> Co-authored-by: Peter Goodspeed-Niklaus <peter.r.goodspeedniklaus@gmail.com>
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
[package]
|
||||
name = "max-encoded-len"
|
||||
version = "3.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://substrate.dev"
|
||||
repository = "https://github.com/paritytech/substrate/"
|
||||
description = "Trait MaxEncodedLen bounds the max encoded length of an item."
|
||||
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||
impl-trait-for-tuples = "0.2.1"
|
||||
max-encoded-len-derive = { package = "max-encoded-len-derive", version = "3.0.0", path = "derive", default-features = false, optional = true }
|
||||
primitive-types = { version = "0.9.0", default-features = false, features = ["codec"] }
|
||||
|
||||
[dev-dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [ "derive" ] }
|
||||
frame-support = { path = "../frame/support" }
|
||||
rustversion = "1.0.4"
|
||||
trybuild = "1.0.42"
|
||||
|
||||
[features]
|
||||
default = [
|
||||
"derive",
|
||||
"std",
|
||||
]
|
||||
derive = [
|
||||
"max-encoded-len-derive",
|
||||
]
|
||||
std = [
|
||||
"codec/std",
|
||||
"max-encoded-len-derive/std",
|
||||
"primitive-types/std",
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "max-encoded-len-derive"
|
||||
version = "3.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://substrate.dev"
|
||||
repository = "https://github.com/paritytech/substrate/"
|
||||
description = "Derive support for MaxEncodedLen"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0.6"
|
||||
proc-macro-crate = "1.0.0"
|
||||
quote = "1.0.3"
|
||||
syn = { version = "1.0.58", features = ["full"] }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
@@ -0,0 +1,204 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 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.
|
||||
|
||||
use quote::{quote, quote_spanned};
|
||||
use syn::{
|
||||
Data, DeriveInput, Error, Fields, GenericParam, Generics, Meta, TraitBound, Type,
|
||||
TypeParamBound, parse_quote, spanned::Spanned,
|
||||
};
|
||||
use proc_macro_crate::{crate_name, FoundCrate};
|
||||
use proc_macro2::{Ident, Span};
|
||||
|
||||
/// Generate the crate access for the crate using 2018 syntax.
|
||||
fn generate_crate_access_2018(def_crate: &str) -> Result<syn::Ident, Error> {
|
||||
match crate_name(def_crate) {
|
||||
Ok(FoundCrate::Itself) => {
|
||||
let name = def_crate.to_string().replace("-", "_");
|
||||
Ok(syn::Ident::new(&name, Span::call_site()))
|
||||
},
|
||||
Ok(FoundCrate::Name(name)) => {
|
||||
Ok(Ident::new(&name, Span::call_site()))
|
||||
},
|
||||
Err(e) => {
|
||||
Err(Error::new(Span::call_site(), e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Derive `MaxEncodedLen`.
|
||||
#[proc_macro_derive(MaxEncodedLen, attributes(max_encoded_len_crate))]
|
||||
pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input: DeriveInput = match syn::parse(input) {
|
||||
Ok(input) => input,
|
||||
Err(e) => return e.to_compile_error().into(),
|
||||
};
|
||||
|
||||
let mel_trait = match max_encoded_len_trait(&input) {
|
||||
Ok(mel_trait) => mel_trait,
|
||||
Err(e) => return e.to_compile_error().into(),
|
||||
};
|
||||
|
||||
let name = &input.ident;
|
||||
let generics = add_trait_bounds(input.generics, mel_trait.clone());
|
||||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
||||
|
||||
let data_expr = data_length_expr(&input.data);
|
||||
|
||||
quote::quote!(
|
||||
const _: () = {
|
||||
impl #impl_generics #mel_trait for #name #ty_generics #where_clause {
|
||||
fn max_encoded_len() -> usize {
|
||||
#data_expr
|
||||
}
|
||||
}
|
||||
};
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn max_encoded_len_trait(input: &DeriveInput) -> syn::Result<TraitBound> {
|
||||
let mel = {
|
||||
const EXPECT_LIST: &str = "expect: #[max_encoded_len_crate(path::to::crate)]";
|
||||
const EXPECT_PATH: &str = "expect: path::to::crate";
|
||||
|
||||
macro_rules! return_err {
|
||||
($wrong_style:expr, $err:expr) => {
|
||||
return Err(Error::new($wrong_style.span(), $err))
|
||||
};
|
||||
}
|
||||
|
||||
let mut mel_crates = Vec::with_capacity(2);
|
||||
mel_crates.extend(input
|
||||
.attrs
|
||||
.iter()
|
||||
.filter(|attr| attr.path == parse_quote!(max_encoded_len_crate))
|
||||
.take(2)
|
||||
.map(|attr| {
|
||||
let meta_list = match attr.parse_meta()? {
|
||||
Meta::List(meta_list) => meta_list,
|
||||
Meta::Path(wrong_style) => return_err!(wrong_style, EXPECT_LIST),
|
||||
Meta::NameValue(wrong_style) => return_err!(wrong_style, EXPECT_LIST),
|
||||
};
|
||||
if meta_list.nested.len() != 1 {
|
||||
return_err!(meta_list, "expected exactly 1 item");
|
||||
}
|
||||
let first_nested =
|
||||
meta_list.nested.into_iter().next().expect("length checked above");
|
||||
let meta = match first_nested {
|
||||
syn::NestedMeta::Lit(l) => {
|
||||
return_err!(l, "expected a path item, not a literal")
|
||||
}
|
||||
syn::NestedMeta::Meta(meta) => meta,
|
||||
};
|
||||
let path = match meta {
|
||||
Meta::Path(path) => path,
|
||||
Meta::List(ref wrong_style) => return_err!(wrong_style, EXPECT_PATH),
|
||||
Meta::NameValue(ref wrong_style) => return_err!(wrong_style, EXPECT_PATH),
|
||||
};
|
||||
Ok(path)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?);
|
||||
|
||||
// we have to return `Result<Ident, Error>` here in order to satisfy the trait
|
||||
// bounds for `.or_else` for `generate_crate_access_2018`, even though `Option<Ident>`
|
||||
// would be more natural in this circumstance.
|
||||
match mel_crates.len() {
|
||||
0 => Err(Error::new(
|
||||
input.span(),
|
||||
"this error is spurious and swallowed by the or_else below",
|
||||
)),
|
||||
1 => Ok(mel_crates.into_iter().next().expect("length is checked")),
|
||||
_ => return_err!(mel_crates[1], "duplicate max_encoded_len_crate definition"),
|
||||
}
|
||||
}
|
||||
.or_else(|_| generate_crate_access_2018("max-encoded-len").map(|ident| ident.into()))?;
|
||||
Ok(parse_quote!(#mel::MaxEncodedLen))
|
||||
}
|
||||
|
||||
// Add a bound `T: MaxEncodedLen` to every type parameter T.
|
||||
fn add_trait_bounds(mut generics: Generics, mel_trait: TraitBound) -> Generics {
|
||||
for param in &mut generics.params {
|
||||
if let GenericParam::Type(ref mut type_param) = *param {
|
||||
type_param.bounds.push(TypeParamBound::Trait(mel_trait.clone()));
|
||||
}
|
||||
}
|
||||
generics
|
||||
}
|
||||
|
||||
/// generate an expression to sum up the max encoded length from several fields
|
||||
fn fields_length_expr(fields: &Fields) -> proc_macro2::TokenStream {
|
||||
let type_iter: Box<dyn Iterator<Item = &Type>> = match fields {
|
||||
Fields::Named(ref fields) => Box::new(fields.named.iter().map(|field| &field.ty)),
|
||||
Fields::Unnamed(ref fields) => Box::new(fields.unnamed.iter().map(|field| &field.ty)),
|
||||
Fields::Unit => Box::new(std::iter::empty()),
|
||||
};
|
||||
// expands to an expression like
|
||||
//
|
||||
// 0
|
||||
// .saturating_add(<type of first field>::max_encoded_len())
|
||||
// .saturating_add(<type of second field>::max_encoded_len())
|
||||
//
|
||||
// We match the span of each field to the span of the corresponding
|
||||
// `max_encoded_len` call. This way, if one field's type doesn't implement
|
||||
// `MaxEncodedLen`, the compiler's error message will underline which field
|
||||
// caused the issue.
|
||||
let expansion = type_iter.map(|ty| {
|
||||
quote_spanned! {
|
||||
ty.span() => .saturating_add(<#ty>::max_encoded_len())
|
||||
}
|
||||
});
|
||||
quote! {
|
||||
0_usize #( #expansion )*
|
||||
}
|
||||
}
|
||||
|
||||
// generate an expression to sum up the max encoded length of each field
|
||||
fn data_length_expr(data: &Data) -> proc_macro2::TokenStream {
|
||||
match *data {
|
||||
Data::Struct(ref data) => fields_length_expr(&data.fields),
|
||||
Data::Enum(ref data) => {
|
||||
// We need an expression expanded for each variant like
|
||||
//
|
||||
// 0
|
||||
// .max(<variant expression>)
|
||||
// .max(<variant expression>)
|
||||
// .saturating_add(1)
|
||||
//
|
||||
// The 1 derives from the discriminant; see
|
||||
// https://github.com/paritytech/parity-scale-codec/
|
||||
// blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/derive/src/encode.rs#L211-L216
|
||||
//
|
||||
// Each variant expression's sum is computed the way an equivalent struct's would be.
|
||||
|
||||
let expansion = data.variants.iter().map(|variant| {
|
||||
let variant_expression = fields_length_expr(&variant.fields);
|
||||
quote! {
|
||||
.max(#variant_expression)
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
0_usize #( #expansion )* .saturating_add(1)
|
||||
}
|
||||
}
|
||||
Data::Union(ref data) => {
|
||||
// https://github.com/paritytech/parity-scale-codec/
|
||||
// blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/derive/src/encode.rs#L290-L293
|
||||
Error::new(data.union_token.span(), "Union types are not supported").to_compile_error()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 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.
|
||||
|
||||
//! `trait MaxEncodedLen` bounds the max encoded length of items.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use codec::{Compact, Encode};
|
||||
use impl_trait_for_tuples::impl_for_tuples;
|
||||
use core::{mem, marker::PhantomData};
|
||||
use primitive_types::{H160, H256, H512};
|
||||
|
||||
/// Derive macro for `MaxEncodedLen`.
|
||||
///
|
||||
/// ```
|
||||
/// # use max_encoded_len::MaxEncodedLen;
|
||||
/// # use codec::Encode;
|
||||
/// #[derive(Encode, MaxEncodedLen)]
|
||||
/// struct Example;
|
||||
/// ```
|
||||
///
|
||||
/// Sometimes the `MaxEncodedLen` trait and macro are accessed without explicitly importing its
|
||||
/// crate, notably via the `frame_support::max_encoded_len` re-binding. In these circumstances,
|
||||
/// the derive macro needs some help to understand where its crate should be:
|
||||
///
|
||||
/// ```
|
||||
/// # use codec::Encode;
|
||||
/// use frame_support::max_encoded_len::MaxEncodedLen;
|
||||
///
|
||||
/// #[derive(Encode, MaxEncodedLen)]
|
||||
/// #[max_encoded_len_crate(frame_support::max_encoded_len)]
|
||||
/// struct Example;
|
||||
/// ```
|
||||
#[cfg(feature = "derive")]
|
||||
pub use max_encoded_len_derive::MaxEncodedLen;
|
||||
|
||||
/// Items implementing `MaxEncodedLen` have a statically known maximum encoded size.
|
||||
///
|
||||
/// Some containers, such as `BoundedVec`, have enforced size limits and this trait
|
||||
/// can be implemented accurately. Other containers, such as `StorageMap`, do not have enforced size
|
||||
/// limits. For those containers, it is necessary to make a documented assumption about the maximum
|
||||
/// usage, and compute the max encoded length based on that assumption.
|
||||
pub trait MaxEncodedLen: Encode {
|
||||
/// Upper bound, in bytes, of the maximum encoded size of this item.
|
||||
fn max_encoded_len() -> usize;
|
||||
}
|
||||
|
||||
macro_rules! impl_primitives {
|
||||
( $($t:ty),+ ) => {
|
||||
$(
|
||||
impl MaxEncodedLen for $t {
|
||||
fn max_encoded_len() -> usize {
|
||||
mem::size_of::<$t>()
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
impl_primitives!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool, H160, H256, H512);
|
||||
|
||||
macro_rules! impl_compact {
|
||||
($( $t:ty => $e:expr; )*) => {
|
||||
$(
|
||||
impl MaxEncodedLen for Compact<$t> {
|
||||
fn max_encoded_len() -> usize {
|
||||
$e
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_compact!(
|
||||
// github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L261
|
||||
u8 => 2;
|
||||
// github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L291
|
||||
u16 => 4;
|
||||
// github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L326
|
||||
u32 => 5;
|
||||
// github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L369
|
||||
u64 => 9;
|
||||
// github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L413
|
||||
u128 => 17;
|
||||
);
|
||||
|
||||
// impl_for_tuples for values 19 and higher fails because that's where the WrapperTypeEncode impl stops.
|
||||
#[impl_for_tuples(18)]
|
||||
impl MaxEncodedLen for Tuple {
|
||||
fn max_encoded_len() -> usize {
|
||||
let mut len: usize = 0;
|
||||
for_tuples!( #( len = len.saturating_add(Tuple::max_encoded_len()); )* );
|
||||
len
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: MaxEncodedLen, const N: usize> MaxEncodedLen for [T; N] {
|
||||
fn max_encoded_len() -> usize {
|
||||
T::max_encoded_len().saturating_mul(N)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: MaxEncodedLen> MaxEncodedLen for Option<T> {
|
||||
fn max_encoded_len() -> usize {
|
||||
T::max_encoded_len().saturating_add(1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> MaxEncodedLen for Result<T, E>
|
||||
where
|
||||
T: MaxEncodedLen,
|
||||
E: MaxEncodedLen,
|
||||
{
|
||||
fn max_encoded_len() -> usize {
|
||||
T::max_encoded_len().max(E::max_encoded_len()).saturating_add(1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> MaxEncodedLen for PhantomData<T> {
|
||||
fn max_encoded_len() -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
macro_rules! test_compact_length {
|
||||
($(fn $name:ident($t:ty);)*) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
assert_eq!(Compact(<$t>::MAX).encode().len(), Compact::<$t>::max_encoded_len());
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
test_compact_length!(
|
||||
fn compact_u8(u8);
|
||||
fn compact_u16(u16);
|
||||
fn compact_u32(u32);
|
||||
fn compact_u64(u64);
|
||||
fn compact_u128(u128);
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
// 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
|
||||
|
||||
#![cfg(feature = "derive")]
|
||||
|
||||
use max_encoded_len::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,27 @@
|
||||
// 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.
|
||||
|
||||
#[cfg(feature = "derive")]
|
||||
#[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,10 @@
|
||||
use codec::Encode;
|
||||
use frame_support::max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[max_encoded_len_crate(foo())]
|
||||
struct Example;
|
||||
|
||||
fn main() {
|
||||
let _ = Example::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
error: expect: path::to::crate
|
||||
--> $DIR/list_list_item.rs:5:25
|
||||
|
|
||||
5 | #[max_encoded_len_crate(foo())]
|
||||
| ^^^
|
||||
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `Example` in the current scope
|
||||
--> $DIR/list_list_item.rs:9:19
|
||||
|
|
||||
6 | struct Example;
|
||||
| --------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | let _ = Example::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item not found in `Example`
|
||||
|
|
||||
= 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::max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[max_encoded_len_crate("frame_support::max_encoded_len")]
|
||||
struct Example;
|
||||
|
||||
fn main() {
|
||||
let _ = Example::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
error: expected a path item, not a literal
|
||||
--> $DIR/literal_list_item.rs:5:25
|
||||
|
|
||||
5 | #[max_encoded_len_crate("frame_support::max_encoded_len")]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `Example` in the current scope
|
||||
--> $DIR/literal_list_item.rs:9:19
|
||||
|
|
||||
6 | struct Example;
|
||||
| --------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | let _ = Example::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item not found in `Example`
|
||||
|
|
||||
= 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::max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[max_encoded_len_crate = "frame_support::max_encoded_len"]
|
||||
struct Example;
|
||||
|
||||
fn main() {
|
||||
let _ = Example::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
error: expect: #[max_encoded_len_crate(path::to::crate)]
|
||||
--> $DIR/name_value_attr.rs:5:3
|
||||
|
|
||||
5 | #[max_encoded_len_crate = "frame_support::max_encoded_len"]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `Example` in the current scope
|
||||
--> $DIR/name_value_attr.rs:9:19
|
||||
|
|
||||
6 | struct Example;
|
||||
| --------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | let _ = Example::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item not found in `Example`
|
||||
|
|
||||
= 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::max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[max_encoded_len_crate(path = "frame_support::max_encoded_len")]
|
||||
struct Example;
|
||||
|
||||
fn main() {
|
||||
let _ = Example::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
error: expect: path::to::crate
|
||||
--> $DIR/name_value_list_item.rs:5:25
|
||||
|
|
||||
5 | #[max_encoded_len_crate(path = "frame_support::max_encoded_len")]
|
||||
| ^^^^
|
||||
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `Example` in the current scope
|
||||
--> $DIR/name_value_list_item.rs:9:19
|
||||
|
|
||||
6 | struct Example;
|
||||
| --------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | let _ = Example::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item not found in `Example`
|
||||
|
|
||||
= 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::max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[max_encoded_len_crate]
|
||||
struct Example;
|
||||
|
||||
fn main() {
|
||||
let _ = Example::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
error: expect: #[max_encoded_len_crate(path::to::crate)]
|
||||
--> $DIR/no_path_list_items.rs:5:3
|
||||
|
|
||||
5 | #[max_encoded_len_crate]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `Example` in the current scope
|
||||
--> $DIR/no_path_list_items.rs:9:19
|
||||
|
|
||||
6 | struct Example;
|
||||
| --------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | let _ = Example::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item not found in `Example`
|
||||
|
|
||||
= 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,6 @@
|
||||
use max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(MaxEncodedLen)]
|
||||
struct NotEncode;
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,13 @@
|
||||
error[E0277]: the trait bound `NotEncode: parity_scale_codec::codec::WrapperTypeEncode` is not satisfied
|
||||
--> $DIR/not_encode.rs:3:10
|
||||
|
|
||||
3 | #[derive(MaxEncodedLen)]
|
||||
| ^^^^^^^^^^^^^ the trait `parity_scale_codec::codec::WrapperTypeEncode` is not implemented for `NotEncode`
|
||||
|
|
||||
::: $WORKSPACE/max-encoded-len/src/lib.rs
|
||||
|
|
||||
| pub trait MaxEncodedLen: Encode {
|
||||
| ------ required by this bound in `MaxEncodedLen`
|
||||
|
|
||||
= note: required because of the requirements on the impl of `parity_scale_codec::codec::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 max_encoded_len::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::max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[max_encoded_len_crate]
|
||||
struct Example;
|
||||
|
||||
fn main() {
|
||||
let _ = Example::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
error: expect: #[max_encoded_len_crate(path::to::crate)]
|
||||
--> $DIR/path_attr.rs:5:3
|
||||
|
|
||||
5 | #[max_encoded_len_crate]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `Example` in the current scope
|
||||
--> $DIR/path_attr.rs:9:19
|
||||
|
|
||||
6 | struct Example;
|
||||
| --------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | let _ = Example::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item not found in `Example`
|
||||
|
|
||||
= 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::max_encoded_len::MaxEncodedLen;
|
||||
|
||||
#[derive(Encode, MaxEncodedLen)]
|
||||
#[max_encoded_len_crate(max_encoded_len, frame_support::max_encoded_len)]
|
||||
struct Example;
|
||||
|
||||
fn main() {
|
||||
let _ = Example::max_encoded_len();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
error: expected exactly 1 item
|
||||
--> $DIR/two_path_list_items.rs:5:3
|
||||
|
|
||||
5 | #[max_encoded_len_crate(max_encoded_len, frame_support::max_encoded_len)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0599]: no function or associated item named `max_encoded_len` found for struct `Example` in the current scope
|
||||
--> $DIR/two_path_list_items.rs:9:19
|
||||
|
|
||||
6 | struct Example;
|
||||
| --------------- function or associated item `max_encoded_len` not found for this
|
||||
...
|
||||
9 | let _ = Example::max_encoded_len();
|
||||
| ^^^^^^^^^^^^^^^ function or associated item not found in `Example`
|
||||
|
|
||||
= 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 max_encoded_len::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 max_encoded_len::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