| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame^] | 1 | use std::ffi::c_void; |
| 2 | use std::fmt::{self, Display}; |
| 3 | use std::marker::PhantomData; |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 4 | use std::mem; |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame^] | 5 | use std::ptr; |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 6 | |
| David Tolnay | 61a9fdf | 2020-04-24 16:19:42 -0700 | [diff] [blame] | 7 | /// Binding to C++ `std::vector<T, std::allocator<T>>`. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 8 | /// |
| 9 | /// # Invariants |
| 10 | /// |
| 11 | /// 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] | 12 | /// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in |
| 13 | /// Rust code we will only ever look at a vector behind a reference or smart |
| 14 | /// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`. |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 15 | #[repr(C, packed)] |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 16 | pub struct CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 17 | _private: [T; 0], |
| 18 | } |
| 19 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame] | 20 | impl<T> CxxVector<T> |
| 21 | where |
| 22 | T: VectorElement, |
| 23 | { |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 24 | /// Returns the number of elements in the vector. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 25 | /// |
| 26 | /// Matches the behavior of C++ [std::vector\<T\>::size][size]. |
| 27 | /// |
| 28 | /// [size]: https://en.cppreference.com/w/cpp/container/vector/size |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 29 | pub fn len(&self) -> usize { |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 30 | T::__vector_size(self) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 31 | } |
| 32 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 33 | /// Returns true if the vector contains no elements. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 34 | /// |
| 35 | /// Matches the behavior of C++ [std::vector\<T\>::empty][empty]. |
| 36 | /// |
| 37 | /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 38 | pub fn is_empty(&self) -> bool { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 39 | self.len() == 0 |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 40 | } |
| 41 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 42 | /// Returns a reference to an element at the given position, or `None` if |
| 43 | /// out of bounds. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 44 | pub fn get(&self, pos: usize) -> Option<&T> { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 45 | if pos < self.len() { |
| David Tolnay | 31ad0be | 2020-04-24 13:29:51 -0700 | [diff] [blame] | 46 | Some(unsafe { T::__get_unchecked(self, pos) }) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 47 | } else { |
| 48 | None |
| 49 | } |
| 50 | } |
| 51 | |
| David Tolnay | 4944f2f | 2020-04-24 13:46:12 -0700 | [diff] [blame] | 52 | /// Returns a reference to an element without doing bounds checking. |
| 53 | /// |
| 54 | /// This is generally not recommended, use with caution! Calling this method |
| 55 | /// with an out-of-bounds index is undefined behavior even if the resulting |
| 56 | /// reference is not used. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 57 | /// |
| 58 | /// Matches the behavior of C++ |
| 59 | /// [std::vector\<T\>::operator\[\]][operator_at]. |
| 60 | /// |
| 61 | /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at |
| David Tolnay | 4944f2f | 2020-04-24 13:46:12 -0700 | [diff] [blame] | 62 | pub unsafe fn get_unchecked(&self, pos: usize) -> &T { |
| 63 | T::__get_unchecked(self, pos) |
| 64 | } |
| 65 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 66 | /// Appends an element to the back of the vector. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 67 | pub fn push_back(&mut self, item: &T) { |
| David Tolnay | 0a63b4c | 2020-04-24 13:04:26 -0700 | [diff] [blame] | 68 | T::__push_back(self, item); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 72 | pub struct Iter<'a, T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 73 | v: &'a CxxVector<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 74 | index: usize, |
| 75 | } |
| 76 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame] | 77 | impl<'a, T> IntoIterator for &'a CxxVector<T> |
| 78 | where |
| 79 | T: VectorElement, |
| 80 | { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 81 | type Item = &'a T; |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 82 | type IntoIter = Iter<'a, T>; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 83 | |
| 84 | fn into_iter(self) -> Self::IntoIter { |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 85 | Iter { v: self, index: 0 } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame] | 89 | impl<'a, T> Iterator for Iter<'a, T> |
| 90 | where |
| 91 | T: VectorElement, |
| 92 | { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 93 | type Item = &'a T; |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 94 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 95 | fn next(&mut self) -> Option<Self::Item> { |
| 96 | self.index = self.index + 1; |
| 97 | self.v.get(self.index - 1) |
| 98 | } |
| 99 | } |
| 100 | |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame^] | 101 | pub struct TypeName<T> { |
| 102 | element: PhantomData<T>, |
| 103 | } |
| 104 | |
| 105 | impl<T> TypeName<T> { |
| 106 | pub const fn new() -> Self { |
| 107 | TypeName { |
| 108 | element: PhantomData, |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | impl<T> Display for TypeName<T> |
| 114 | where |
| 115 | T: VectorElement, |
| 116 | { |
| 117 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 118 | write!(formatter, "CxxVector<{}>", T::__NAME) |
| 119 | } |
| 120 | } |
| 121 | |
| David Tolnay | 5104c86 | 2020-04-24 13:26:01 -0700 | [diff] [blame] | 122 | // Methods are private; not intended to be implemented outside of cxxbridge |
| 123 | // codebase. |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 124 | #[doc(hidden)] |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 125 | pub unsafe trait VectorElement: Sized { |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame^] | 126 | const __NAME: &'static dyn Display; |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 127 | fn __vector_size(v: &CxxVector<Self>) -> usize; |
| David Tolnay | cc75ad2 | 2020-04-24 14:45:16 -0700 | [diff] [blame] | 128 | unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self; |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 129 | fn __push_back(v: &CxxVector<Self>, item: &Self); |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame^] | 130 | fn __unique_ptr_null() -> *mut c_void; |
| 131 | unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void; |
| 132 | unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>; |
| 133 | unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>; |
| 134 | unsafe fn __unique_ptr_drop(repr: *mut c_void); |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 137 | macro_rules! impl_vector_element_for_primitive { |
| 138 | ($ty:ident) => { |
| 139 | unsafe impl VectorElement for $ty { |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame^] | 140 | const __NAME: &'static dyn Display = &stringify!($ty); |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 141 | fn __vector_size(v: &CxxVector<$ty>) -> usize { |
| 142 | extern "C" { |
| 143 | attr! { |
| 144 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$size")] |
| 145 | fn __vector_size(_: &CxxVector<$ty>) -> usize; |
| 146 | } |
| 147 | } |
| 148 | unsafe { __vector_size(v) } |
| 149 | } |
| 150 | unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty { |
| 151 | extern "C" { |
| 152 | attr! { |
| 153 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$get_unchecked")] |
| 154 | fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty; |
| 155 | } |
| 156 | } |
| 157 | &*__get_unchecked(v, pos) |
| 158 | } |
| 159 | fn __push_back(v: &CxxVector<$ty>, item: &$ty) { |
| 160 | extern "C" { |
| 161 | attr! { |
| 162 | #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$push_back")] |
| 163 | fn __push_back(_: &CxxVector<$ty>, _: &$ty); |
| 164 | } |
| 165 | } |
| 166 | unsafe { __push_back(v, item) } |
| 167 | } |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame^] | 168 | fn __unique_ptr_null() -> *mut c_void { |
| 169 | extern "C" { |
| 170 | attr! { |
| 171 | #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$null")] |
| 172 | fn __unique_ptr_null(this: *mut *mut c_void); |
| 173 | } |
| 174 | } |
| 175 | let mut repr = ptr::null_mut::<c_void>(); |
| 176 | unsafe { __unique_ptr_null(&mut repr) } |
| 177 | repr |
| 178 | } |
| 179 | unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void { |
| 180 | extern "C" { |
| 181 | attr! { |
| 182 | #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$raw")] |
| 183 | fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>); |
| 184 | } |
| 185 | } |
| 186 | let mut repr = ptr::null_mut::<c_void>(); |
| 187 | __unique_ptr_raw(&mut repr, raw); |
| 188 | repr |
| 189 | } |
| 190 | unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> { |
| 191 | extern "C" { |
| 192 | attr! { |
| 193 | #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$get")] |
| 194 | fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>; |
| 195 | } |
| 196 | } |
| 197 | __unique_ptr_get(&repr) |
| 198 | } |
| 199 | unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> { |
| 200 | extern "C" { |
| 201 | attr! { |
| 202 | #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$release")] |
| 203 | fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>; |
| 204 | } |
| 205 | } |
| 206 | __unique_ptr_release(&mut repr) |
| 207 | } |
| 208 | unsafe fn __unique_ptr_drop(mut repr: *mut c_void) { |
| 209 | extern "C" { |
| 210 | attr! { |
| 211 | #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$drop")] |
| 212 | fn __unique_ptr_drop(this: *mut *mut c_void); |
| 213 | } |
| 214 | } |
| 215 | __unique_ptr_drop(&mut repr); |
| 216 | } |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 217 | } |
| 218 | }; |
| 219 | } |
| 220 | |
| David Tolnay | 4b91eaa | 2020-04-24 14:19:22 -0700 | [diff] [blame] | 221 | impl_vector_element_for_primitive!(u8); |
| 222 | impl_vector_element_for_primitive!(u16); |
| 223 | impl_vector_element_for_primitive!(u32); |
| 224 | impl_vector_element_for_primitive!(u64); |
| 225 | impl_vector_element_for_primitive!(usize); |
| 226 | impl_vector_element_for_primitive!(i8); |
| 227 | impl_vector_element_for_primitive!(i16); |
| 228 | impl_vector_element_for_primitive!(i32); |
| 229 | impl_vector_element_for_primitive!(i64); |
| 230 | impl_vector_element_for_primitive!(isize); |
| 231 | impl_vector_element_for_primitive!(f32); |
| 232 | impl_vector_element_for_primitive!(f64); |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 233 | |
| 234 | const_assert_eq!(1, mem::align_of::<CxxVector<usize>>()); |