You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This RFC proposes the creation of a new "growable" vector named `StackVec` that manages stack memory and can be seen as an alternative for the built-in structure that handles heap-allocated memory, aka `alloc::vec::Vec<T>`.
9
+
This RFC, which depends and takes advantage of the upcoming stabilization of constant generics (min_const_generics), tries to propose the creation of a new "growable" vector named `ArrayVec` that manages stack memory and can be seen as an alternative for the built-in structure that handles heap-allocated memory, aka `alloc::vec::Vec<T>`.
10
10
11
11
# Motivation
12
12
[motivation]: #motivation
13
13
14
-
`core::collections::StackVec<T>` has several use-cases and should be conveniently added into the standard library due to its importance.
14
+
`core::collections::ArrayVec<T>` has several use-cases and should be conveniently added into the standard library due to its importance.
15
15
16
16
### Unification
17
17
@@ -23,15 +23,12 @@ Stack-based allocation is generally faster than heap-based allocation and can be
23
23
24
24
### Building block
25
25
26
-
Just like `Vec`, `StackVec` is a primitive vector where high-level structures can use it as a building block. For example, a stack-based matrix or binary heap.
26
+
Just like `Vec`, `ArrayVec` is also a primitive vector where high-level structures can use it as a building block. For example, a stack-based matrix or binary heap.
27
27
28
28
### Useful in the real world
29
29
30
30
`arrayvec` is one of the most downloaded project of `crates.io` and is used by thousand of projects, including Rustc itself.
31
31
32
-
### Compiler internals
33
-
34
-
Unstable features can be used internally to make operations more efficient, e.g., `specialization`.
@@ -64,23 +61,23 @@ Instead of relying on a heap-allocator, stack-based memory area is added and rem
64
61
---------
65
62
```
66
63
67
-
`StackVec` takes advantage of this predictable behavior to reserve an exactly amount of uninitialized bytes up-front and these bytes form a buffer where elements can be included dynamically.
64
+
`ArrayVec` takes advantage of this predictable behavior to reserve an exactly amount of uninitialized bytes up-front and these bytes form a buffer where elements can be included dynamically.
68
65
69
66
```rust
70
67
fnmain() {
71
-
// `stack_vec` has a pre-allocated memory of 2048 bits that can store up to 64 decimals.
The most natural module for `StackVec` is `core::collections` with an API that basically mimics most of the current `Vec<T>` surface.
89
+
`ArrayVec` is a contiguous memory block where elements can be collected, threfore, a collection by definition and even though `core::collections` doesn't exist, it is the most natural modulo placement.
90
+
91
+
The API basically mimics most of the current `Vec` surface with some tweaks and additional methods to manage capacity. Notably, these additional methods are fallible versions of some well-known functions like `insert` that will return `Result` instead of panicking at run-time.
Self::CapacityOverflow=>"It is not possible to add more elements",
212
+
Self::NoElement=>"There are no elements in the vector",
213
+
};
214
+
write!(f, "{}", s)
215
+
}
104
216
}
105
217
```
106
218
219
+
Meaningless, unstable and deprecated methods like `reserve` or `drain_filter` weren't considered and in general, everything that includes or removes elements have a fallible version: `extend_from_cloneable_slice`, `extend_from_copyable_slice`, `pop`, `remove` and `swap_remove`.
220
+
221
+
A concrete implementation is available at https://github.com/c410-f3r/stack-based-vec.
222
+
107
223
# Drawbacks
108
224
[drawbacks]: #drawbacks
109
225
@@ -132,7 +248,7 @@ As seen, there isn't an implementation that stands out among the others because
132
248
133
249
### Nomenclature
134
250
135
-
`StackVec` will probably avoid conflicts with existing crates but another name might be a better option.
251
+
`ArrayVec` will conflict with `arrayvec::ArrayVec` and `tinyvec::ArrayVec`.
136
252
137
253
### Prelude
138
254
@@ -142,10 +258,10 @@ Should it be included in the prelude?
0 commit comments