-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Conversation
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 think adding derefMut can be dangerous and add footguns.
@@ -188,11 +201,26 @@ impl<T: BoundedVecValue, S: Get<u32>> sp_std::ops::Deref for BoundedVec<T, S> { | |||
} | |||
} | |||
|
|||
impl<T: BoundedVecValue, S: Get<u32>> sp_std::ops::DerefMut for BoundedVec<T, S> { |
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 allows you to call methods like extend
and push
without checking, not a good idea.
overall, bounded vec should only coerce to vec when methods are safe regarding the bound (cannot expand and re allocate). This is why I only did deref initially.
Same comments apply to indexMut and AsRefMut: if you can expand the size with them, we can't add them and instead it should be a checked method on the type itself, or just manually convert to vec and try and convert it back.
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'm going with DerefMut, this shouldn't be implelemented as said by @kianenigma
However, IndexMut and AsMut are safe as they don't support extending the vec. Both just give you multiple access to the vec elements.
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.
once we remove deref mut, then it is good to me
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
bot merge |
Waiting for commit status. |
* improve bounded vec api * Update frame/support/src/storage/bounded_vec.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Update frame/support/src/storage/bounded_vec.rs * Update frame/support/src/storage/bounded_vec.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This adds some useful APIs for making the conversion from
Vec
toBoundedVec
more seamless.