8000 GitHub - tower120/any_vec at v0.4.0
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

tower120/any_vec

Repository files navigation

crates.io license Docs CI

Type erased vector. All elements have the same type.

Designed to be type-erased as far as possible - most of the operations does not know about concrete type.

Only destruct operations have additional overhead of indirect call.

Usage

    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.
    //
    // Equivalent to:
    //
    // let element = vec.swap_remove(0);
    // other.push(element);
    unsafe{
        let element: &mut[u8] = other_vec.push_uninit();    // allocate element 
        vec.swap_remove_into(0, element);                   // swap_remove
    }

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

Known alternatives

  • type_erased_vec. Allow to store Vec<T> in type erased way, but you need to perform operations, you need to "cast" to concrete type first.
  • untyped_vec. Some operations like len, capacity performed without type knowledge; but the rest - require concrete type.

About

Rust type erased vector.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0