rustonaut / vec1

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

More constructors that guarantee safety #26

Closed giarc3 closed 2 years ago

giarc3 commented 2 years ago

Recently when using this crate I've been writing lots of code that looks like

let my_vec1: Vec1<_> = my_vec
    .into_iter()
    .chain(std::iter::once(item_to_add))
    .collect_vec()
    .try_into()
    .unwrap();

Essentially I want to append an item to a Vec and then turn it into a Vec1. Since I added an item, I know that this is safe, but the unwrap doesn't capture this guarantee.

Would a function for this (and also one that prepends to the Vec) be accepted into the crate?

rustonaut commented 2 years ago

I implemented it but I still have to fix the signing & SSH key on my private laptop. Through if nothing gets in the way a v1.9 should be released tomorrow with methods like:

rustonaut commented 2 years ago

Done :tada:

giarc3 commented 2 years ago

This is fantastic, thank you so much! :tada: