Vec

Struct Vec 

Source
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> type is quite large on the stack. It is expected that you store it behind an Arc or similar.

Implementations§

Source§

impl<T> Vec<T>

Source

pub const fn new() -> Vec<T>

Constructs a new, empty Vec<T>.

§Examples
let vec: boxcar::Vec<i32> = boxcar::Vec::new();
Source

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);
Source

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);
Source

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]);
Source

pub fn push_with<F>(&self, f: F) -> usize
where F: FnOnce(usize) -> T,

Appends the element returned from the closure f to the back of the vector at the index supplied to the closure.

Returns the index that the element was inserted into.

§Examples
let vec = boxcar::vec![0, 1];
vec.push_with(|index| index);
assert_eq!(vec, [0, 1, 2]);
Source

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);
Source

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());
Source

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));
Source

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));
Source

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);
}
Source

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);
}
Source

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);
Source

pub fn clear(&mut self)

Clears the vector, removing all values.

Note that this method has no effect on the allocated capacity of the vector.

§Examples
let mut vec = boxcar::Vec::new();
vec.push(1);
vec.push(2);

vec.clear();
assert!(vec.is_empty());

vec.push(3); // Will not allocate.

Trait Implementations§

Source§

impl<T: Clone> Clone for Vec<T>

Source§

fn clone(&self) -> Vec<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Vec<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Vec<T>

Source§

fn default() -> Vec<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Extend<T> for Vec<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> FromIterator<T> for Vec<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Index<usize> for Vec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a Vec<T>

Source§

type Item = (usize, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for Vec<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<A, T> PartialEq<A> for Vec<T>
where A: AsRef<[T]>, T: PartialEq,

Source§

fn eq(&self, other: &A) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq for Vec<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for Vec<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Vec<T>

§

impl<T> RefUnwindSafe for Vec<T>

§

impl<T> Send for Vec<T>
where T: Send,

§

impl<T> Sync for Vec<T>
where T: Send + Sync,

§

impl<T> Unpin for Vec<T>

§

impl<T> UnwindSafe for Vec<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.