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