-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement an inline-only growable (bounded) 'vector' #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this could be done in a parameterized way even without integer generics if we use the array type as a parameter. I have a proof-of-concept implementation which seems to work although I'm not completely sure how much I can assume about the low-level representation of |
wow @tdudziak you had the same ideas as I, even down to the name! I've published crates.io/arrayvec by now, and I found some interesting quirks along the way. It turns out Option is not a good idea with uninitialized memory, see testcase here and solution (I think) with this enum here. Yours is very nice because it doesn't use any array trait at all. That's probably superior. |
It's worth looking at Gecko's nsAutoTArray: https://hg.mozilla.org/mozilla-central/file/d3228c82badd/xpcom/glue/nsTArray.h#l2296 |
Any plans for getting this into the standard library? |
@ticki I hope not until we have proper type-level numerals |
Right, that's why I said plans. Having a non-generic one would be a mess. |
|
@WiSaGaN This is not what we are discussing. We are discussing a vector with an arbitrary size, but only heap allocated when a certain length is met. |
@ticki I am not sure that's the case. OP's post explicitly mentioned "bounded". |
arrayvec is mentioned in this thread already. It's a bounded capacity vector. Capacity generics are limited by the current state of rust (no integer generic parameters). |
Closing this in favor of |
Friday Sep 05, 2014 at 01:04 GMT
For earlier discussion, see rust-lang/rust#16998
This issue was labelled with: A-collections, A-libs in the Rust repository
Something like
[T, .. n]
but with alength
field (and the correct destructor), so it's valid to store anywhere from 0 upton
T
s in it. This can haveT
s dynamically pushed and popped, but attempting to push more thann
would be an error. I.e.(The error could either be
fail!
or aResult<(), ...>
return value.)This is useful for data structures like 2-4-trees, B-trees etc., where they have internal nodes that store a sequence of keys with variable, but bounded, length. At the moment the only safe way to encode this is either a heap allocation (
Vec
) or with[Option<T>, .. n]
where the "length" is modeled by storing[Some(x_1), Some(x_2), ..., Some(x_length), None, ..., None]
(this leads to a lot ofunwrap
and unnecessary branches).#197 is related, to make the destructors work right. For full generality we would need integer type params, but we can certainly have an internal abstraction of this form in
collections
(for use in btree etc.) with the lengths required hard-coded, and generalise/publish it later.The text was updated successfully, but these errors were encountered: