#vec #reuse #empty #convert #size #alignment #vec-b

revec

Convert a Vec<A> to a Vec<B> provided vec is empty

3 releases (breaking)

new 0.3.0 Nov 12, 2024
0.2.0 Mar 26, 2022
0.1.0 Mar 26, 2022

#516 in Algorithms

Download history 30/week @ 2024-07-25 19/week @ 2024-08-01 41/week @ 2024-08-08 5/week @ 2024-08-15 7/week @ 2024-08-22 15/week @ 2024-08-29 9/week @ 2024-09-05 42/week @ 2024-09-12 20/week @ 2024-09-19 19/week @ 2024-09-26 46/week @ 2024-10-03 66/week @ 2024-10-10 53/week @ 2024-10-17 23/week @ 2024-10-24 29/week @ 2024-10-31 97/week @ 2024-11-07

208 downloads per month
Used in 3 crates (via broccoli)

MIT license

4KB

Allows a user to safely convert between Vec<A> and Vec<B> provided the vec is empty is A and B have the same memory size and alignment.

fn test() {
    let v:Vec<*mut usize> = Vec::new();

    assert_eq!(v.capacity(),0);
    
    let k = &mut [5usize, 4, 3, 2, 1];

    let mut v2:Vec<&usize>=convert_empty_vec(v);
    v2.extend(k.iter());

    assert_eq!(*v2[0], 5);

    v2.clear();
    assert!(v2.capacity()>=5);


    let mut v3:Vec<&mut usize>=convert_empty_vec(v2);
    assert!(v3.capacity()>=5);


    let k = &mut [5usize, 4, 3, 2, 1];

    v3.extend(k.iter_mut());

    assert_eq!(*v3[0], 5);

    
}

No runtime deps