Debian Codegraph @

README.md

raw symbols SCIP
1 # Inplace-Vec-Builder
2
3 A small library to build a [Vec](https://doc.rust-lang.org/std/vec/struct.Vec.html) or [SmallVec](https://docs.rs/smallvec) out of itself without allocating.
4
5 This is useful when writing in place operations that do not allocate.
6
7 Imagine you have a vec that contains some numbers. You now want to apply some transformation on
8 these elements, like mapping, filtering, adding some elements, and then store the result in the same place.
9
10 The simplest way to do this would be something like this:
11
12 ```rust
13 let mut res = self
14 .elements
15 .iter()
16 .filter(|x| **x > 5)
17 .map(|x| *x * 2)
18 .chain(std::iter::once(123))
19 .collect();
20 std::mem::swap(&mut self.elements, &mut res);
21 ```
22
23 But this does allocate a new vector. Usually not a big deal, but if this is some very frequently used code, you want to avoid it.
24
25 Note that in many cases where you do filtering combined with a transformation, [retain](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain) can be used. If that is the case using retain is
26 of course preferable.
27
28 This crate provides a helper that allows doing something like the above without allocations. It is
29 fairly low level, since it is intended to be used from other libraries.
30
31 ```rust
32 let mut t = InPlaceVecBuilder::from(&mut self.elements);
33 while let Some(elem) = t.pop_front() {
34 if elem > 5 {
35 t.push(elem * 2);
36 }
37 }
38 t.push(123);
39 ```
40
41 # Features
42
43 - stdvec (default): std Vec support
44 - smallvec: SmallVec support