| 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 | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 18 | pub fn len(&self) -> usize { |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 19 | T::__vector_size(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 true if the vector contains no elements. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 23 | pub fn is_empty(&self) -> bool { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 24 | self.len() == 0 |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 25 | } |
| 26 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 27 | /// Returns a reference to an element at the given position, or `None` if |
| 28 | /// out of bounds. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 29 | pub fn get(&self, pos: usize) -> Option<&T> { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 30 | if pos < self.len() { |
| David Tolnay | 31ad0be | 2020-04-24 13:29:51 -0700 | [diff] [blame] | 31 | Some(unsafe { T::__get_unchecked(self, pos) }) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 32 | } else { |
| 33 | None |
| 34 | } |
| 35 | } |
| 36 | |
| David Tolnay | 4944f2f | 2020-04-24 13:46:12 -0700 | [diff] [blame] | 37 | /// Returns a reference to an element without doing bounds checking. |
| 38 | /// |
| 39 | /// This is generally not recommended, use with caution! Calling this method |
| 40 | /// with an out-of-bounds index is undefined behavior even if the resulting |
| 41 | /// reference is not used. |
| 42 | pub unsafe fn get_unchecked(&self, pos: usize) -> &T { |
| 43 | T::__get_unchecked(self, pos) |
| 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 | |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 52 | pub struct Iter<'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; |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 59 | type IntoIter = Iter<'a, T>; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 60 | |
| 61 | fn into_iter(self) -> Self::IntoIter { |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 62 | Iter { v: self, index: 0 } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 66 | impl<'a, T: VectorElement> Iterator for Iter<'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 { |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 79 | fn __vector_size(v: &CxxVector<Self>) -> usize; |
| David Tolnay | cc75ad2 | 2020-04-24 14:45:16 -0700 | [diff] [blame] | 80 | unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self; |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 81 | fn __push_back(v: &CxxVector<Self>, item: &Self); |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 84 | macro_rules! impl_vector_element_for_primitive { |
| 85 | ($ty:ident) => { |
| 86 | unsafe impl VectorElement for $ty { |
| 87 | fn __vector_size(v: &CxxVector<$ty>) -> usize { |
| 88 | extern "C" { |
| 89 | attr! { |
| 90 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$size")] |
| 91 | fn __vector_size(_: &CxxVector<$ty>) -> usize; |
| 92 | } |
| 93 | } |
| 94 | unsafe { __vector_size(v) } |
| 95 | } |
| 96 | unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty { |
| 97 | extern "C" { |
| 98 | attr! { |
| 99 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$get_unchecked")] |
| 100 | fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty; |
| 101 | } |
| 102 | } |
| 103 | &*__get_unchecked(v, pos) |
| 104 | } |
| 105 | fn __push_back(v: &CxxVector<$ty>, item: &$ty) { |
| 106 | extern "C" { |
| 107 | attr! { |
| 108 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$push_back")] |
| 109 | fn __push_back(_: &CxxVector<$ty>, _: &$ty); |
| 110 | } |
| 111 | } |
| 112 | unsafe { __push_back(v, item) } |
| 113 | } |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | |
| David Tolnay | 4b91eaa | 2020-04-24 14:19:22 -0700 | [diff] [blame] | 118 | impl_vector_element_for_primitive!(u8); |
| 119 | impl_vector_element_for_primitive!(u16); |
| 120 | impl_vector_element_for_primitive!(u32); |
| 121 | impl_vector_element_for_primitive!(u64); |
| 122 | impl_vector_element_for_primitive!(usize); |
| 123 | impl_vector_element_for_primitive!(i8); |
| 124 | impl_vector_element_for_primitive!(i16); |
| 125 | impl_vector_element_for_primitive!(i32); |
| 126 | impl_vector_element_for_primitive!(i64); |
| 127 | impl_vector_element_for_primitive!(isize); |
| 128 | impl_vector_element_for_primitive!(f32); |
| 129 | impl_vector_element_for_primitive!(f64); |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 130 | |
| 131 | const_assert_eq!(1, mem::align_of::<CxxVector<usize>>()); |