| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 1 | use std::mem; |
| 2 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 3 | pub trait VectorTarget<T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 4 | fn get_unchecked(v: &CxxVector<T>, pos: usize) -> &T |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 5 | where |
| 6 | Self: Sized; |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 7 | fn vector_length(v: &CxxVector<T>) -> usize |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 8 | where |
| 9 | Self: Sized; |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 10 | fn push_back(v: &CxxVector<T>, item: &T) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 11 | where |
| 12 | Self: Sized; |
| 13 | } |
| 14 | |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 15 | /// Binding to C++ `std::vector<T>`. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 16 | /// |
| 17 | /// # Invariants |
| 18 | /// |
| 19 | /// As an invariant of this API and the static analysis of the cxx::bridge |
| David Tolnay | 5fe9363 | 2020-04-24 12:31:00 -0700 | [diff] [blame^] | 20 | /// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in |
| 21 | /// Rust code we will only ever look at a vector behind a reference or smart |
| 22 | /// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`. |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 23 | #[repr(C, packed)] |
| 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 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 56 | pub struct VectorIntoIterator<'a, T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 57 | v: &'a CxxVector<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 58 | index: usize, |
| 59 | } |
| 60 | |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 61 | impl<'a, T: VectorTarget<T>> IntoIterator for &'a CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 62 | type Item = &'a T; |
| 63 | type IntoIter = VectorIntoIterator<'a, T>; |
| 64 | |
| 65 | fn into_iter(self) -> Self::IntoIter { |
| 66 | VectorIntoIterator { v: self, index: 0 } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl<'a, T: VectorTarget<T>> Iterator for VectorIntoIterator<'a, T> { |
| 71 | type Item = &'a T; |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 72 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 73 | fn next(&mut self) -> Option<Self::Item> { |
| 74 | self.index = self.index + 1; |
| 75 | self.v.get(self.index - 1) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | cxxbridge_macro::vector_builtin!(u8); |
| 80 | cxxbridge_macro::vector_builtin!(u16); |
| 81 | cxxbridge_macro::vector_builtin!(u32); |
| 82 | cxxbridge_macro::vector_builtin!(u64); |
| 83 | cxxbridge_macro::vector_builtin!(usize); |
| 84 | cxxbridge_macro::vector_builtin!(i8); |
| 85 | cxxbridge_macro::vector_builtin!(i16); |
| 86 | cxxbridge_macro::vector_builtin!(i32); |
| 87 | cxxbridge_macro::vector_builtin!(i64); |
| 88 | cxxbridge_macro::vector_builtin!(isize); |
| 89 | cxxbridge_macro::vector_builtin!(f32); |
| 90 | cxxbridge_macro::vector_builtin!(f64); |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 91 | |
| 92 | const_assert_eq!(1, mem::align_of::<CxxVector<usize>>()); |