# \[Pre-RFC\] Fixed-capacity view of Vec

**URL:** <https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413>\
**Category:** libs\
**Created:** [September 16, 2018, 6:53pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413 "2018-09-16T18:53:55Z")\
**Posts on this page:** 12\
**Page:** 3

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gnzlbg/32/3858_2.png) [@gnzlbg](https://internals.rust-lang.org/u/gnzlbg)\
**Post date:** [November 29, 2018, 1:54pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/41 "2018-11-29T13:54:33Z")

</div>

An interesting consequence of a fixed-capacity view of a vector, is that, because the vector cannot grow through the view, inserting element through it never invalidates references to elements in the vector. That is, one could iterate over the elements of such a view, while inserting elements through it, something that cannot be done with just `Vec`.

---

<div class="post-metadata">

**Author:** ![TimDiekmann](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/timdiekmann/32/4669_2.png) [@TimDiekmann](https://internals.rust-lang.org/u/TimDiekmann)\
**Post date:** [December 20, 2018, 4:10am UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/42 "2018-12-20T04:10:10Z")

</div>

This could also be expanded to a fixed-capacity view of a `String`.

Additionally, there could be two variants: An owned and a borrowed one: `FixedVec/String` and `FixedVec/StringView`. The owned variant is constructed with a fixed capacity while the borrowed one is a view into another non-fixed one.

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [January 19, 2019, 11:25pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/43 "2019-01-19T23:25:28Z")

</div>

I wrote a similar abstraction in the [`buffer` crate](https://docs.rs/buffer). It allows you to pass uninitialized buffers of bytes around. This allows things like

```rust
let mut vec = Vec::with_capacity(1024);
if reader.read_buffer(&mut vec)?.len() != 0 {
    if vec[0] == 0 {
        // ...
    }
}

```

in safe Rust. It actually does have one reverse dependency now. 😮 But I use it extensively in crates not published on [crates.io](http://crates.io), it makes for a very good abstraction for 1) networking, 2) compression/decompression, 3) building protocol packets, all with no or minimal allocations. (You can use an uninitialized `ArrayVec` as well as a `Vec` with spare capacity.)

This all works without (or at least with little) monomorphisation cost, because each function taking a `B: Buffer` can immediately convert it into a `BufferRef`, so the function won’t be instantiated for each possible buffer type. The conversion makes sure that the underlying buffer gets resized before the function returns.

What do you think about this?

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [January 20, 2019, 5:50am UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/44 "2019-01-20T05:50:50Z")

</div>

Can’t OP problem be solved by something like `VecCapView`? It will provide opaque view into capacity space and will allow to push data into it. Something like this:

```rust
let mut buf = Vec::with_capacity(12);
buf.extend_from_slice(b"hi");
// `cap_view` is somewhat similar to `split_at`, we also can add `cap_view_mut`,
// which will return `&mut [T]` instead of `&[T]`.
// Under the hood `VecCapView` contains mutable reference to the parent `Vec`
let (data: &[u8], mut cap_view: VecCapView) = buf.cap_view();
// values are written into memory, but not committed yet (i.e. `len` field
// is left unchanged, so if we'll forget the view, vector will not change),
// each method checks if there is enough capacity is left in the view
// returning an error otherwise
cap_view.push(b':')?;
cap_view.extend([b'f', b'o', b'o', b'='].iter().cloned())?;
cap_view.extend_from_slice(data)?;
// now it's time to "commit" data, this method consumes the view
// and simply bumps `len` field of the parent vector
cap_view.commit();

// assuming `data` is out of scope
assert_eq!(&buf[..], b"hi:foo=hi")

```

---

<div class="post-metadata">

**Author:** ![Shnatsel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/shnatsel/32/4613_2.png) [@Shnatsel](https://internals.rust-lang.org/u/Shnatsel)\
**Post date:** [January 20, 2019, 11:11am UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/45 "2019-01-20T11:11:36Z")

</div>

I like the simplicity of the `VecCapView` idea. Why not automatically commit the length on dropping the view though?

---

<div class="post-metadata">

**Author:** ![Shnatsel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/shnatsel/32/4613_2.png) [@Shnatsel](https://internals.rust-lang.org/u/Shnatsel)\
**Post date:** [January 20, 2019, 11:21am UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/46 "2019-01-20T11:21:33Z")

</div>

`Buffer` crate looks interesting. I don’t think you can actually read bytes from it without allocating another buffer and copying there, though? So you can’t implement RLE without copying?

Also, I have some safety concerns about `BufferRef::new()` - shouldn’t it be `unsafe fn` if setting the `initialized` parameter lets you view uninitialized memory? I’d split it in two constructors - make the current `new()` an `unsafe fn` and provide another safe one that always sets `initialized` to 0.

There’s also `bytes` crate that alread does all we need with [BytesMut.split\_to()](https://docs.rs/bytes/0.4.10/bytes/struct.BytesMut.html#method.split_to), but has extra overhead due to atomics and reference counting.

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [January 20, 2019, 3:46pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/47 "2019-01-20T15:46:51Z")

</div>

> [@Shnatsel](#):
>
> Why not automatically commit the length on dropping the view though?

Yes, auto-commits are viable alternative, but I thought it could be useful to leave vector unchanged until we are sure that there is enough capacity for our data. I guess it's up to discussion.

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [January 20, 2019, 9:31pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/48 "2019-01-20T21:31:02Z")

</div>

This is basically what the `buffer` crate does, except that it autocommits on drop and supports more than just `Vec`.

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [January 20, 2019, 9:37pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/49 "2019-01-20T21:37:14Z")

</div>

> [@Shnatsel](#):
>
> `Buffer` crate looks interesting. I don’t think you can actually read bytes from it without allocating another buffer and copying there, though? So you can’t implement RLE without copying?

I'm not sure we're talking about the same thing. `Buffer` allows you to pass an uninitialized buffer to a function, some space where it can write to. It's not a read-write-view of data, it always assumes its data is uninitalized, so you can't read from it.

> [@Shnatsel](#):
>
> Also, I have some safety concerns about `BufferRef::new()` - shouldn’t it be `unsafe fn` if setting the `initialized` parameter lets you view uninitialized memory? I’d split it in two constructors - make the current `new()` an `unsafe fn` and provide another safe one that always sets `initialized` to 0.

It's not an unsafe function — if you pass it a valid `&mut [u8]` all the bytes are already initialized.

If you don't, that's also fine, but you're already in unsafe code by providing the slice.

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [January 20, 2019, 9:50pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/50 "2019-01-20T21:50:00Z")

</div>

I suppose adding an `unsafe` constructor taking `*mut [u8]` might be good.

---

<div class="post-metadata">

**Author:** ![Shnatsel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/shnatsel/32/4613_2.png) [@Shnatsel](https://internals.rust-lang.org/u/Shnatsel)\
**Post date:** [January 27, 2019, 10:25pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/51 "2019-01-27T22:25:03Z")

</div>

Okay, this is very interesting! I’d very much like to see one of the motivating examples rewritten to use the `buffer` crate to gauge safety and performance of this solution. But I already have two Rust projects in active development stage, so I’m not sure when I’ll be able to get around to it, if at all.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 27, 2019, 10:34pm UTC](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413/52 "2019-04-27T22:34:30Z")

</div>

Closed on April 27, 2019, 10:34pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/pre-rfc-fixed-capacity-view-of-vec/8413.md?page=2)
