rustonaut / vec1

Rust `Vec<T>` wrapper that gurantees to contain at least 1 element
Apache License 2.0
90 stars 15 forks source link

Consider use of NonZeroUsize for methods like truncate #27

Closed gliderkite closed 8 months ago

gliderkite commented 1 year ago

Vec1::truncate returns an error if the specified len: usize is 0. This could be avoided by having NonZeroUsize instead as input (breaking change).

dzmitry-lahoda commented 1 year ago

NonZero so badly integrated into Rust. Are there are compile time/const traits which help with that? Tbh only when NZ will have CONST into it would be awesome.

rustonaut commented 1 year ago

Thanks for the interest.

I do not plan to make truncate accept non zero for two reasons:

  1. in many cases it just moves the non-zero check out of the function, but also no longer yields a Size0Error

  2. doing so works for truncate but not other functions (e.g. pop) so making truncate fallible is more in line with the rest of the API

Through is will add a variant of the function (and potential other functions, I will have to check) which does accept NonZeroUsize.

rustonaut commented 8 months ago

I added truncate_nonzero and some similar methods in the last release.