-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add push
that fails if upper bound is violated
#9
Comments
This is an excellent question! Actually, I wonder if we should make all existing add/remove methods (e.g. push, pop, append, etc.) failable. I mean we have lower and upper bounds and we should error out in case we're going out of them. That'd make BoundVec API not so similar to Vec anymore, but I'm not sure it's worth sticking to it that much. What do you think? |
There's some code in the WASM bindings for sigma-rust where I felt it would be useful, namely, Tokens, which currently wraps a Vec, but I plan on changing it to BoundedVec. impl Tokens {
/// Adds an elements to the collection
pub fn add(&mut self, elem: &Token) {
self.0.push(elem.clone());
}
} Without a try_add, this becomes a bit trickier since you need to take the BoundedVec, consume it, and create a new one. An additional suggestion is to also make Tokens::add fallable, thus this could become: pub fn add(&mut self, elem: &Token) -> Result<(), OutOfBounds) {
if let Some(tokens) = self.0 {
tokens.try_add(elem.clone())?; // BoundedVec::try_add
}
else {
self.0 = Some(BoundedVec::from([elem.clone()]));
}
Ok(())
} |
I totally understand the need for this. What I'm thinking is to name it not |
That seems pretty reasonable yea. By default rustc warns about unused Results anyways, so people would adapt quickly |
try_push
that fails if upper bound is violatedpush
that fails if upper bound is violated
Good point! I can see that existing familiarity with Vec::push might even help. |
from #8 by @SethDusek:
The text was updated successfully, but these errors were encountered: