-
Notifications
You must be signed in to change notification settings - Fork 742
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
WithMaxSize #4188
Closed
Closed
WithMaxSize #4188
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,91 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 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 codec::{Encode, Decode, Input, MaxEncodedLen}; | ||
use frame_support_procedural::{EqNoBound, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound}; | ||
use scale_info::TypeInfo; | ||
use sp_core::Get; | ||
|
||
use crate::Parameter; | ||
|
||
/// TODO: docs | ||
pub struct InputWithMaxLength<'a, I: Input, S: Get<usize>> { | ||
input: &'a mut I, | ||
len: usize, | ||
xlc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_phantom: core::marker::PhantomData<S>, | ||
} | ||
|
||
impl<'a, I: Input, S: Get<usize>> InputWithMaxLength<'a, I, S> { | ||
pub fn new(input: &'a mut I) -> Self { | ||
Self { input, len: S::get(), _phantom: Default::default() } | ||
} | ||
} | ||
|
||
impl<'a, I: Input, S: Get<usize>> Input for InputWithMaxLength<'a, I, S> { | ||
fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> { | ||
let len = self.input.remaining_len()?; | ||
Ok(len.map(|l| l.min(self.len))) | ||
} | ||
|
||
fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> { | ||
let max_len = self.len.min(into.len()); | ||
xlc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.len -= max_len; | ||
self.input.read(&mut into[..max_len])?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// TODO: docs | ||
#[derive(Encode, EqNoBound, PartialEqNoBound, CloneNoBound, TypeInfo, RuntimeDebugNoBound)] | ||
xlc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[scale_info(skip_type_params(S))] | ||
pub struct WithMaxSize<T: Parameter, S: Get<usize>> { | ||
value: T, | ||
_phantom: core::marker::PhantomData<S>, | ||
} | ||
|
||
impl<T: Parameter, S: Get<usize>> WithMaxSize<T, S> { | ||
/// TODO: docs | ||
pub fn unchecked_new(value: T) -> Self { | ||
Self { value, _phantom: Default::default() } | ||
} | ||
|
||
/// TODO: docs | ||
pub fn new(value: T) -> Result<Self, ()> { | ||
if value.encoded_size() <= S::get() { | ||
Ok(Self { value, _phantom: Default::default() }) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
|
||
pub fn value(self) -> T { | ||
self.value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could make sense to impl the |
||
} | ||
} | ||
|
||
impl<T: Parameter, S: Get<usize>> MaxEncodedLen for WithMaxSize<T, S> { | ||
fn max_encoded_len() -> usize { | ||
S::get() | ||
} | ||
} | ||
|
||
impl<T: Parameter, S: Get<usize>> Decode for WithMaxSize<T, S> { | ||
fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> { | ||
let mut input = InputWithMaxLength::<I, S>::new(input); | ||
Ok(Self::unchecked_new(T::decode(&mut input)?)) | ||
} | ||
} |
Oops, something went wrong.
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.
Not sure what's the best way to handle this