univec 0.7.0

A vector that can hold elements of any single type
Documentation

A dynamic vector that can store elements of any single type.

Usage

UniVec is a dynamic vector that wraps any Vec<T> to store elements of any single type determined at runtime. Its primary use case is when one need to store elements of a single type, but this type might vary by context.

The type T becomes associated to an instance of UniVec upon the first insertion of an element of type T. Once the type is set, all subsequent insertions must be of the same type.

It comes with a push/pop API's to implement an efficient stack functionality.

Example

# use univec::{UniVec, univec};
# use std::collections::HashMap;
let mut things = HashMap::<String, UniVec>::new();

things.insert("integers".to_string(), univec![1, 2, 3]);
things.insert("names".to_string(), univec!["foo", "bar", "baz"]);

assert_eq!(things["integers"].get::<i32>(0), Some(&1));
assert_eq!(things["names"].get::<&str>(0), Some(&"foo"));

Performance

UniVec is a wrapper around dyn trait of a Vec<T>. Most operations are just a single layer of indirection into the underlying Vec. Because of this, UniVec should have performance close to Vec.

Limitations

  • Since it is based on std::any::Any references contained in stored objects must be of 'static lifetime.
  • The contained type T must implement Debug.
  • The Index and IndexMut traits are not implemented for UniVec because the result type is only known at runtime.

Fun Fact

This crates is a testbed for code and documentation generation with ChatGPT and Codepilot. Nevertheless all code is audited and tested with miri and cargo mutants.

Contribution

This crate is far from complete. Contributions are welcome! Feel free to open an issue or submit a pull request.

Credits

  • FSMaxB -- Implementation Idea about the helper trait.