Crates.io | rent_vec |
lib.rs | rent_vec |
version | 0.1.0 |
source | src |
created_at | 2022-09-15 13:11:02.126629 |
updated_at | 2022-09-15 13:11:02.126629 |
description | A Rust vector with a unique owner for every item |
homepage | |
repository | |
max_upload_size | |
id | 666675 |
size | 12,445 |
The idea behind this crate is to have a vector where each item has its owner, as if it was rented.
Every time you push something into a RentVec it will return you with a lease, and that is one way to access the underlying data.
use rent_vec::RentVec;
let mut vec = RentVec::new();
let mut lease = vec.push(1u32);
let mut item = lease.guard();
*item = 2;
The other way to access data is through the write guard and iterators, that guarantee that no lease is modifying its entry.
let mut guard = vec.guard();
guard.iter();
guard.iter_mut();
If an entry is removed, it will move an item from the back into its location, and mark the other as moved.
let mut lease = vec.push(10u32);
lease.remove();
Once a moved entry is accessed, the lease will become aware of the new location, and the moved entry can be freed.
If you need a StableVec that is as contiguous as possible. The leases are also guaranteed to be valid.
This was written in a morning to figure out what the concept could look like.
The access performance is worse, since it has to resolve moved entries. After the first resolution, it is O(1).
Push performance is also slower, since it has to search for freed entries amongst moved entries. If there aren't moved entries, it is O(1).