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