Skip to content
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

Implement Iterator trait for Fragment and BoundedVec #327

Merged
merged 3 commits into from
Jun 7, 2024
Merged

Conversation

Pasifaee
Copy link
Contributor

@Pasifaee Pasifaee commented Jun 6, 2024

This PR creates Iterator trait with one function: for_each and implements it for Fragment and BoundedVec.
It also migrates the code to use for_each where possible. I only found 2 places (1 for Fragment and 1 for BoundedVec) where it's possible to use it. In the rest of the loops iterating over fragments or boundedvecs i (loop iterator) is used in some way that makes it impossible to use for_each.

@Pasifaee Pasifaee requested review from LogvinovLeon and aajj999 June 6, 2024 11:27
Copy link
Contributor

@aajj999 aajj999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about bytes_to_nibbles function? I think there are a few similar places. I understand that we need i in these situations, but maybe we can add additional counter? Let's check if it would look better or worse

@aajj999 aajj999 self-requested a review June 6, 2024 13:48
Copy link
Contributor

@aajj999 aajj999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve after discussing offline my previous comment :)

bounded_vec.for_each(|x| result.push(f(x)));

*result
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea:

impl<T, N> Iterator<T> for BoundedVec<T, N> {
    fn map<Env, R>(self, f: fn[Env](T) -> R) -> Iterator<R> { 
       ...   
    }
}

@@ -1,3 +1,5 @@
use crate::misc::iterator::Iterator;

pub fn bounded_vec_from_array<T, N, M>(array: [T; N]) -> BoundedVec<T, M> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do it as From trait?

}

impl<T, N> Iterator<T> for BoundedVec<T, N> {
fn for_each<Env>(self, f: fn[Env](T) -> ()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> each

@Pasifaee Pasifaee added this pull request to the merge queue Jun 7, 2024
Merged via the queue into main with commit d5b0cc4 Jun 7, 2024
6 checks passed
@Pasifaee Pasifaee deleted the iza/iterator branch June 7, 2024 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants