• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1{{#title rust::Vec<T> — Rust ♡ C++}}
2# rust::Vec\<T\>
3
4### Public API:
5
6```cpp,hidelines
7// rust/cxx.h
8#
9# #include <initializer_list>
10# #include <iterator>
11# #include <type_traits>
12#
13# namespace rust {
14
15template <typename T>
16class Vec final {
17public:
18  using value_type = T;
19
20  Vec() noexcept;
21  Vec(std::initializer_list<T>);
22  Vec(const Vec &);
23  Vec(Vec &&) noexcept;
24  ~Vec() noexcept;
25
26  Vec &operator=(Vec &&) noexcept;
27  Vec &operator=(const Vec &);
28
29  size_t size() const noexcept;
30  bool empty() const noexcept;
31  const T *data() const noexcept;
32  T *data() noexcept;
33  size_t capacity() const noexcept;
34
35  const T &operator[](size_t n) const noexcept;
36  const T &at(size_t n) const;
37  const T &front() const;
38  const T &back() const;
39
40  T &operator[](size_t n) noexcept;
41  T &at(size_t n);
42  T &front();
43  T &back();
44
45  void reserve(size_t new_cap);
46  void push_back(const T &value);
47  void push_back(T &&value);
48  template <typename... Args>
49  void emplace_back(Args &&...args);
50
51  class iterator;
52  iterator begin() noexcept;
53  iterator end() noexcept;
54
55  class const_iterator;
56  const_iterator begin() const noexcept;
57  const_iterator end() const noexcept;
58  const_iterator cbegin() const noexcept;
59  const_iterator cend() const noexcept;
60
61  void swap(Vec &) noexcept;
62};
63#
64# template <typename T>
65# class Vec<T>::iterator final {
66# public:
67#   using iterator_category = std::random_access_iterator_tag;
68#   using value_type = T;
69#   using pointer = T *;
70#   using reference = T &;
71#
72#   T &operator*() const noexcept;
73#   T *operator->() const noexcept;
74#   T &operator[](ptrdiff_t) const noexcept;
75#
76#   iterator &operator++() noexcept;
77#   iterator operator++(int) noexcept;
78#   iterator &operator--() noexcept;
79#   iterator operator--(int) noexcept;
80#
81#   iterator &operator+=(ptrdiff_t) noexcept;
82#   iterator &operator-=(ptrdiff_t) noexcept;
83#   iterator operator+(ptrdiff_t) const noexcept;
84#   iterator operator-(ptrdiff_t) const noexcept;
85#   ptrdiff_t operator-(const iterator &) const noexcept;
86#
87#   bool operator==(const iterator &) const noexcept;
88#   bool operator!=(const iterator &) const noexcept;
89#   bool operator<(const iterator &) const noexcept;
90#   bool operator<=(const iterator &) const noexcept;
91#   bool operator>(const iterator &) const noexcept;
92#   bool operator>=(const iterator &) const noexcept;
93# };
94#
95# template <typename T>
96# class Vec<T>::const_iterator final {
97# public:
98#   using iterator_category = std::random_access_iterator_tag;
99#   using value_type = const T;
100#   using pointer = const T *;
101#   using reference = const T &;
102#
103#   const T &operator*() const noexcept;
104#   const T *operator->() const noexcept;
105#   const T &operator[](ptrdiff_t) const noexcept;
106#
107#   const_iterator &operator++() noexcept;
108#   const_iterator operator++(int) noexcept;
109#   const_iterator &operator--() noexcept;
110#   const_iterator operator--(int) noexcept;
111#
112#   const_iterator &operator+=(ptrdiff_t) noexcept;
113#   const_iterator &operator-=(ptrdiff_t) noexcept;
114#   const_iterator operator+(ptrdiff_t) const noexcept;
115#   const_iterator operator-(ptrdiff_t) const noexcept;
116#   ptrdiff_t operator-(const const_iterator &) const noexcept;
117#
118#   bool operator==(const const_iterator &) const noexcept;
119#   bool operator!=(const const_iterator &) const noexcept;
120#   bool operator<(const const_iterator &) const noexcept;
121#   bool operator<=(const const_iterator &) const noexcept;
122#   bool operator>(const const_iterator &) const noexcept;
123#   bool operator>=(const const_iterator &) const noexcept;
124# };
125#
126# } // namespace rust
127```
128
129### Restrictions:
130
131Vec\<T\> does not support T being an opaque C++ type. You should use
132CxxVector\<T\> (C++ std::vector\<T\>) instead for collections of opaque C++
133types on the language boundary.
134
135## Example
136
137```rust,noplayground
138// src/main.rs
139
140#[cxx::bridge]
141mod ffi {
142    struct Shared {
143        v: u32,
144    }
145
146    unsafe extern "C++" {
147        include!("example/include/example.h");
148
149        fn f(elements: Vec<Shared>);
150    }
151}
152
153fn main() {
154    let shared = |v| ffi::Shared { v };
155    let elements = vec![shared(3), shared(2), shared(1)];
156    ffi::f(elements);
157}
158```
159
160```cpp
161// include/example.h
162
163#pragma once
164#include "example/src/main.rs.h"
165#include "rust/cxx.h"
166
167void f(rust::Vec<Shared> elements);
168```
169
170```cpp
171// src/example.cc
172
173#include "example/include/example.h"
174#include <algorithm>
175#include <cassert>
176#include <iostream>
177#include <iterator>
178#include <vector>
179
180void f(rust::Vec<Shared> v) {
181  for (auto shared : v) {
182    std::cout << shared.v << std::endl;
183  }
184
185  // Copy the elements to a C++ std::vector using STL algorithm.
186  std::vector<Shared> stdv;
187  std::copy(v.begin(), v.end(), std::back_inserter(stdv));
188  assert(v.size() == stdv.size());
189}
190```
191