-
Notifications
You must be signed in to change notification settings - Fork 216
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
Deserialize boundedbtreeset #781
Deserialize boundedbtreeset #781
Conversation
|
||
while let Some(value) = seq.next_element()? { | ||
values.insert(value); | ||
if values.len() > max { |
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 weird. You insert before checking for the max, i.e. you add 1 more value over the limit before you check the limit?
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 also found it weird. It was like that in BoundedVec
's Deserialize implementation: https://github.com/paritytech/parity-common/blob/master/bounded-collections/src/bounded_vec.rs#L90-L92
I thought that was intentional and didn't put much thought tbh.
I can fix them both if you like?
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.
Sure, but there's one more additional issue: checking values.len() > max
doesn't sound right to me either, because it too is also waiting for 1 element over the max before the condition triggers. What should really happen is values.len() >= max
.
It would be so great if you can write tests for these as well.
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.
Since we are already discussing the implementation of Deserialize for BoundedVec as well, I had another thing which confused me:
So, if we are performing a check in here for the size, do we need another check in here:
https://github.com/paritytech/parity-common/blob/master/bounded-collections/src/bounded_vec.rs#L90-L92
?
I think the second check is not necessary and the error produced by the second check is unreachable. So, I don't know if it's possible to write a test for triggering that error. It will trigger the previous error instead.
I might be missing something, so feel free to correct me
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 first check is using size_hint()
and that could also be None
in which case the size is 0
as you see in the code.
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've added a new commit:
- insertion is now performed after the bound check
- new tests for bound check:
- i've copied the
deserializer_test
and made the size equal to the bound - for the tests,
size_hint()
does indeed returnNone
, so I'm positive I'm testing the inner check - if you prefer not to have an additional test, the same test can be done by changing the bound to the size of the deserialized string in the
deserializer_test
- i've copied the
I followed
BoundedVec
's Deserialize implementation.Tests are passing 🥳