#pinned #memory #vec #array #vector

orx-pinned-vec

PinnedVec trait defines the interface for vectors which guarantee that elements added to the vector are pinned to their memory locations unless explicitly changed

27 releases (13 stable)

new 2.8.0 Apr 12, 2024
2.6.0 Mar 24, 2024
1.0.1 Jan 6, 2024
0.5.3 Dec 26, 2023
0.4.3 Sep 8, 2023

#1336 in Data structures

Download history 69/week @ 2023-12-20 74/week @ 2023-12-27 27/week @ 2024-01-03 29/week @ 2024-02-14 71/week @ 2024-02-21 490/week @ 2024-02-28 241/week @ 2024-03-06 294/week @ 2024-03-13 533/week @ 2024-03-20 53/week @ 2024-03-27 159/week @ 2024-04-03

1,058 downloads per month
Used in 9 crates

MIT license

57KB
958 lines

orx-pinned-vec

orx-pinned-vec crate orx-pinned-vec documentation

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. Pinned Elements Guarantee

A PinnedVec guarantees that positions of its elements do not change implicitly.

To be specific, let's assume that a pinned vector currently has n elements:

Method Expected Behavior
push(new_element) does not change the memory locations of the n elements
extend_from_slice(slice) does not change the memory locations of the first n elements
insert(a, new_element) does not change the memory locations of the first a elements, where a <= n; elements to the right of the inserted element might be changed, commonly shifted to right
pop() does not change the memory locations of the first n-1 elements, the n-th element is removed
remove(a) does not change the memory locations of the first a elements, where a < n; elements to the right of the removed element might be changed, commonly shifted to left
truncate(a) does not change the memory locations of the first a elements, where a < n

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.

B. Motivation

There are various situations where pinned elements are necessary.

  • It is critical in enabling efficient, convenient and safe self-referential collections with thin references, see SelfRefCol for details, and its special cases such as LinkedList.
  • It is essential in allowing an immutable push vector; i.e., ImpVec. This is a very useful operation when the desired collection is a bag or a container of things, rather than having a collective meaning. In such cases, ImpVec allows avoiding certain borrow checker complexities, heap allocations and wide pointers such as Box or Rc or etc.
  • It is important for concurrent programs since it eliminates safety concerns related with elements implicitly carried to different memory locations. This helps reducing and dealing with the complexity of concurrency. ConcurrentBag is a very simplistic and efficient concurrent data structure built on top of pinned vector guarantees.

C. Implementations

SplitVec and FixedVec are two efficient implementations.

License

This library is licensed under MIT license. See LICENSE for details.

No runtime deps