fizyk20 / generic-array

Generic array types in Rust
MIT License
404 stars 77 forks source link

support try_form? #119

Closed bigdogs closed 1 year ago

bigdogs commented 2 years ago

GenericArray::from_slice(&[u8) will panic if slice length is not match. It would be great to provide a Result version, e.g. (GenericArray::try_from_slice(&[u8) -> Result<Self, LengthNotMatch>)

bigdogs commented 2 years ago

and is there a method to get the array length from GenericArray? If try_from is not possible to support, I can check slice size before 'GenericArray::from_slice', But I didn't find any method for this.

orthecreedence commented 2 years ago

I do this outside the crate:

let arr: [u8; 32] = some_vec.try_from()?;
let genarr = GenericArray::from(arr);
agostbiro commented 2 years ago

There is also from_exact_iter that will return None if the sizes don't match.