Struct CxxVector

Source
#[repr(C, packed(1))]
pub struct CxxVector<T> { /* private fields */ }
Expand description

Binding to C++ std::vector<T, std::allocator<T>>.

§Invariants

As an invariant of this API and the static analysis of the cxx::bridge macro, in Rust code we can never obtain a CxxVector by value. Instead in Rust code we will only ever look at a vector behind a reference or smart pointer, as in &CxxVector<T> or UniquePtr<CxxVector<T>>.

Implementations§

Source§

impl<T> CxxVector<T>
where T: VectorElement,

Source

pub fn new() -> UniquePtr<Self>

Constructs a new heap allocated vector, wrapped by UniquePtr.

The C++ vector is default constructed.

Source

pub fn len(&self) -> usize

Returns the number of elements in the vector.

Matches the behavior of C++ std::vector<T>::size.

Source

pub fn capacity(&self) -> usize

Returns the capacity of the vector.

Matches the behavior of C++ std::vector<T>::capacity.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Matches the behavior of C++ std::vector<T>::empty.

Source

pub fn get(&self, pos: usize) -> Option<&T>

Returns a reference to an element at the given position, or None if out of bounds.

Source

pub fn index_mut(self: Pin<&mut Self>, pos: usize) -> Option<Pin<&mut T>>

Returns a pinned mutable reference to an element at the given position, or None if out of bounds.

This method cannot be named “get_mut” due to a conflict with Pin::get_mut.

Source

pub unsafe fn get_unchecked(&self, pos: usize) -> &T

Returns a reference to an element without doing bounds checking.

This is generally not recommended, use with caution! Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

Matches the behavior of C++ std::vector<T>::operator[] const.

Source

pub unsafe fn index_unchecked_mut( self: Pin<&mut Self>, pos: usize, ) -> Pin<&mut T>

Returns a pinned mutable reference to an element without doing bounds checking.

This is generally not recommended, use with caution! Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

Matches the behavior of C++ std::vector<T>::operator[].

This method cannot be named “get_unchecked_mut” due to a conflict with Pin::get_unchecked_mut.

Source

pub fn as_slice(&self) -> &[T]
where T: ExternType<Kind = Trivial>,

Returns a slice to the underlying contiguous array of elements.

Source

pub fn as_mut_slice(self: Pin<&mut Self>) -> &mut [T]
where T: ExternType<Kind = Trivial>,

Returns a slice to the underlying contiguous array of elements by mutable reference.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over elements of type &T.

Source

pub fn iter_mut(self: Pin<&mut Self>) -> IterMut<'_, T>

Returns an iterator over elements of type Pin<&mut T>.

Source

pub fn push(self: Pin<&mut Self>, value: T)
where T: ExternType<Kind = Trivial>,

Appends an element to the back of the vector.

Matches the behavior of C++ std::vector<T>::push_back.

Source

pub fn pop(self: Pin<&mut Self>) -> Option<T>
where T: ExternType<Kind = Trivial>,

Removes the last element from a vector and returns it, or None if the vector is empty.

Source

pub fn reserve(self: Pin<&mut Self>, additional: usize)

Ensures that this vector’s capacity is at least additional elements larger than its length.

The capacity may be increased by more than additional elements if the implementation chooses, to amortize the cost of frequent reallocations.

The meaning of the argument is not the same as std::vector<T>::reserve in C++. The C++ standard library and Rust standard library both have a reserve method on vectors, but in C++ code the argument always refers to total capacity, whereas in Rust code it always refers to additional capacity. This API on CxxVector follows the Rust convention, the same way that for the length accessor we use the Rust conventional len() naming and not C++ size().

§Panics

Panics if the new capacity overflows usize.

Trait Implementations§

Source§

impl<T> Debug for CxxVector<T>
where T: VectorElement + Debug,

Source§

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

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

impl<T> Extend<T> for Pin<&mut CxxVector<T>>
where T: ExternType<Kind = Trivial> + VectorElement,

Source§

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

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<'a, T> IntoIterator for &'a CxxVector<T>
where T: VectorElement,

Source§

type Item = &'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<'a, T> IntoIterator for Pin<&'a mut CxxVector<T>>
where T: VectorElement,

Source§

type Item = Pin<&'a mut T>

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'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> UniquePtrTarget for CxxVector<T>
where T: VectorElement,

Auto Trait Implementations§

§

impl<T> Freeze for CxxVector<T>

§

impl<T> RefUnwindSafe for CxxVector<T>
where T: RefUnwindSafe,

§

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

§

impl<T> Sync for CxxVector<T>
where T: Sync,

§

impl<T> !Unpin for CxxVector<T>

§

impl<T> UnwindSafe for CxxVector<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> 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, 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.