rustonaut / vec1

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

Feature request: generic over the underlying vector like container #31

Open Pr0methean opened 8 months ago

Pr0methean commented 8 months ago

Making this struct's inner type a generic parameter would mean it could also be used with String, boxed slices, and borrowed slices.

If Index doesn't provide enough features, an alternative would be AsRef<[T]>.

rustonaut commented 8 months ago

I think you are misunderstanding what this crate does.

It doesn't just provide a new-type you check the len>1 invariant on construction (you can always do that with just a few lines of code).

It provides a full Vec/SmallVec functionality with the len>1 variant, i.e. as far as viable (and I didn't overlook it):

And same for SmallVec.

As you might have noticed most of this functionality doesn't apply to Box<[T]> or &[T] at all.

And at least when I wrote that crate using generics wasn't very feasible (probably is different by now). But even if it's feasible it's not convenient, i.e. requires a bunch of custom Traits and getting all the ~55 trait implementations right (as in exactly the same behavior as now) isn't trivial.

Furthermore I don't want to release a v2 of this crate as it would be annoying for anyone using Vec1 in a public API (which might be possible by making Vec1 a type alias, but can be tricky).

Oh and String has completely different APIs and most times "non zero" constraints on Strings are not "non zero unicode code points" but non zero printable characters and similar.