2 releases

0.1.1 Dec 5, 2022
0.1.0 Dec 5, 2022

#30 in #reuse

MIT license

4KB

PolyVec

Created to reuse reference borrow buffers over iteration


lib.rs:

This crate helps reusing allocated Vec memory.

Example 1

use polyvec::*;

let mut v: PolyVec = Vec::<(usize, usize)>::with_capacity(128).into();

for _ in 0..10 {
    let g: Vec<usize> = v.try_into().unwrap();

    assert!(g.capacity() >= 256);
    v = g.into();
}

Example 2

use polyvec::*;

let src_vec: Vec<_> = (0..256).collect();
let mut poly = PolyVec::new::<&usize>();

for i in 1..=3 {
    println!("ITERATION {i} --- ");

    let mut refs: Vec<&usize> = poly.try_into().unwrap();
    let mut prev_capacity = refs.capacity();
    let mut num_realloc = 0;

    for i in 0..src_vec.len() {
        refs.push(&src_vec[i]);
        if prev_capacity != refs.capacity() {
            println!("  Cap {prev_capacity} -> {}", refs.capacity());
            prev_capacity = refs.capacity();
            num_realloc += 1;
        }
    }

    println!(" => reallocation: {num_realloc}");
    poly = refs.into();
}

Dependencies

~270–730KB
~17K SLoC