| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1 | pub trait VectorTarget<T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 2 | fn get_unchecked(v: &CxxVector<T>, pos: usize) -> &T |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 3 | where |
| 4 | Self: Sized; |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 5 | fn vector_length(v: &CxxVector<T>) -> usize |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 6 | where |
| 7 | Self: Sized; |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 8 | fn push_back(v: &CxxVector<T>, item: &T) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 9 | where |
| 10 | Self: Sized; |
| 11 | } |
| 12 | |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 13 | /// Binding to C++ `std::vector<T>`. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 14 | /// |
| 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 Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 24 | pub struct CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 25 | _private: [T; 0], |
| 26 | } |
| 27 | |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 28 | impl<T: VectorTarget<T>> CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 29 | /// 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 Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 56 | unsafe impl<T> Send for CxxVector<T> where T: Send + VectorTarget<T> {} |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 57 | |
| 58 | pub struct VectorIntoIterator<'a, T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 59 | v: &'a CxxVector<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 60 | index: usize, |
| 61 | } |
| 62 | |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame^] | 63 | impl<'a, T: VectorTarget<T>> IntoIterator for &'a CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 64 | 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 | |
| 72 | impl<'a, T: VectorTarget<T>> Iterator for VectorIntoIterator<'a, T> { |
| 73 | type Item = &'a T; |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 74 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 75 | fn next(&mut self) -> Option<Self::Item> { |
| 76 | self.index = self.index + 1; |
| 77 | self.v.get(self.index - 1) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | cxxbridge_macro::vector_builtin!(u8); |
| 82 | cxxbridge_macro::vector_builtin!(u16); |
| 83 | cxxbridge_macro::vector_builtin!(u32); |
| 84 | cxxbridge_macro::vector_builtin!(u64); |
| 85 | cxxbridge_macro::vector_builtin!(usize); |
| 86 | cxxbridge_macro::vector_builtin!(i8); |
| 87 | cxxbridge_macro::vector_builtin!(i16); |
| 88 | cxxbridge_macro::vector_builtin!(i32); |
| 89 | cxxbridge_macro::vector_builtin!(i64); |
| 90 | cxxbridge_macro::vector_builtin!(isize); |
| 91 | cxxbridge_macro::vector_builtin!(f32); |
| 92 | cxxbridge_macro::vector_builtin!(f64); |