| 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 | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 16 | impl<T: VectorElement> CxxVector<T> { |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame^] | 17 | /// Returns the number of elements in the vector. |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 18 | pub fn len(&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 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame^] | 22 | /// Returns a reference to an element without doing bounds checking. |
| 23 | /// |
| 24 | /// This is generally not recommended, use with caution! Calling this method |
| 25 | /// with an out-of-bounds index is undefined behavior even if the resulting |
| 26 | /// reference is not used. |
| David Tolnay | 31ad0be | 2020-04-24 13:29:51 -0700 | [diff] [blame] | 27 | pub unsafe fn get_unchecked(&self, pos: usize) -> &T { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 28 | T::__get_unchecked(self, pos) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 29 | } |
| 30 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame^] | 31 | /// Returns true if the vector contains no elements. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 32 | pub fn is_empty(&self) -> bool { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 33 | self.len() == 0 |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 34 | } |
| 35 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame^] | 36 | /// Returns a reference to an element at the given position, or `None` if |
| 37 | /// out of bounds. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 38 | pub fn get(&self, pos: usize) -> Option<&T> { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 39 | if pos < self.len() { |
| David Tolnay | 31ad0be | 2020-04-24 13:29:51 -0700 | [diff] [blame] | 40 | Some(unsafe { T::__get_unchecked(self, pos) }) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 41 | } else { |
| 42 | None |
| 43 | } |
| 44 | } |
| 45 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame^] | 46 | /// Appends an element to the back of the vector. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 47 | pub fn push_back(&mut self, item: &T) { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 48 | T::__push_back(self, item); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 52 | pub struct VectorIntoIterator<'a, T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 53 | v: &'a CxxVector<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 54 | index: usize, |
| 55 | } |
| 56 | |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 57 | impl<'a, T: VectorElement> IntoIterator for &'a CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 58 | type Item = &'a T; |
| 59 | type IntoIter = VectorIntoIterator<'a, T>; |
| 60 | |
| 61 | fn into_iter(self) -> Self::IntoIter { |
| 62 | VectorIntoIterator { v: self, index: 0 } |
| 63 | } |
| 64 | } |
| 65 | |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 66 | impl<'a, T: VectorElement> Iterator for VectorIntoIterator<'a, T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 67 | type Item = &'a T; |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 68 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 69 | fn next(&mut self) -> Option<Self::Item> { |
| 70 | self.index = self.index + 1; |
| 71 | self.v.get(self.index - 1) |
| 72 | } |
| 73 | } |
| 74 | |
| David Tolnay | 5104c86 | 2020-04-24 13:26:01 -0700 | [diff] [blame] | 75 | // Methods are private; not intended to be implemented outside of cxxbridge |
| 76 | // codebase. |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 77 | #[doc(hidden)] |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 78 | pub unsafe trait VectorElement: Sized { |
| 79 | unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self; |
| 80 | fn __vector_length(v: &CxxVector<Self>) -> usize; |
| 81 | fn __push_back(v: &CxxVector<Self>, item: &Self); |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 84 | cxxbridge_macro::vector_builtin!(u8); |
| 85 | cxxbridge_macro::vector_builtin!(u16); |
| 86 | cxxbridge_macro::vector_builtin!(u32); |
| 87 | cxxbridge_macro::vector_builtin!(u64); |
| 88 | cxxbridge_macro::vector_builtin!(usize); |
| 89 | cxxbridge_macro::vector_builtin!(i8); |
| 90 | cxxbridge_macro::vector_builtin!(i16); |
| 91 | cxxbridge_macro::vector_builtin!(i32); |
| 92 | cxxbridge_macro::vector_builtin!(i64); |
| 93 | cxxbridge_macro::vector_builtin!(isize); |
| 94 | cxxbridge_macro::vector_builtin!(f32); |
| 95 | cxxbridge_macro::vector_builtin!(f64); |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 96 | |
| 97 | const_assert_eq!(1, mem::align_of::<CxxVector<usize>>()); |