rate_common/memory/
boundedvector.rs

1//! `BoundedVector` is a non-growable
2//! [`std::vec::Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html).
3
4use crate::memory::{HeapSpace, Vector};
5use rate_macros::HeapSpace;
6use std::{
7    ops::{Index, IndexMut},
8    slice,
9};
10
11/// A contiguous but non-growable array type, using [`Vector`](../vector/struct.Vector.html)
12///
13/// This exposes a subset of the `Vector` API (and thus essentially behaves
14/// like a `std::vec::Vec`). Notably, it does not provide functions that grow the
15/// capacity of the vector.
16///
17/// A `BoundedVector` can be used as a stack with a known maximum size.
18#[derive(Debug, Clone, HeapSpace, PartialEq, Default)]
19pub struct BoundedVector<T>
20where
21    T: HeapSpace,
22{
23    /// The wrapped `Vector`
24    vector: Vector<T>,
25}
26
27impl<T: HeapSpace> BoundedVector<T> {
28    /// See [`Vec::with_capacity()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.with_capacity).
29    pub fn with_capacity(capacity: usize) -> BoundedVector<T> {
30        BoundedVector {
31            vector: Vector::with_capacity(capacity),
32        }
33    }
34    /// Pushes a value, increasing the vector's length by one.
35    ///
36    /// Note that unlike `
37    /// [`Vec::push()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push)
38    /// this does not grow the vector if it is full.
39    ///
40    /// # Panics
41    /// Panics if there is no space for the new element.
42    pub fn push(&mut self, value: T) {
43        self.vector.push_no_grow(value)
44    }
45    /// See [`Vec::len()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.len).
46    pub fn len(&self) -> usize {
47        self.vector.len()
48    }
49    /// See [`Vec::is_empty()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.is_empty).
50    pub fn is_empty(&self) -> bool {
51        self.vector.is_empty()
52    }
53    /// See [`Vec::capacity()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.capacity).
54    pub fn capacity(&self) -> usize {
55        self.vector.capacity()
56    }
57    /// See [`Vec::pop()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.pop).
58    pub fn pop(&mut self) -> Option<T> {
59        self.vector.pop()
60    }
61    /// See [`Vec::first()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.first).
62    pub fn first(&self) -> &T {
63        self.vector.first()
64    }
65    /// See [`Vec::last()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.last).
66    pub fn last(&self) -> &T {
67        self.vector.last()
68    }
69    /// See [`Vec::iter()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.iter).
70    pub fn iter(&self) -> slice::Iter<T> {
71        self.vector.iter()
72    }
73    /// See [`Vec::as_ptr()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_ptr).
74    pub fn as_ptr(&mut self) -> *const T {
75        self.vector.as_ptr()
76    }
77    /// See [`Vec::mut_ptr()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.mut_ptr).
78    pub fn mut_ptr(&mut self) -> *mut T {
79        self.vector.mut_ptr()
80    }
81    /// See [`Vec::truncate()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.truncate).
82    pub fn truncate(&mut self, new_length: usize) {
83        self.vector.truncate(new_length)
84    }
85    /// See [`Vec::clear()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.clear).
86    pub fn clear(&mut self) {
87        self.vector.clear()
88    }
89}
90
91impl<T: HeapSpace + Clone + Default> BoundedVector<T> {
92    /// See [`Vec::resize()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.resize).
93    pub fn resize(&mut self, new_length: usize) {
94        self.vector.resize(new_length)
95    }
96}
97
98impl<T: HeapSpace + Ord> BoundedVector<T> {
99    /// See [`Vec::sort_unstable()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort_unstable).
100    pub fn sort_unstable(&mut self) {
101        self.vector.sort_unstable()
102    }
103}
104
105impl<T: HeapSpace> BoundedVector<T> {
106    /// See [`Vec::sort_unstable_by_key()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort_unstable_by_key).
107    pub fn sort_unstable_by_key<F, K>(&mut self, f: F)
108    where
109        F: FnMut(&T) -> K,
110        K: Ord,
111    {
112        self.vector.sort_unstable_by_key(f)
113    }
114}
115
116impl<T: HeapSpace> Index<usize> for BoundedVector<T> {
117    type Output = T;
118    fn index(&self, offset: usize) -> &T {
119        self.vector.index(offset)
120    }
121}
122
123impl<T: HeapSpace> IndexMut<usize> for BoundedVector<T> {
124    fn index_mut(&mut self, offset: usize) -> &mut T {
125        self.vector.index_mut(offset)
126    }
127}
128
129impl<'a, T: HeapSpace> IntoIterator for &'a BoundedVector<T> {
130    type Item = &'a T;
131    type IntoIter = slice::Iter<'a, T>;
132    fn into_iter(self) -> Self::IntoIter {
133        self.vector.iter()
134    }
135}