Crates.io | orx-pinned-vec |
lib.rs | orx-pinned-vec |
version | |
source | src |
created_at | 2023-08-26 20:52:22.049093 |
updated_at | 2024-12-12 15:19:48.244138 |
description | `PinnedVec` trait defines the interface for vectors which guarantee that elements added to the vector are pinned to their memory locations unless explicitly changed. |
homepage | |
repository | https://github.com/orxfun/orx-pinned-vec/ |
max_upload_size | |
id | 955713 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
PinnedVec
trait defines the interface for vectors which guarantee that elements added to the vector are pinned to their memory locations unless explicitly changed.
A PinnedVec
guarantees that positions of its elements do not change implicitly.
Assume that we have a pinned vector which is at a state containing n elements. Pinned vector guarantees can then be summarized as follows.
Note that this eliminates a certain set of errors that are easy to make in some languages and forbidden by the borrow checker in rust. Consider, for example, the classical example that does not compile in rust. The reason this code has a bug is due to the fact that the elements of the standard vector are not pinned to their memory locations and it is possible that the push
leads to changing them all together. Using a pinned vector, on the other hand, this would be a memory safe operation.
let mut vec = vec![0, 1, 2, 3];
let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);
vec.push(4);
// does not compile due to the following reason: cannot borrow `vec` as mutable because it is also borrowed as immutable
// assert_eq!(ref_to_first, &0);
An example data structure relying on pinned vector guarantees to enable immutable push operation is the ImpVec which is also discussed in this article.
Furthermore, self-referential-collections following thin references rather than wide pointers or index numbers rely on the consistency of memory positions of its elements. Pinned vectors again come in very useful for them. You may find an efficient and capable LinkedList implementation built on top of the pinned element guarantees.
Finally, it is easy to observe that pinned element guarantees make it extremely more convenient and safer to achieve data structures which enable concurrent growth. There are various concurrent data structures relying on pinned vectors, such as ConcurrentVec, ConcurrentBag and ConcurrentOrderedBag. These concurrent collections play an essential role in efficiently collecting results of a parallel computation by the parallel iterator Par.
PinnedVec
trait on its own cannot provide the pinned element guarantee; hence, it could be considered as a marker trait.
However, this crate additionally provides the test function to assert these guarantees:
pub fn test_pinned_vec<P: PinnedVec<usize>>(pinned_vec: P, test_vec_len: usize) {
// ...
}
This function performs an extensive test on the specific implementation P
and fails if any of the above guarantees is not provided.
Note that std::vec::Vec
does not provide the pinned elements during growth guarantee. You may find a wrapper struct JustVec
which is nothing but the standard vec here: src/pinned_vec_tests/test_all.rs. As expected, test_pinned_vec
method fails for this struct.
SplitVec
and FixedVec
are two efficient PinnedVec implementations.
Contributions are welcome! If you notice an error, have a question or think something could be improved, please open an issue or create a PR.
This library is licensed under MIT license. See LICENSE for details.