-
Notifications
You must be signed in to change notification settings - Fork 98
Implement BoundedEncodedLength trait and implementations on primitives
#267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7fd2069
formatting, dates
coriolinus 6118e4c
implement trait BoundedEncodedLen
coriolinus a051f46
impl BoundedEncodedLen for primitives
coriolinus 4cdae47
impl BoundedEncodedLen for tuples
coriolinus ef7c213
impl BoundedEncodedLen for fixed-size arrays
coriolinus 8da6a01
compact types can require an extra byte to encode
coriolinus 4f4fad4
use saturating math in case of silly-sized types
coriolinus 348a726
implement BoundedEncodedLen for Option, Result, PhantomData
coriolinus 5f886b4
always saturating math
coriolinus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright 2021 Parity Technologies | ||
| // | ||
| // 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 core::mem; | ||
| use crate::Encode; | ||
|
|
||
| /// Items implementing `BoundedEncodedLen` 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 BoundedEncodedLen: 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 BoundedEncodedLen for $t { | ||
| fn max_encoded_len() -> usize { | ||
| mem::size_of::<$t>() | ||
| } | ||
| } | ||
| )+ | ||
| }; | ||
| } | ||
|
|
||
| impl_primitives!((), u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); | ||
|
|
||
| macro_rules! impl_tuples { | ||
| // end of recursive descent | ||
| () => {}; | ||
| // recursive producer | ||
| ( $head:ident $(, $rest:ident )* ) => { | ||
| impl<$head $(, $rest)*> BoundedEncodedLen for ( $head, $($rest),* ) | ||
| where | ||
| $head: BoundedEncodedLen, | ||
| $( | ||
| $rest: BoundedEncodedLen, | ||
| )* | ||
| { | ||
| fn max_encoded_len() -> usize { | ||
| $head::max_encoded_len() | ||
| $( | ||
| + $rest::max_encoded_len() | ||
| )* | ||
| } | ||
| } | ||
|
|
||
| impl_tuples!($($rest),*); | ||
| }; | ||
| } | ||
|
|
||
| impl_tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R); | ||
|
|
||
| impl<T: BoundedEncodedLen, const N: usize> BoundedEncodedLen for [T; N] { | ||
| fn max_encoded_len() -> usize { | ||
| T::max_encoded_len() * N | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't see why this should be implemented in scale. This is Substrate specific and not SCALE specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The short answer is that
Encode::size_hintis toothless: it's a 'best-effort' thing with a useless default implementation. Still, we do have the need sometimes for a more realistic size hint, and that need is generalizable to all of SCALE, not just Substrate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also "toothless" as what is the maximum size of a vec?
As already said in element, I don't see this in SCALE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not implemented for
Vec😁. I'll move this work to the substrate repo instead though.