I think the struct { T, Vec<T> } representation of OP is advantageous in that it has no hidden invariants, but indexing and len are not as efficient. The struct { Vec<T> } quotient representation does have a hidden invariant that needs to be carefully maintained, but it has no extra overhead on indexing or len. So pick your poison I guess?
No heap allocation/pointer indirection in the Single case. More clearly expresses intent? I don't know what the common case is though: single or multiple elements.
That was in comparison to the struct OneOrMore<T>(Vec<T>) representation - should've made that clearer.
Also, NOrMore<T>(T, Vec<T>) always has inline storage for T + Vec, whereas the storage for the enum is max(T, Vec<T>). But I suspect all of this isn't really what @vandenoever is after. Is the sole concern to optimize for compile time safety?
Are you referring to literally just the creation process being efficient or subsequent access to the data there being efficient as well? Is this thing modified after construction? Is the common case >1 value in there? I guess what exactly are we optimizing for here?
The length of the Vec will typically be 2-10. Correctness (compile time check) is important as is a correct API that mimics Vec. Speed is nice but not the focus at the moment.
The current use case is to load Relax NG models and create code from them. Relax NG, like XML Schema, can specify arrays with a minimal length. To avoid the creation of objects that do not match the schema, the data types should also have this minimal length.
Prefer an implementation that allows you to take a slice of the whole contents -- the enum and the newtyped Vec both do that. With access to slices you get a whole lot of functionality for free, for example the slice iterators.
Hi, I implemented a small crate 'seq' proivding a Sequence type Seq which may be used as cheap stack-only replacement for Vector. The OneOrMore type you sketched is looking similar to my Seq-type (?) https://crates.io/crates/seq
and https://github.com/frehberg/seq-rs