| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 1 | use std::mem; |
| 2 | |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 3 | /// Binding to C++ `std::vector<T>`. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 4 | /// |
| 5 | /// # Invariants |
| 6 | /// |
| 7 | /// 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] | 8 | /// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in |
| 9 | /// Rust code we will only ever look at a vector behind a reference or smart |
| 10 | /// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`. |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 11 | #[repr(C, packed)] |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 12 | pub struct CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 13 | _private: [T; 0], |
| 14 | } |
| 15 | |
| David Tolnay | 67ba0b5 | 2020-04-24 12:43:39 -0700 | [diff] [blame] | 16 | impl<T: VectorElement<T>> CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 17 | /// Returns the length of the vector in bytes. |
| 18 | pub fn size(&self) -> usize { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 19 | T::__vector_length(self) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | pub fn get_unchecked(&self, pos: usize) -> &T { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 23 | T::__get_unchecked(self, pos) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | /// Returns true if `self` has a length of zero bytes. |
| 27 | pub fn is_empty(&self) -> bool { |
| 28 | self.size() == 0 |
| 29 | } |
| 30 | |
| 31 | pub fn get(&self, pos: usize) -> Option<&T> { |
| 32 | if pos < self.size() { |
| 33 | Some(self.get_unchecked(pos)) |
| 34 | } else { |
| 35 | None |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn push_back(&mut self, item: &T) { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 40 | T::__push_back(self, item); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 44 | pub struct VectorIntoIterator<'a, T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 45 | v: &'a CxxVector<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 46 | index: usize, |
| 47 | } |
| 48 | |
| David Tolnay | 67ba0b5 | 2020-04-24 12:43:39 -0700 | [diff] [blame] | 49 | impl<'a, T: VectorElement<T>> IntoIterator for &'a CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 50 | type Item = &'a T; |
| 51 | type IntoIter = VectorIntoIterator<'a, T>; |
| 52 | |
| 53 | fn into_iter(self) -> Self::IntoIter { |
| 54 | VectorIntoIterator { v: self, index: 0 } |
| 55 | } |
| 56 | } |
| 57 | |
| David Tolnay | 67ba0b5 | 2020-04-24 12:43:39 -0700 | [diff] [blame] | 58 | impl<'a, T: VectorElement<T>> Iterator for VectorIntoIterator<'a, T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 59 | type Item = &'a T; |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 60 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 61 | fn next(&mut self) -> Option<Self::Item> { |
| 62 | self.index = self.index + 1; |
| 63 | self.v.get(self.index - 1) |
| 64 | } |
| 65 | } |
| 66 | |
| David Tolnay | 5104c86 | 2020-04-24 13:26:01 -0700 | [diff] [blame^] | 67 | // Methods are private; not intended to be implemented outside of cxxbridge |
| 68 | // codebase. |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 69 | #[doc(hidden)] |
| David Tolnay | 5104c86 | 2020-04-24 13:26:01 -0700 | [diff] [blame^] | 70 | pub unsafe trait VectorElement<T> { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 71 | fn __get_unchecked(v: &CxxVector<T>, pos: usize) -> &T |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 72 | where |
| 73 | Self: Sized; |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 74 | fn __vector_length(v: &CxxVector<T>) -> usize |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 75 | where |
| 76 | Self: Sized; |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 77 | fn __push_back(v: &CxxVector<T>, item: &T) |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 78 | where |
| 79 | Self: Sized; |
| 80 | } |
| 81 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 82 | cxxbridge_macro::vector_builtin!(u8); |
| 83 | cxxbridge_macro::vector_builtin!(u16); |
| 84 | cxxbridge_macro::vector_builtin!(u32); |
| 85 | cxxbridge_macro::vector_builtin!(u64); |
| 86 | cxxbridge_macro::vector_builtin!(usize); |
| 87 | cxxbridge_macro::vector_builtin!(i8); |
| 88 | cxxbridge_macro::vector_builtin!(i16); |
| 89 | cxxbridge_macro::vector_builtin!(i32); |
| 90 | cxxbridge_macro::vector_builtin!(i64); |
| 91 | cxxbridge_macro::vector_builtin!(isize); |
| 92 | cxxbridge_macro::vector_builtin!(f32); |
| 93 | cxxbridge_macro::vector_builtin!(f64); |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 94 | |
| 95 | const_assert_eq!(1, mem::align_of::<CxxVector<usize>>()); |