blob: feb2e29cba7d4903ef0cd3958112d72b272ce77c [file] [log] [blame]
Myron Ahneba35cf2020-02-05 19:41:51 +07001pub trait VectorTarget<T> {
David Tolnaye90be1d2020-04-24 11:45:57 -07002 fn get_unchecked(v: &CxxVector<T>, pos: usize) -> &T
Myron Ahneba35cf2020-02-05 19:41:51 +07003 where
4 Self: Sized;
David Tolnaye90be1d2020-04-24 11:45:57 -07005 fn vector_length(v: &CxxVector<T>) -> usize
Myron Ahneba35cf2020-02-05 19:41:51 +07006 where
7 Self: Sized;
David Tolnaye90be1d2020-04-24 11:45:57 -07008 fn push_back(v: &CxxVector<T>, item: &T)
Myron Ahneba35cf2020-02-05 19:41:51 +07009 where
10 Self: Sized;
11}
12
David Tolnaye90be1d2020-04-24 11:45:57 -070013/// Binding to C++ `std::vector<T>`.
Myron Ahneba35cf2020-02-05 19:41:51 +070014///
15/// # Invariants
16///
17/// As an invariant of this API and the static analysis of the cxx::bridge
18/// macro, in Rust code we can never obtain a `Vector` by value. C++'s vector
19/// requires a move constructor and may hold internal pointers, which is not
20/// compatible with Rust's move behavior. Instead in Rust code we will only ever
21/// look at a Vector through a reference or smart pointer, as in `&Vector`
22/// or `UniquePtr<Vector>`.
23#[repr(C)]
David Tolnaye90be1d2020-04-24 11:45:57 -070024pub struct CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070025 _private: [T; 0],
26}
27
David Tolnaye90be1d2020-04-24 11:45:57 -070028impl<T: VectorTarget<T>> CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070029 /// Returns the length of the vector in bytes.
30 pub fn size(&self) -> usize {
31 T::vector_length(self)
32 }
33
34 pub fn get_unchecked(&self, pos: usize) -> &T {
35 T::get_unchecked(self, pos)
36 }
37
38 /// Returns true if `self` has a length of zero bytes.
39 pub fn is_empty(&self) -> bool {
40 self.size() == 0
41 }
42
43 pub fn get(&self, pos: usize) -> Option<&T> {
44 if pos < self.size() {
45 Some(self.get_unchecked(pos))
46 } else {
47 None
48 }
49 }
50
51 pub fn push_back(&mut self, item: &T) {
52 T::push_back(self, item);
53 }
54}
55
David Tolnaye90be1d2020-04-24 11:45:57 -070056unsafe impl<T> Send for CxxVector<T> where T: Send + VectorTarget<T> {}
Myron Ahneba35cf2020-02-05 19:41:51 +070057
58pub struct VectorIntoIterator<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070059 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070060 index: usize,
61}
62
David Tolnaye90be1d2020-04-24 11:45:57 -070063impl<'a, T: VectorTarget<T>> IntoIterator for &'a CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070064 type Item = &'a T;
65 type IntoIter = VectorIntoIterator<'a, T>;
66
67 fn into_iter(self) -> Self::IntoIter {
68 VectorIntoIterator { v: self, index: 0 }
69 }
70}
71
72impl<'a, T: VectorTarget<T>> Iterator for VectorIntoIterator<'a, T> {
73 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070074
Myron Ahneba35cf2020-02-05 19:41:51 +070075 fn next(&mut self) -> Option<Self::Item> {
76 self.index = self.index + 1;
77 self.v.get(self.index - 1)
78 }
79}
80
81cxxbridge_macro::vector_builtin!(u8);
82cxxbridge_macro::vector_builtin!(u16);
83cxxbridge_macro::vector_builtin!(u32);
84cxxbridge_macro::vector_builtin!(u64);
85cxxbridge_macro::vector_builtin!(usize);
86cxxbridge_macro::vector_builtin!(i8);
87cxxbridge_macro::vector_builtin!(i16);
88cxxbridge_macro::vector_builtin!(i32);
89cxxbridge_macro::vector_builtin!(i64);
90cxxbridge_macro::vector_builtin!(isize);
91cxxbridge_macro::vector_builtin!(f32);
92cxxbridge_macro::vector_builtin!(f64);