You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed an API inconsistency. ArrayVec::push panics if you overflow the capacity, but ArrayVec::extend silently drops elements if you overflow the capacity.
I prefer the panicking behavior, since otherwise you run the risk of accidentally losing iterator elements. imo that's not an intuitive failure mode. I'd suggest changing extend to panic on overflow, and introduce a fallible try_extend (after all, you already have try_extend_from_slice), try_from_iterator, and probably try_collect and a trait to go with it.
This is a breaking change. The migration path would be pretty straightforward:
// previous version
arrayvec.extend(iterator);// next version
arrayvec.extend(iterator.take(arrayvec.remaining_capacity());// -- OR --let _ignore_result = arrayvec.try_extend(iterator);
The text was updated successfully, but these errors were encountered:
whatisaphone
changed the title
Suggested API change
Suggested API change – extend/collect should panic when capacity overflows
Jul 24, 2020
I'm inclined to agree, but would like to hear other's thoughts as well. It's not great that extend/fromiterator may panic, but there are only these two (bad) options.
The behaviour change is implemented in #175, but .try_extend() is not implemented yet (that's a new feature, not a behaviour change). A new issue is needed for that.
I'm a bit unsure about the try_extend name because I'm not so keen to have a "try" method that can return an error, but still have an effect in the error case. Since it's an iterator we wouldn't know its length until we iterate it. So maybe it could be called something else than "try_extend". Compare with try_extend_from_slice: it doesn't partly extend in the error case - it leaves the arrayvec is unchanged on error.
I noticed an API inconsistency.
ArrayVec::push
panics if you overflow the capacity, butArrayVec::extend
silently drops elements if you overflow the capacity.I prefer the panicking behavior, since otherwise you run the risk of accidentally losing iterator elements. imo that's not an intuitive failure mode. I'd suggest changing
extend
to panic on overflow, and introduce a fallibletry_extend
(after all, you already havetry_extend_from_slice
),try_from_iterator
, and probablytry_collect
and a trait to go with it.This is a breaking change. The migration path would be pretty straightforward:
The text was updated successfully, but these errors were encountered: