| David Tolnay | b5d039c | 2020-12-12 23:21:17 -0800 | [diff] [blame] | 1 | //! Less used details of `CxxVector` are exposed in this module. `CxxVector` |
| 2 | //! itself is exposed at the crate root. |
| 3 | |
| David Tolnay | 181ee91 | 2020-12-04 12:15:10 -0800 | [diff] [blame] | 4 | use crate::extern_type::ExternType; |
| 5 | use crate::kind::Trivial; |
| David Tolnay | bac2582 | 2020-12-12 23:13:51 -0800 | [diff] [blame] | 6 | use crate::string::CxxString; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 7 | use core::ffi::c_void; |
| David Tolnay | ac5af50 | 2021-03-25 00:29:06 -0400 | [diff] [blame] | 8 | use core::fmt::{self, Debug}; |
| David Tolnay | 526faa2 | 2020-12-13 16:10:47 -0800 | [diff] [blame] | 9 | use core::iter::FusedIterator; |
| David Tolnay | 95dab1d | 2020-11-15 14:32:37 -0800 | [diff] [blame] | 10 | use core::marker::{PhantomData, PhantomPinned}; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 11 | use core::mem; |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 12 | use core::pin::Pin; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 13 | use core::ptr; |
| David Tolnay | 93637ca | 2020-09-24 15:58:20 -0400 | [diff] [blame] | 14 | use core::slice; |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 15 | |
| David Tolnay | 61a9fdf | 2020-04-24 16:19:42 -0700 | [diff] [blame] | 16 | /// Binding to C++ `std::vector<T, std::allocator<T>>`. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 17 | /// |
| 18 | /// # Invariants |
| 19 | /// |
| 20 | /// 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] | 21 | /// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in |
| 22 | /// Rust code we will only ever look at a vector behind a reference or smart |
| 23 | /// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`. |
| David Tolnay | 4f7e6fa | 2020-04-24 11:52:44 -0700 | [diff] [blame] | 24 | #[repr(C, packed)] |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 25 | pub struct CxxVector<T> { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 26 | _private: [T; 0], |
| David Tolnay | 95dab1d | 2020-11-15 14:32:37 -0800 | [diff] [blame] | 27 | _pinned: PhantomData<PhantomPinned>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 28 | } |
| 29 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame] | 30 | impl<T> CxxVector<T> |
| 31 | where |
| 32 | T: VectorElement, |
| 33 | { |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 34 | /// Returns the number of elements in the vector. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 35 | /// |
| 36 | /// Matches the behavior of C++ [std::vector\<T\>::size][size]. |
| 37 | /// |
| 38 | /// [size]: https://en.cppreference.com/w/cpp/container/vector/size |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 39 | pub fn len(&self) -> usize { |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 40 | T::__vector_size(self) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 41 | } |
| 42 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 43 | /// Returns true if the vector contains no elements. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 44 | /// |
| 45 | /// Matches the behavior of C++ [std::vector\<T\>::empty][empty]. |
| 46 | /// |
| 47 | /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 48 | pub fn is_empty(&self) -> bool { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 49 | self.len() == 0 |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 50 | } |
| 51 | |
| David Tolnay | cdc8796 | 2020-04-24 13:45:59 -0700 | [diff] [blame] | 52 | /// Returns a reference to an element at the given position, or `None` if |
| 53 | /// out of bounds. |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 54 | pub fn get(&self, pos: usize) -> Option<&T> { |
| David Tolnay | c01d0a0 | 2020-04-24 13:30:44 -0700 | [diff] [blame] | 55 | if pos < self.len() { |
| David Tolnay | 93637ca | 2020-09-24 15:58:20 -0400 | [diff] [blame] | 56 | Some(unsafe { self.get_unchecked(pos) }) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 57 | } else { |
| 58 | None |
| 59 | } |
| 60 | } |
| 61 | |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 62 | /// Returns a pinned mutable reference to an element at the given position, |
| 63 | /// or `None` if out of bounds. |
| David Tolnay | 5b395b3 | 2020-12-31 10:44:26 -0800 | [diff] [blame] | 64 | pub fn index_mut(self: Pin<&mut Self>, pos: usize) -> Option<Pin<&mut T>> { |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 65 | if pos < self.len() { |
| David Tolnay | 5b395b3 | 2020-12-31 10:44:26 -0800 | [diff] [blame] | 66 | Some(unsafe { self.index_unchecked_mut(pos) }) |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 67 | } else { |
| 68 | None |
| 69 | } |
| 70 | } |
| 71 | |
| David Tolnay | 4944f2f | 2020-04-24 13:46:12 -0700 | [diff] [blame] | 72 | /// Returns a reference to an element without doing bounds checking. |
| 73 | /// |
| 74 | /// This is generally not recommended, use with caution! Calling this method |
| 75 | /// with an out-of-bounds index is undefined behavior even if the resulting |
| 76 | /// reference is not used. |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 77 | /// |
| 78 | /// Matches the behavior of C++ |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 79 | /// [std::vector\<T\>::operator\[\] const][operator_at]. |
| 80 | /// |
| 81 | /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at |
| 82 | pub unsafe fn get_unchecked(&self, pos: usize) -> &T { |
| 83 | let this = self as *const CxxVector<T> as *mut CxxVector<T>; |
| 84 | let ptr = T::__get_unchecked(this, pos) as *const T; |
| 85 | &*ptr |
| 86 | } |
| 87 | |
| 88 | /// Returns a pinned mutable reference to an element without doing bounds |
| 89 | /// checking. |
| 90 | /// |
| 91 | /// This is generally not recommended, use with caution! Calling this method |
| 92 | /// with an out-of-bounds index is undefined behavior even if the resulting |
| 93 | /// reference is not used. |
| 94 | /// |
| 95 | /// Matches the behavior of C++ |
| David Tolnay | dd83919 | 2020-04-24 16:41:29 -0700 | [diff] [blame] | 96 | /// [std::vector\<T\>::operator\[\]][operator_at]. |
| 97 | /// |
| 98 | /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at |
| David Tolnay | 5b395b3 | 2020-12-31 10:44:26 -0800 | [diff] [blame] | 99 | pub unsafe fn index_unchecked_mut(self: Pin<&mut Self>, pos: usize) -> Pin<&mut T> { |
| 100 | let ptr = T::__get_unchecked(self.get_unchecked_mut(), pos); |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 101 | Pin::new_unchecked(&mut *ptr) |
| David Tolnay | 93637ca | 2020-09-24 15:58:20 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /// Returns a slice to the underlying contiguous array of elements. |
| David Tolnay | 181ee91 | 2020-12-04 12:15:10 -0800 | [diff] [blame] | 105 | pub fn as_slice(&self) -> &[T] |
| 106 | where |
| 107 | T: ExternType<Kind = Trivial>, |
| 108 | { |
| David Tolnay | 93637ca | 2020-09-24 15:58:20 -0400 | [diff] [blame] | 109 | let len = self.len(); |
| 110 | if len == 0 { |
| David Tolnay | a5a14ce | 2020-09-24 16:02:40 -0400 | [diff] [blame] | 111 | // The slice::from_raw_parts in the other branch requires a nonnull |
| 112 | // and properly aligned data ptr. C++ standard does not guarantee |
| 113 | // that data() on a vector with size 0 would return a nonnull |
| 114 | // pointer or sufficiently aligned pointer, so using it would be |
| 115 | // undefined behavior. Create our own empty slice in Rust instead |
| 116 | // which upholds the invariants. |
| David Tolnay | acc7fb0 | 2020-09-24 18:10:09 -0400 | [diff] [blame] | 117 | &[] |
| David Tolnay | 93637ca | 2020-09-24 15:58:20 -0400 | [diff] [blame] | 118 | } else { |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 119 | let this = self as *const CxxVector<T> as *mut CxxVector<T>; |
| 120 | let ptr = unsafe { T::__get_unchecked(this, 0) }; |
| David Tolnay | 93637ca | 2020-09-24 15:58:20 -0400 | [diff] [blame] | 121 | unsafe { slice::from_raw_parts(ptr, len) } |
| 122 | } |
| David Tolnay | 4944f2f | 2020-04-24 13:46:12 -0700 | [diff] [blame] | 123 | } |
| David Tolnay | 4f71cc5 | 2020-11-15 23:55:27 -0800 | [diff] [blame] | 124 | |
| David Tolnay | ab1ac88 | 2020-12-31 11:54:37 -0800 | [diff] [blame] | 125 | /// Returns a slice to the underlying contiguous array of elements by |
| 126 | /// mutable reference. |
| 127 | pub fn as_mut_slice(self: Pin<&mut Self>) -> &mut [T] |
| 128 | where |
| 129 | T: ExternType<Kind = Trivial>, |
| 130 | { |
| 131 | let len = self.len(); |
| 132 | if len == 0 { |
| 133 | &mut [] |
| 134 | } else { |
| 135 | let ptr = unsafe { T::__get_unchecked(self.get_unchecked_mut(), 0) }; |
| 136 | unsafe { slice::from_raw_parts_mut(ptr, len) } |
| 137 | } |
| 138 | } |
| 139 | |
| David Tolnay | 4f71cc5 | 2020-11-15 23:55:27 -0800 | [diff] [blame] | 140 | /// Returns an iterator over elements of type `&T`. |
| 141 | pub fn iter(&self) -> Iter<T> { |
| 142 | Iter { v: self, index: 0 } |
| 143 | } |
| David Tolnay | 26a5292 | 2020-12-21 17:29:04 -0800 | [diff] [blame] | 144 | |
| 145 | /// Returns an iterator over elements of type `Pin<&mut T>`. |
| David Tolnay | 30bea1c | 2020-12-31 10:41:42 -0800 | [diff] [blame] | 146 | pub fn iter_mut(self: Pin<&mut Self>) -> IterMut<T> { |
| David Tolnay | 26a5292 | 2020-12-21 17:29:04 -0800 | [diff] [blame] | 147 | IterMut { v: self, index: 0 } |
| 148 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 149 | } |
| 150 | |
| David Tolnay | b5d039c | 2020-12-12 23:21:17 -0800 | [diff] [blame] | 151 | /// Iterator over elements of a `CxxVector` by shared reference. |
| 152 | /// |
| 153 | /// The iterator element type is `&'a T`. |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 154 | pub struct Iter<'a, T> { |
| David Tolnay | e90be1d | 2020-04-24 11:45:57 -0700 | [diff] [blame] | 155 | v: &'a CxxVector<T>, |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 156 | index: usize, |
| 157 | } |
| 158 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame] | 159 | impl<'a, T> IntoIterator for &'a CxxVector<T> |
| 160 | where |
| 161 | T: VectorElement, |
| 162 | { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 163 | type Item = &'a T; |
| David Tolnay | 3d88bdc | 2020-04-24 13:48:18 -0700 | [diff] [blame] | 164 | type IntoIter = Iter<'a, T>; |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 165 | |
| 166 | fn into_iter(self) -> Self::IntoIter { |
| David Tolnay | 4f71cc5 | 2020-11-15 23:55:27 -0800 | [diff] [blame] | 167 | self.iter() |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| David Tolnay | 4074ad2 | 2020-04-24 18:20:11 -0700 | [diff] [blame] | 171 | impl<'a, T> Iterator for Iter<'a, T> |
| 172 | where |
| 173 | T: VectorElement, |
| 174 | { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 175 | type Item = &'a T; |
| David Tolnay | 85db5a0 | 2020-04-25 13:17:27 -0700 | [diff] [blame] | 176 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 177 | fn next(&mut self) -> Option<Self::Item> { |
| David Tolnay | 0d52717 | 2020-12-21 17:35:24 -0800 | [diff] [blame] | 178 | let next = self.v.get(self.index)?; |
| 179 | self.index += 1; |
| 180 | Some(next) |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 181 | } |
| David Tolnay | 724ac75 | 2020-12-13 16:00:48 -0800 | [diff] [blame] | 182 | |
| 183 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 184 | let len = self.len(); |
| 185 | (len, Some(len)) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | impl<'a, T> ExactSizeIterator for Iter<'a, T> |
| 190 | where |
| 191 | T: VectorElement, |
| 192 | { |
| 193 | fn len(&self) -> usize { |
| 194 | self.v.len() - self.index |
| 195 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 196 | } |
| 197 | |
| David Tolnay | 526faa2 | 2020-12-13 16:10:47 -0800 | [diff] [blame] | 198 | impl<'a, T> FusedIterator for Iter<'a, T> where T: VectorElement {} |
| 199 | |
| David Tolnay | 26a5292 | 2020-12-21 17:29:04 -0800 | [diff] [blame] | 200 | /// Iterator over elements of a `CxxVector` by pinned mutable reference. |
| 201 | /// |
| 202 | /// The iterator element type is `Pin<&'a mut T>`. |
| 203 | pub struct IterMut<'a, T> { |
| David Tolnay | 30bea1c | 2020-12-31 10:41:42 -0800 | [diff] [blame] | 204 | v: Pin<&'a mut CxxVector<T>>, |
| David Tolnay | 26a5292 | 2020-12-21 17:29:04 -0800 | [diff] [blame] | 205 | index: usize, |
| 206 | } |
| 207 | |
| David Tolnay | 30bea1c | 2020-12-31 10:41:42 -0800 | [diff] [blame] | 208 | impl<'a, T> IntoIterator for Pin<&'a mut CxxVector<T>> |
| David Tolnay | 26a5292 | 2020-12-21 17:29:04 -0800 | [diff] [blame] | 209 | where |
| 210 | T: VectorElement, |
| 211 | { |
| 212 | type Item = Pin<&'a mut T>; |
| 213 | type IntoIter = IterMut<'a, T>; |
| 214 | |
| 215 | fn into_iter(self) -> Self::IntoIter { |
| 216 | self.iter_mut() |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | impl<'a, T> Iterator for IterMut<'a, T> |
| 221 | where |
| 222 | T: VectorElement, |
| 223 | { |
| 224 | type Item = Pin<&'a mut T>; |
| 225 | |
| 226 | fn next(&mut self) -> Option<Self::Item> { |
| David Tolnay | 5b395b3 | 2020-12-31 10:44:26 -0800 | [diff] [blame] | 227 | let next = self.v.as_mut().index_mut(self.index)?; |
| David Tolnay | 26a5292 | 2020-12-21 17:29:04 -0800 | [diff] [blame] | 228 | self.index += 1; |
| 229 | // Extend lifetime to allow simultaneous holding of nonoverlapping |
| 230 | // elements, analogous to slice::split_first_mut. |
| 231 | unsafe { |
| 232 | let ptr = Pin::into_inner_unchecked(next) as *mut T; |
| 233 | Some(Pin::new_unchecked(&mut *ptr)) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 238 | let len = self.len(); |
| 239 | (len, Some(len)) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | impl<'a, T> ExactSizeIterator for IterMut<'a, T> |
| 244 | where |
| 245 | T: VectorElement, |
| 246 | { |
| 247 | fn len(&self) -> usize { |
| 248 | self.v.len() - self.index |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | impl<'a, T> FusedIterator for IterMut<'a, T> where T: VectorElement {} |
| 253 | |
| David Tolnay | a8100ed | 2020-12-04 12:41:24 -0800 | [diff] [blame] | 254 | impl<T> Debug for CxxVector<T> |
| 255 | where |
| 256 | T: VectorElement + Debug, |
| 257 | { |
| 258 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 259 | formatter.debug_list().entries(self).finish() |
| 260 | } |
| 261 | } |
| 262 | |
| David Tolnay | 71b8d38 | 2021-03-25 02:03:14 -0400 | [diff] [blame^] | 263 | /// Trait bound for types which may be used as the `T` inside of a |
| 264 | /// `CxxVector<T>` in generic code. |
| 265 | /// |
| 266 | /// This trait has no publicly callable or implementable methods. Implementing |
| 267 | /// it outside of the CXX codebase is not supported. |
| 268 | /// |
| 269 | /// # Example |
| 270 | /// |
| 271 | /// A bound `T: VectorElement` may be necessary when manipulating [`CxxVector`] |
| 272 | /// in generic code. |
| 273 | /// |
| 274 | /// ``` |
| 275 | /// use cxx::vector::{CxxVector, VectorElement}; |
| 276 | /// use std::fmt::Display; |
| 277 | /// |
| 278 | /// pub fn take_generic_vector<T>(vector: &CxxVector<T>) |
| 279 | /// where |
| 280 | /// T: VectorElement + Display, |
| 281 | /// { |
| 282 | /// println!("the vector elements are:"); |
| 283 | /// for element in vector { |
| 284 | /// println!(" • {}", element); |
| 285 | /// } |
| 286 | /// } |
| 287 | /// ``` |
| 288 | /// |
| 289 | /// Writing the same generic function without a `VectorElement` trait bound |
| 290 | /// would not compile. |
| David Tolnay | c3ed3a6 | 2020-04-24 13:34:50 -0700 | [diff] [blame] | 291 | pub unsafe trait VectorElement: Sized { |
| David Tolnay | ac5af50 | 2021-03-25 00:29:06 -0400 | [diff] [blame] | 292 | fn __typename(f: &mut fmt::Formatter) -> fmt::Result; |
| David Tolnay | 0e08466 | 2020-04-24 14:02:51 -0700 | [diff] [blame] | 293 | fn __vector_size(v: &CxxVector<Self>) -> usize; |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 294 | unsafe fn __get_unchecked(v: *mut CxxVector<Self>, pos: usize) -> *mut Self; |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 295 | fn __unique_ptr_null() -> *mut c_void; |
| 296 | unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void; |
| 297 | unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>; |
| 298 | unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>; |
| 299 | unsafe fn __unique_ptr_drop(repr: *mut c_void); |
| David Tolnay | 1b34119 | 2020-04-24 13:04:04 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| David Tolnay | 47e239d | 2020-08-28 00:32:04 -0700 | [diff] [blame] | 302 | macro_rules! impl_vector_element { |
| 303 | ($segment:expr, $name:expr, $ty:ty) => { |
| David Tolnay | f044663 | 2020-04-25 11:29:26 -0700 | [diff] [blame] | 304 | const_assert_eq!(1, mem::align_of::<CxxVector<$ty>>()); |
| 305 | |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 306 | unsafe impl VectorElement for $ty { |
| David Tolnay | ac5af50 | 2021-03-25 00:29:06 -0400 | [diff] [blame] | 307 | fn __typename(f: &mut fmt::Formatter) -> fmt::Result { |
| 308 | f.write_str($name) |
| 309 | } |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 310 | fn __vector_size(v: &CxxVector<$ty>) -> usize { |
| 311 | extern "C" { |
| 312 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 313 | #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$size")] |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 314 | fn __vector_size(_: &CxxVector<$ty>) -> usize; |
| 315 | } |
| 316 | } |
| 317 | unsafe { __vector_size(v) } |
| 318 | } |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 319 | unsafe fn __get_unchecked(v: *mut CxxVector<$ty>, pos: usize) -> *mut $ty { |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 320 | extern "C" { |
| 321 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 322 | #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$get_unchecked")] |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 323 | fn __get_unchecked(_: *mut CxxVector<$ty>, _: usize) -> *mut $ty; |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| David Tolnay | 93637ca | 2020-09-24 15:58:20 -0400 | [diff] [blame] | 326 | __get_unchecked(v, pos) |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 327 | } |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 328 | fn __unique_ptr_null() -> *mut c_void { |
| 329 | extern "C" { |
| 330 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 331 | #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$null")] |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 332 | fn __unique_ptr_null(this: *mut *mut c_void); |
| 333 | } |
| 334 | } |
| 335 | let mut repr = ptr::null_mut::<c_void>(); |
| 336 | unsafe { __unique_ptr_null(&mut repr) } |
| 337 | repr |
| 338 | } |
| 339 | unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void { |
| 340 | extern "C" { |
| 341 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 342 | #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$raw")] |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 343 | fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>); |
| 344 | } |
| 345 | } |
| 346 | let mut repr = ptr::null_mut::<c_void>(); |
| 347 | __unique_ptr_raw(&mut repr, raw); |
| 348 | repr |
| 349 | } |
| 350 | unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> { |
| 351 | extern "C" { |
| 352 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 353 | #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$get")] |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 354 | fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>; |
| 355 | } |
| 356 | } |
| 357 | __unique_ptr_get(&repr) |
| 358 | } |
| 359 | unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> { |
| 360 | extern "C" { |
| 361 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 362 | #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$release")] |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 363 | fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>; |
| 364 | } |
| 365 | } |
| 366 | __unique_ptr_release(&mut repr) |
| 367 | } |
| 368 | unsafe fn __unique_ptr_drop(mut repr: *mut c_void) { |
| 369 | extern "C" { |
| 370 | attr! { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 371 | #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$drop")] |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 372 | fn __unique_ptr_drop(this: *mut *mut c_void); |
| 373 | } |
| 374 | } |
| 375 | __unique_ptr_drop(&mut repr); |
| 376 | } |
| David Tolnay | e4b6a62 | 2020-04-24 14:55:42 -0700 | [diff] [blame] | 377 | } |
| 378 | }; |
| 379 | } |
| 380 | |
| David Tolnay | 47e239d | 2020-08-28 00:32:04 -0700 | [diff] [blame] | 381 | macro_rules! impl_vector_element_for_primitive { |
| 382 | ($ty:ident) => { |
| 383 | impl_vector_element!(stringify!($ty), stringify!($ty), $ty); |
| 384 | }; |
| 385 | } |
| 386 | |
| David Tolnay | 4b91eaa | 2020-04-24 14:19:22 -0700 | [diff] [blame] | 387 | impl_vector_element_for_primitive!(u8); |
| 388 | impl_vector_element_for_primitive!(u16); |
| 389 | impl_vector_element_for_primitive!(u32); |
| 390 | impl_vector_element_for_primitive!(u64); |
| 391 | impl_vector_element_for_primitive!(usize); |
| 392 | impl_vector_element_for_primitive!(i8); |
| 393 | impl_vector_element_for_primitive!(i16); |
| 394 | impl_vector_element_for_primitive!(i32); |
| 395 | impl_vector_element_for_primitive!(i64); |
| 396 | impl_vector_element_for_primitive!(isize); |
| 397 | impl_vector_element_for_primitive!(f32); |
| 398 | impl_vector_element_for_primitive!(f64); |
| David Tolnay | 47e239d | 2020-08-28 00:32:04 -0700 | [diff] [blame] | 399 | |
| 400 | impl_vector_element!("string", "CxxString", CxxString); |