Skip to content

Extend Vec to allow reference to content while pushing new elements

Notifications You must be signed in to change notification settings

WanzenBug/rust-fixed-capacity-vec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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]);

About

Extend Vec to allow reference to content while pushing new elements

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages