#vec #pin #elements #move #structure #pinned #vec-like

pinned_vec

Vec-like data structure whose elements never move

2 releases

0.1.1 Nov 25, 2021
0.1.0 Nov 25, 2021

#1224 in Data structures

Download history 50/week @ 2023-12-13 47/week @ 2023-12-20 18/week @ 2023-12-27 71/week @ 2024-01-03 14/week @ 2024-01-10 5/week @ 2024-01-17 11/week @ 2024-01-24 26/week @ 2024-01-31 66/week @ 2024-02-07 19/week @ 2024-02-14 108/week @ 2024-02-21 291/week @ 2024-02-28 80/week @ 2024-03-06 25/week @ 2024-03-13 109/week @ 2024-03-20 78/week @ 2024-03-27

296 downloads per month
Used in 3 crates (2 directly)

MPL-2.0 license

9KB
180 lines

PinnedVec

crates.io crates.io

Vec-like structure whose elements never move.

Normal Vec holds all its content in one contigious region, and moves when it needs to expand. PinnedVec holds several smaller sub-vector, each of which never moves. The first sub-vector is of capacity 1, the second 2, the third 4, the nth 2^(n-1).

Example Usage

use pinned_vec::PinnedVec;
use std::pin::Pin;
let mut v = PinnedVec::new();
v.push(5);
{
	let r: Pin<&i32> = v.get(0).unwrap();
	assert_eq!(*r, 5);
}
{
	let r: Pin<&mut i32> = v.get_mut(0).unwrap();
	assert_eq!(*r, 5);
}
assert_eq!(v.len(), 1);
v.pop();
v.push(7);
v.push(8);
v.replace(0, 6);
assert_eq!(*v.get(0).unwrap(), 6);
assert_eq!(*v.get(1).unwrap(), 8);

lib.rs:

PinnedVec

Vec-like structure whose elements never move.

Normal Vec holds all its content in one contigious region, and moves when it needs to expand. PinnedVec holds several smaller sub-vector, each of which never moves. The first sub-vector is of capacity 1, the second 2, the third 4, the nth 2^(n-1).

Example Usage

use pinned_vec::PinnedVec;
use std::pin::Pin;
let mut v = PinnedVec::new();
v.push(5);
{
	let r: Pin<&i32> = v.get(0).unwrap();
	assert_eq!(*r, 5);
}
{
	let r: Pin<&mut i32> = v.get_mut(0).unwrap();
	assert_eq!(*r, 5);
}
assert_eq!(v.len(), 1);
v.pop();
v.push(7);
v.push(8);
v.replace(0, 6);
assert_eq!(*v.get(0).unwrap(), 6);
assert_eq!(*v.get(1).unwrap(), 8);

No runtime deps