| Crates.io | fixed-index-vec |
| lib.rs | fixed-index-vec |
| version | 0.1.0 |
| created_at | 2023-12-11 11:52:29.553447+00 |
| updated_at | 2023-12-11 11:52:29.553447+00 |
| description | A vector-like data structure whose indices do not change when elements are removed. |
| homepage | |
| repository | https://github.com/calteran/fixed-index-vec |
| max_upload_size | |
| id | 1065062 |
| size | 18,772 |
This crate provides FixedIndexVec, a Rust collection that functions like an array with immutable indices.
Each value is associated with a unique index upon insertion.
The value can be accessed, inserted, and removed with the index serving as the identifier.
An item cannot be modified, nor can it be replaced with another item, even after removal.
With default features, FixedIndexVec has no dependencies outside of the Rust standard library.
I was looking for a way to do simple version control within a data structure.
The immutability of the indices in FixedIndexVec allows for a simple implementation of this functionality.
Items' insertion order is preserved through comparison of their indices, and the removal of an item is reflected
by indices that return None when accessed.
To access the "current" version of an item, simply call FixedIndexVec::last().
There are other use cases for FixedIndexVec as well, but this is the one that motivated me to create it.
Add the following to your Cargo.toml:
[dependencies]
fixed_index_vec = "0.1.0"
Then include it in your application:
use fixed_index_vec::FixedIndexVec;
FixedIndexVec with FixedIndexVec::new()FixedIndexVec with FixedIndexVec::push(value)FixedIndexVec::remove(index). If no element exists at that index, returns None.FixedIndexVec::get(index). If no element exists at that index, returns None.FixedIndexVec::iter(). Skips indices that do not have a corresponding value.FixedIndexVec with FixedIndexVec::len()FixedIndexVec contains no elements with FixedIndexVec::is_empty()FixedIndexVec with FixedIndexVec::clear()FixedIndexVec and resets the next index to 0 with FixedIndexVec::reset()serde: Enables serialization and deserialization of FixedIndexVec with Serde. Requires FixedIndexVec to contain values that implement Serialize and DeserializeOwned.use fixed_index_vec::FixedIndexVec;
let mut vec = FixedIndexVec::new();
vec.push("value1".to_string()); vec.push("value2".to_string());
assert_eq!(vec.get(0), Some(&"value1".to_string())); assert_eq!(vec.get(1), Some(&"value2".to_string()));
vec.remove(1);
assert_eq!(vec.get(1), None);
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License.