any_vec 0.6.0

Type erased vector. Most operations can be done without type knowledge. Mostly zero overhead.
Documentation

Type erased vector [AnyVec]. Allow to store elements of the same type. Have same performance and operations as [std::vec::Vec].

You can downcast type erased [AnyVec] to concrete [AnyVecTyped<Element>] with downcast-family. Or use [AnyVec]s type erased operations, which works with AnyValue.

use any_vec::AnyVec;
use any_vec::any_value::AnyValue;
let mut vec: AnyVec = AnyVec::new::<String>();
{
// Typed operations.
let mut vec = vec.downcast_mut::<String>().unwrap();
vec.push(String::from("0"));
vec.push(String::from("1"));
vec.push(String::from("2"));
}

let mut other_vec: AnyVec = AnyVec::new::<String>();
// Fully type erased element move from one vec to another
// without intermediate mem-copies.
let element = vec.swap_remove(0);
other_vec.push(element);

// Output 2 1
for s in vec.downcast_ref::<String>().unwrap().as_slice(){
println!("{}", s);
}

Send, Sync, Clone

You can make [AnyVec] [Send]able, [Sync]able, Cloneable:

use any_vec::AnyVec;
use any_vec::traits::*;
let v1: AnyVec<dyn Cloneable + Sync + Send> = AnyVec::new::<String>();
let v2 = v1.clone();

This constraints will be applied compiletime to element type:

# use any_vec::AnyVec;
# use std::rc::Rc;
let v1: AnyVec<dyn Sync + Send> = AnyVec::new::<Rc<usize>>();