Skip to content
Extend Vec to allow reference to content while pushing new elements
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
benches
src
.gitignore
Cargo.lock
Cargo.toml
README.md

README.md

fixed_capacity_vec

Extend Vec to allow reference to content while pushing new elements.

Inspired by this Pre-RFC

Example

use fixed_capacity_vec::VecExt;

let mut vec = vec![1, 2, 3, 4];
{
    let (content, mut extend_end) = vec.with_fixed_capacity(5);
    extend_end.push(4);
    assert_eq!(extend_end.as_ref(), &[4]);

    // We can still access content here.
    assert_eq!(content, &[1, 2, 3, 4]);

    // We can even copy one buffer into the other
    extend_end.extend_from_slice(content);
    assert_eq!(extend_end.as_ref(), &[4, 1, 2, 3, 4]);

    // The following line would panic because we reached max. capacity:
    // extend_end.push(10);
}
// All operations happened on vec
assert_eq!(vec, &[1, 2, 3, 4, 4, 1, 2, 3, 4]);
You can’t perform that action at this time.