pub struct Vec<T> { /* private fields */ }Expand description
A concurrent, append-only vector.
See the crate documentation for details.
§Notes
The bucket array is stored inline, meaning that the
Vec<T> is quite large. It is expected that you
store it behind an Arc or similar.
Implementations§
source§impl<T> Vec<T>
impl<T> Vec<T>
sourcepub fn with_capacity(capacity: usize) -> Vec<T>
pub fn with_capacity(capacity: usize) -> Vec<T>
Constructs a new, empty Vec<T> with the specified capacity.
The vector will be able to hold at least capacity elements
without reallocating.
§Examples
let vec = boxcar::Vec::with_capacity(10);
for i in 0..10 {
// will not allocate
vec.push(i);
}
// may allocate
vec.push(11);sourcepub fn reserve(&self, additional: usize)
pub fn reserve(&self, additional: usize)
Reserves capacity for at least additional more elements to be inserted
in the given Vec<T>. The collection may reserve more space to avoid
frequent reallocations.
Does nothing if capacity is already sufficient.
§Examples
let vec = boxcar::Vec::new();
vec.reserve(10);
for i in 0..10 {
// will not allocate
vec.push(i);
}
// may allocate
vec.push(11);sourcepub fn push(&self, value: T) -> usize
pub fn push(&self, value: T) -> usize
Appends an element to the back of the vector, returning the index it was inserted into.
§Examples
let vec = boxcar::vec![1, 2];
assert_eq!(vec.push(3), 2);
assert_eq!(vec, [1, 2, 3]);sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Returns the number of elements in the vector.
Note that due to concurrent writes, it is not guaranteed
that all elements 0..vec.count() are initialized.
§Examples
let vec = boxcar::Vec::new();
assert_eq!(vec.count(), 0);
vec.push(1);
vec.push(2);
assert_eq!(vec.count(), 2);sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the vector contains no elements.
§Examples
let vec = boxcar::Vec::new();
assert!(vec.is_empty());
vec.push(1);
assert!(!vec.is_empty());sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at the given index.
§Examples
let vec = boxcar::vec![10, 40, 30];
assert_eq!(Some(&40), vec.get(1));
assert_eq!(None, vec.get(3));sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index.
§Examples
let mut vec = boxcar::vec![10, 40, 30];
assert_eq!(Some(&mut 40), vec.get_mut(1));
assert_eq!(None, vec.get_mut(3));sourcepub unsafe fn get_unchecked(&self, index: usize) -> &T
pub unsafe fn get_unchecked(&self, index: usize) -> &T
Returns a reference to an element, without doing bounds checking or verifying that the element is fully initialized.
For a safe alternative see get.
§Safety
Calling this method with an out-of-bounds index, or for an element that is being concurrently initialized is undefined behavior, even if the resulting reference is not used.
§Examples
let vec = boxcar::vec![1, 2, 4];
unsafe {
assert_eq!(vec.get_unchecked(1), &2);
}sourcepub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Returns a mutable reference to an element, without doing bounds checking or verifying that the element is fully initialized.
For a safe alternative see get.
§Safety
Calling this method with an out-of-bounds index is undefined behavior, even if the resulting reference is not used.
§Examples
let mut vec = boxcar::vec![1, 2, 4];
unsafe {
assert_eq!(vec.get_unchecked_mut(1), &mut 2);
}sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator over the vector.
Values are yielded in the form (index, value). The vector may
have in-progress concurrent writes that create gaps, so index
may not be strictly sequential.
§Examples
let vec = boxcar::vec![1, 2, 4];
let mut iterator = vec.iter();
assert_eq!(iterator.next(), Some((0, &1)));
assert_eq!(iterator.next(), Some((1, &2)));
assert_eq!(iterator.next(), Some((2, &4)));
assert_eq!(iterator.next(), None);Trait Implementations§
source§impl<T> Extend<T> for Vec<T>
impl<T> Extend<T> for Vec<T>
source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)