rustonaut / vec1

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

Returning Vec if Vec1::from() fails doesn’t make sense #6

Closed Profpatsch closed 5 years ago

Profpatsch commented 5 years ago

It can only ever be an empty Vec, so

pub fn from_vec( vec: Vec<T> ) -> Option<Vec1<T>> {
        if vec.len() > 0 {
            Some( Vec1( vec ) )
        } else {
            None
        }
    }

would be a more sensible interface

rustonaut commented 5 years ago

We would have to at last return an error type.

The thing is the Vec might be empty, but have GB of allocated and previously used memory in it.

Hm, through for the same reason we might really not want to return the Vec wrapped inside a error as this would prevent the memory from being freed.

So I guess Option it is (I mean the large capacity Vec case where the Vec should be re-used is most likely super rare, so we can ignore it here).

rustonaut commented 5 years ago

There is now a new version (1.2.0).

I first wasn't sure if I like Option and that's a braking change, and then a lot of (unrelated) work came my way so it took a while but now it's finally done.

The changes ended up like following: