| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 1 | use std::mem; |
| 2 | |
| David Tolnay | 61a9fdf | 2020-04-24 16:19:42 -0700 | [diff] [blame] | 3 | /// Binding to C++ `std::vector<T, std::allocator<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 | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame^] | 16 | impl<T> CxxVector<T> |
| 17 | where |
| 18 | T: VectorElement, |
| 19 | { |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 20 | /// Returns the number of elements in the vector. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 21 | /// |
| 22 | /// Matches the behavior of C++ [std::vector\<T\>::size][size]. |
| 23 | /// |
| 24 | /// [size]: https://en.cppreference.com/w/cpp/container/vector/size |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 25 | pub fn len(&self) -> usize { |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 26 | T::__vector_size(self) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 27 | } |
| 28 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 29 | /// Returns true if the vector contains no elements. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 30 | /// |
| 31 | /// Matches the behavior of C++ [std::vector\<T\>::empty][empty]. |
| 32 | /// |
| 33 | /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 34 | pub fn is_empty(&self) -> bool { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 35 | self.len() == 0 |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 36 | } |
| 37 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 38 | /// Returns a reference to an element at the given position, or `None` if |
| 39 | /// out of bounds. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 40 | pub fn get(&self, pos: usize) -> Option<&T> { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 41 | if pos < self.len() { |
| David Tolnay | 31ad0be | 2020-04-24 13:29:51 -0700 | [diff] [blame] | 42 | Some(unsafe { T::__get_unchecked(self, pos) }) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 43 | } else { |
| 44 | None |
| 45 | } |
| 46 | } |
| 47 | |
| David Tolnay | 4944f2f | 2020-04-24 13:46:12 -0700 | [diff] [blame] | 48 | /// Returns a reference to an element without doing bounds checking. |
| 49 | /// |
| 50 | /// This is generally not recommended, use with caution! Calling this method |
| 51 | /// with an out-of-bounds index is undefined behavior even if the resulting |
| 52 | /// reference is not used. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 53 | /// |
| 54 | /// Matches the behavior of C++ |
| 55 | /// [std::vector\<T\>::operator\[\]][operator_at]. |
| 56 | /// |
| 57 | /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at |
| David Tolnay | 4944f2f | 2020-04-24 13:46:12 -0700 | [diff] [blame] | 58 | pub unsafe fn get_unchecked(&self, pos: usize) -> &T { |
| 59 | T::__get_unchecked(self, pos) |
| 60 | } |
| 61 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 62 | /// Appends an element to the back of the vector. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 63 | pub fn push_back(&mut self, item: &T) { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 64 | T::__push_back(self, item); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 68 | pub struct Iter<'a, T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 69 | v: &'a CxxVector<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 70 | index: usize, |
| 71 | } |
| 72 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame^] | 73 | impl<'a, T> IntoIterator for &'a CxxVector<T> |
| 74 | where |
| 75 | T: VectorElement, |
| 76 | { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 77 | type Item = &'a T; |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 78 | type IntoIter = Iter<'a, T>; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 79 | |
| 80 | fn into_iter(self) -> Self::IntoIter { |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 81 | Iter { v: self, index: 0 } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame^] | 85 | impl<'a, T> Iterator for Iter<'a, T> |
| 86 | where |
| 87 | T: VectorElement, |
| 88 | { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 89 | type Item = &'a T; |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 90 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 91 | fn next(&mut self) -> Option<Self::Item> { |
| 92 | self.index = self.index + 1; |
| 93 | self.v.get(self.index - 1) |
| 94 | } |
| 95 | } |
| 96 | |
| David Tolnay | 5104c86 | 2020-04-24 13:26:01 -0700 | [diff] [blame] | 97 | // Methods are private; not intended to be implemented outside of cxxbridge |
| 98 | // codebase. |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 99 | #[doc(hidden)] |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 100 | pub unsafe trait VectorElement: Sized { |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 101 | fn __vector_size(v: &CxxVector<Self>) -> usize; |
| David Tolnay | cc75ad2 | 2020-04-24 14:45:16 -0700 | [diff] [blame] | 102 | unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self; |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 103 | fn __push_back(v: &CxxVector<Self>, item: &Self); |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 106 | macro_rules! impl_vector_element_for_primitive { |
| 107 | ($ty:ident) => { |
| 108 | unsafe impl VectorElement for $ty { |
| 109 | fn __vector_size(v: &CxxVector<$ty>) -> usize { |
| 110 | extern "C" { |
| 111 | attr! { |
| 112 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$size")] |
| 113 | fn __vector_size(_: &CxxVector<$ty>) -> usize; |
| 114 | } |
| 115 | } |
| 116 | unsafe { __vector_size(v) } |
| 117 | } |
| 118 | unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty { |
| 119 | extern "C" { |
| 120 | attr! { |
| 121 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$get_unchecked")] |
| 122 | fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty; |
| 123 | } |
| 124 | } |
| 125 | &*__get_unchecked(v, pos) |
| 126 | } |
| 127 | fn __push_back(v: &CxxVector<$ty>, item: &$ty) { |
| 128 | extern "C" { |
| 129 | attr! { |
| 130 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$push_back")] |
| 131 | fn __push_back(_: &CxxVector<$ty>, _: &$ty); |
| 132 | } |
| 133 | } |
| 134 | unsafe { __push_back(v, item) } |
| 135 | } |
| 136 | } |
| 137 | }; |
| 138 | } |
| 139 | |
| David Tolnay | 4b91eaa | 2020-04-24 14:19:22 -0700 | [diff] [blame] | 140 | impl_vector_element_for_primitive!(u8); |
| 141 | impl_vector_element_for_primitive!(u16); |
| 142 | impl_vector_element_for_primitive!(u32); |
| 143 | impl_vector_element_for_primitive!(u64); |
| 144 | impl_vector_element_for_primitive!(usize); |
| 145 | impl_vector_element_for_primitive!(i8); |
| 146 | impl_vector_element_for_primitive!(i16); |
| 147 | impl_vector_element_for_primitive!(i32); |
| 148 | impl_vector_element_for_primitive!(i64); |
| 149 | impl_vector_element_for_primitive!(isize); |
| 150 | impl_vector_element_for_primitive!(f32); |
| 151 | impl_vector_element_for_primitive!(f64); |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 152 | |
| 153 | const_assert_eq!(1, mem::align_of::<CxxVector<usize>>()); |