blob: 23f7621df2eaf726ec0d393ffc9424cc392a32ee [file] [log] [blame]
David Tolnayb5d039c2020-12-12 23:21:17 -08001//! Less used details of `CxxVector` are exposed in this module. `CxxVector`
2//! itself is exposed at the crate root.
3
David Tolnay181ee912020-12-04 12:15:10 -08004use crate::extern_type::ExternType;
5use crate::kind::Trivial;
David Tolnaybac25822020-12-12 23:13:51 -08006use crate::string::CxxString;
David Tolnay3384c142020-09-14 00:26:47 -04007use core::ffi::c_void;
David Tolnayac5af502021-03-25 00:29:06 -04008use core::fmt::{self, Debug};
David Tolnay526faa22020-12-13 16:10:47 -08009use core::iter::FusedIterator;
David Tolnay95dab1d2020-11-15 14:32:37 -080010use core::marker::{PhantomData, PhantomPinned};
David Tolnayfc26d6d2021-04-15 21:18:45 -070011use core::mem::{self, ManuallyDrop};
David Tolnay767e00d2020-12-21 17:12:27 -080012use core::pin::Pin;
David Tolnay3384c142020-09-14 00:26:47 -040013use core::ptr;
David Tolnay93637ca2020-09-24 15:58:20 -040014use core::slice;
David Tolnay4f7e6fa2020-04-24 11:52:44 -070015
David Tolnay61a9fdf2020-04-24 16:19:42 -070016/// Binding to C++ `std::vector<T, std::allocator<T>>`.
Myron Ahneba35cf2020-02-05 19:41:51 +070017///
18/// # Invariants
19///
20/// As an invariant of this API and the static analysis of the cxx::bridge
David Tolnay5fe93632020-04-24 12:31:00 -070021/// 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 Tolnay4f7e6fa2020-04-24 11:52:44 -070024#[repr(C, packed)]
David Tolnaye90be1d2020-04-24 11:45:57 -070025pub struct CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070026 _private: [T; 0],
David Tolnay95dab1d2020-11-15 14:32:37 -080027 _pinned: PhantomData<PhantomPinned>,
Myron Ahneba35cf2020-02-05 19:41:51 +070028}
29
David Tolnay4074ad22020-04-24 18:20:11 -070030impl<T> CxxVector<T>
31where
32 T: VectorElement,
33{
David Tolnaycdc87962020-04-24 13:45:59 -070034 /// Returns the number of elements in the vector.
David Tolnaydd839192020-04-24 16:41:29 -070035 ///
36 /// Matches the behavior of C++ [std::vector\<T\>::size][size].
37 ///
38 /// [size]: https://en.cppreference.com/w/cpp/container/vector/size
David Tolnayc01d0a02020-04-24 13:30:44 -070039 pub fn len(&self) -> usize {
David Tolnay0e084662020-04-24 14:02:51 -070040 T::__vector_size(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070041 }
42
David Tolnaycdc87962020-04-24 13:45:59 -070043 /// Returns true if the vector contains no elements.
David Tolnaydd839192020-04-24 16:41:29 -070044 ///
45 /// Matches the behavior of C++ [std::vector\<T\>::empty][empty].
46 ///
47 /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty
Myron Ahneba35cf2020-02-05 19:41:51 +070048 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070049 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070050 }
51
David Tolnaycdc87962020-04-24 13:45:59 -070052 /// Returns a reference to an element at the given position, or `None` if
53 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070054 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070055 if pos < self.len() {
David Tolnay93637ca2020-09-24 15:58:20 -040056 Some(unsafe { self.get_unchecked(pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070057 } else {
58 None
59 }
60 }
61
David Tolnay767e00d2020-12-21 17:12:27 -080062 /// Returns a pinned mutable reference to an element at the given position,
63 /// or `None` if out of bounds.
David Tolnay5b395b32020-12-31 10:44:26 -080064 pub fn index_mut(self: Pin<&mut Self>, pos: usize) -> Option<Pin<&mut T>> {
David Tolnay767e00d2020-12-21 17:12:27 -080065 if pos < self.len() {
David Tolnay5b395b32020-12-31 10:44:26 -080066 Some(unsafe { self.index_unchecked_mut(pos) })
David Tolnay767e00d2020-12-21 17:12:27 -080067 } else {
68 None
69 }
70 }
71
David Tolnay4944f2f2020-04-24 13:46:12 -070072 /// 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 Tolnaydd839192020-04-24 16:41:29 -070077 ///
78 /// Matches the behavior of C++
David Tolnay767e00d2020-12-21 17:12:27 -080079 /// [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 Tolnaydd839192020-04-24 16:41:29 -070096 /// [std::vector\<T\>::operator\[\]][operator_at].
97 ///
98 /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
David Tolnay5b395b32020-12-31 10:44:26 -080099 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 Tolnay767e00d2020-12-21 17:12:27 -0800101 Pin::new_unchecked(&mut *ptr)
David Tolnay93637ca2020-09-24 15:58:20 -0400102 }
103
104 /// Returns a slice to the underlying contiguous array of elements.
David Tolnay181ee912020-12-04 12:15:10 -0800105 pub fn as_slice(&self) -> &[T]
106 where
107 T: ExternType<Kind = Trivial>,
108 {
David Tolnay93637ca2020-09-24 15:58:20 -0400109 let len = self.len();
110 if len == 0 {
David Tolnaya5a14ce2020-09-24 16:02:40 -0400111 // 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 Tolnayacc7fb02020-09-24 18:10:09 -0400117 &[]
David Tolnay93637ca2020-09-24 15:58:20 -0400118 } else {
David Tolnay767e00d2020-12-21 17:12:27 -0800119 let this = self as *const CxxVector<T> as *mut CxxVector<T>;
120 let ptr = unsafe { T::__get_unchecked(this, 0) };
David Tolnay93637ca2020-09-24 15:58:20 -0400121 unsafe { slice::from_raw_parts(ptr, len) }
122 }
David Tolnay4944f2f2020-04-24 13:46:12 -0700123 }
David Tolnay4f71cc52020-11-15 23:55:27 -0800124
David Tolnayab1ac882020-12-31 11:54:37 -0800125 /// 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 Tolnay4f71cc52020-11-15 23:55:27 -0800140 /// Returns an iterator over elements of type `&T`.
141 pub fn iter(&self) -> Iter<T> {
142 Iter { v: self, index: 0 }
143 }
David Tolnay26a52922020-12-21 17:29:04 -0800144
145 /// Returns an iterator over elements of type `Pin<&mut T>`.
David Tolnay30bea1c2020-12-31 10:41:42 -0800146 pub fn iter_mut(self: Pin<&mut Self>) -> IterMut<T> {
David Tolnay26a52922020-12-21 17:29:04 -0800147 IterMut { v: self, index: 0 }
148 }
David Tolnayfc26d6d2021-04-15 21:18:45 -0700149
150 /// Appends an element to the back of the vector.
151 ///
152 /// Matches the behavior of C++ [std::vector\<T\>::push_back][push_back].
153 ///
154 /// [push_back]: https://en.cppreference.com/w/cpp/container/vector/push_back
155 pub fn push(self: Pin<&mut Self>, value: T)
156 where
157 T: ExternType<Kind = Trivial>,
158 {
159 let mut value = ManuallyDrop::new(value);
160 unsafe {
161 // C++ calls move constructor followed by destructor on `value`.
162 T::__push_back(self, &mut value);
163 }
164 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700165}
166
David Tolnayb5d039c2020-12-12 23:21:17 -0800167/// Iterator over elements of a `CxxVector` by shared reference.
168///
169/// The iterator element type is `&'a T`.
David Tolnay3d88bdc2020-04-24 13:48:18 -0700170pub struct Iter<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -0700171 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +0700172 index: usize,
173}
174
David Tolnay4074ad22020-04-24 18:20:11 -0700175impl<'a, T> IntoIterator for &'a CxxVector<T>
176where
177 T: VectorElement,
178{
Myron Ahneba35cf2020-02-05 19:41:51 +0700179 type Item = &'a T;
David Tolnay3d88bdc2020-04-24 13:48:18 -0700180 type IntoIter = Iter<'a, T>;
Myron Ahneba35cf2020-02-05 19:41:51 +0700181
182 fn into_iter(self) -> Self::IntoIter {
David Tolnay4f71cc52020-11-15 23:55:27 -0800183 self.iter()
Myron Ahneba35cf2020-02-05 19:41:51 +0700184 }
185}
186
David Tolnay4074ad22020-04-24 18:20:11 -0700187impl<'a, T> Iterator for Iter<'a, T>
188where
189 T: VectorElement,
190{
Myron Ahneba35cf2020-02-05 19:41:51 +0700191 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -0700192
Myron Ahneba35cf2020-02-05 19:41:51 +0700193 fn next(&mut self) -> Option<Self::Item> {
David Tolnay0d527172020-12-21 17:35:24 -0800194 let next = self.v.get(self.index)?;
195 self.index += 1;
196 Some(next)
Myron Ahneba35cf2020-02-05 19:41:51 +0700197 }
David Tolnay724ac752020-12-13 16:00:48 -0800198
199 fn size_hint(&self) -> (usize, Option<usize>) {
200 let len = self.len();
201 (len, Some(len))
202 }
203}
204
205impl<'a, T> ExactSizeIterator for Iter<'a, T>
206where
207 T: VectorElement,
208{
209 fn len(&self) -> usize {
210 self.v.len() - self.index
211 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700212}
213
David Tolnay526faa22020-12-13 16:10:47 -0800214impl<'a, T> FusedIterator for Iter<'a, T> where T: VectorElement {}
215
David Tolnay26a52922020-12-21 17:29:04 -0800216/// Iterator over elements of a `CxxVector` by pinned mutable reference.
217///
218/// The iterator element type is `Pin<&'a mut T>`.
219pub struct IterMut<'a, T> {
David Tolnay30bea1c2020-12-31 10:41:42 -0800220 v: Pin<&'a mut CxxVector<T>>,
David Tolnay26a52922020-12-21 17:29:04 -0800221 index: usize,
222}
223
David Tolnay30bea1c2020-12-31 10:41:42 -0800224impl<'a, T> IntoIterator for Pin<&'a mut CxxVector<T>>
David Tolnay26a52922020-12-21 17:29:04 -0800225where
226 T: VectorElement,
227{
228 type Item = Pin<&'a mut T>;
229 type IntoIter = IterMut<'a, T>;
230
231 fn into_iter(self) -> Self::IntoIter {
232 self.iter_mut()
233 }
234}
235
236impl<'a, T> Iterator for IterMut<'a, T>
237where
238 T: VectorElement,
239{
240 type Item = Pin<&'a mut T>;
241
242 fn next(&mut self) -> Option<Self::Item> {
David Tolnay5b395b32020-12-31 10:44:26 -0800243 let next = self.v.as_mut().index_mut(self.index)?;
David Tolnay26a52922020-12-21 17:29:04 -0800244 self.index += 1;
245 // Extend lifetime to allow simultaneous holding of nonoverlapping
246 // elements, analogous to slice::split_first_mut.
247 unsafe {
248 let ptr = Pin::into_inner_unchecked(next) as *mut T;
249 Some(Pin::new_unchecked(&mut *ptr))
250 }
251 }
252
253 fn size_hint(&self) -> (usize, Option<usize>) {
254 let len = self.len();
255 (len, Some(len))
256 }
257}
258
259impl<'a, T> ExactSizeIterator for IterMut<'a, T>
260where
261 T: VectorElement,
262{
263 fn len(&self) -> usize {
264 self.v.len() - self.index
265 }
266}
267
268impl<'a, T> FusedIterator for IterMut<'a, T> where T: VectorElement {}
269
David Tolnaya8100ed2020-12-04 12:41:24 -0800270impl<T> Debug for CxxVector<T>
271where
272 T: VectorElement + Debug,
273{
274 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
275 formatter.debug_list().entries(self).finish()
276 }
277}
278
David Tolnay71b8d382021-03-25 02:03:14 -0400279/// Trait bound for types which may be used as the `T` inside of a
280/// `CxxVector<T>` in generic code.
281///
282/// This trait has no publicly callable or implementable methods. Implementing
283/// it outside of the CXX codebase is not supported.
284///
285/// # Example
286///
287/// A bound `T: VectorElement` may be necessary when manipulating [`CxxVector`]
288/// in generic code.
289///
290/// ```
291/// use cxx::vector::{CxxVector, VectorElement};
292/// use std::fmt::Display;
293///
294/// pub fn take_generic_vector<T>(vector: &CxxVector<T>)
295/// where
296/// T: VectorElement + Display,
297/// {
298/// println!("the vector elements are:");
299/// for element in vector {
300/// println!(" • {}", element);
301/// }
302/// }
303/// ```
304///
305/// Writing the same generic function without a `VectorElement` trait bound
306/// would not compile.
David Tolnayc3ed3a62020-04-24 13:34:50 -0700307pub unsafe trait VectorElement: Sized {
David Tolnayb99359b2021-03-25 02:05:20 -0400308 #[doc(hidden)]
David Tolnayac5af502021-03-25 00:29:06 -0400309 fn __typename(f: &mut fmt::Formatter) -> fmt::Result;
David Tolnayb99359b2021-03-25 02:05:20 -0400310 #[doc(hidden)]
David Tolnay0e084662020-04-24 14:02:51 -0700311 fn __vector_size(v: &CxxVector<Self>) -> usize;
David Tolnayb99359b2021-03-25 02:05:20 -0400312 #[doc(hidden)]
David Tolnay767e00d2020-12-21 17:12:27 -0800313 unsafe fn __get_unchecked(v: *mut CxxVector<Self>, pos: usize) -> *mut Self;
David Tolnayb99359b2021-03-25 02:05:20 -0400314 #[doc(hidden)]
David Tolnayfc26d6d2021-04-15 21:18:45 -0700315 unsafe fn __push_back(v: Pin<&mut CxxVector<Self>>, value: &mut ManuallyDrop<Self>) {
316 // Opaque C type vector elements do not get this method because they can
317 // never exist by value on the Rust side of the bridge.
318 let _ = v;
319 let _ = value;
320 unreachable!()
321 }
322 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700323 fn __unique_ptr_null() -> *mut c_void;
David Tolnayb99359b2021-03-25 02:05:20 -0400324 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700325 unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void;
David Tolnayb99359b2021-03-25 02:05:20 -0400326 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700327 unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>;
David Tolnayb99359b2021-03-25 02:05:20 -0400328 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700329 unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>;
David Tolnayb99359b2021-03-25 02:05:20 -0400330 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700331 unsafe fn __unique_ptr_drop(repr: *mut c_void);
David Tolnay1b341192020-04-24 13:04:04 -0700332}
333
David Tolnayfc26d6d2021-04-15 21:18:45 -0700334macro_rules! vector_element_push_back {
335 (opaque, $segment:expr, $ty:ty) => {};
336 (trivial, $segment:expr, $ty:ty) => {
337 #[doc(hidden)]
338 unsafe fn __push_back(v: Pin<&mut CxxVector<$ty>>, value: &mut ManuallyDrop<$ty>) {
339 extern "C" {
340 attr! {
341 #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$push_back")]
342 fn __push_back(_: Pin<&mut CxxVector<$ty>>, _: &mut ManuallyDrop<$ty>);
343 }
344 }
345 __push_back(v, value);
346 }
347 };
348}
349
David Tolnay47e239d2020-08-28 00:32:04 -0700350macro_rules! impl_vector_element {
David Tolnayfc26d6d2021-04-15 21:18:45 -0700351 ($kind:ident, $segment:expr, $name:expr, $ty:ty) => {
David Tolnayf0446632020-04-25 11:29:26 -0700352 const_assert_eq!(1, mem::align_of::<CxxVector<$ty>>());
353
David Tolnaye4b6a622020-04-24 14:55:42 -0700354 unsafe impl VectorElement for $ty {
David Tolnayb99359b2021-03-25 02:05:20 -0400355 #[doc(hidden)]
David Tolnayac5af502021-03-25 00:29:06 -0400356 fn __typename(f: &mut fmt::Formatter) -> fmt::Result {
357 f.write_str($name)
358 }
David Tolnayb99359b2021-03-25 02:05:20 -0400359 #[doc(hidden)]
David Tolnaye4b6a622020-04-24 14:55:42 -0700360 fn __vector_size(v: &CxxVector<$ty>) -> usize {
361 extern "C" {
362 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800363 #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$size")]
David Tolnaye4b6a622020-04-24 14:55:42 -0700364 fn __vector_size(_: &CxxVector<$ty>) -> usize;
365 }
366 }
367 unsafe { __vector_size(v) }
368 }
David Tolnayb99359b2021-03-25 02:05:20 -0400369 #[doc(hidden)]
David Tolnay767e00d2020-12-21 17:12:27 -0800370 unsafe fn __get_unchecked(v: *mut CxxVector<$ty>, pos: usize) -> *mut $ty {
David Tolnaye4b6a622020-04-24 14:55:42 -0700371 extern "C" {
372 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800373 #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$get_unchecked")]
David Tolnay767e00d2020-12-21 17:12:27 -0800374 fn __get_unchecked(_: *mut CxxVector<$ty>, _: usize) -> *mut $ty;
David Tolnaye4b6a622020-04-24 14:55:42 -0700375 }
376 }
David Tolnay93637ca2020-09-24 15:58:20 -0400377 __get_unchecked(v, pos)
David Tolnaye4b6a622020-04-24 14:55:42 -0700378 }
David Tolnayfc26d6d2021-04-15 21:18:45 -0700379 vector_element_push_back!($kind, $segment, $ty);
David Tolnayb99359b2021-03-25 02:05:20 -0400380 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700381 fn __unique_ptr_null() -> *mut c_void {
382 extern "C" {
383 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800384 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$null")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700385 fn __unique_ptr_null(this: *mut *mut c_void);
386 }
387 }
388 let mut repr = ptr::null_mut::<c_void>();
389 unsafe { __unique_ptr_null(&mut repr) }
390 repr
391 }
David Tolnayb99359b2021-03-25 02:05:20 -0400392 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700393 unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void {
394 extern "C" {
395 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800396 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$raw")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700397 fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>);
398 }
399 }
400 let mut repr = ptr::null_mut::<c_void>();
401 __unique_ptr_raw(&mut repr, raw);
402 repr
403 }
David Tolnayb99359b2021-03-25 02:05:20 -0400404 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700405 unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> {
406 extern "C" {
407 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800408 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$get")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700409 fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>;
410 }
411 }
412 __unique_ptr_get(&repr)
413 }
David Tolnayb99359b2021-03-25 02:05:20 -0400414 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700415 unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> {
416 extern "C" {
417 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800418 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$release")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700419 fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>;
420 }
421 }
422 __unique_ptr_release(&mut repr)
423 }
David Tolnayb99359b2021-03-25 02:05:20 -0400424 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700425 unsafe fn __unique_ptr_drop(mut repr: *mut c_void) {
426 extern "C" {
427 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800428 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$drop")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700429 fn __unique_ptr_drop(this: *mut *mut c_void);
430 }
431 }
432 __unique_ptr_drop(&mut repr);
433 }
David Tolnaye4b6a622020-04-24 14:55:42 -0700434 }
435 };
436}
437
David Tolnay47e239d2020-08-28 00:32:04 -0700438macro_rules! impl_vector_element_for_primitive {
439 ($ty:ident) => {
David Tolnayfc26d6d2021-04-15 21:18:45 -0700440 impl_vector_element!(trivial, stringify!($ty), stringify!($ty), $ty);
David Tolnay47e239d2020-08-28 00:32:04 -0700441 };
442}
443
David Tolnay4b91eaa2020-04-24 14:19:22 -0700444impl_vector_element_for_primitive!(u8);
445impl_vector_element_for_primitive!(u16);
446impl_vector_element_for_primitive!(u32);
447impl_vector_element_for_primitive!(u64);
448impl_vector_element_for_primitive!(usize);
449impl_vector_element_for_primitive!(i8);
450impl_vector_element_for_primitive!(i16);
451impl_vector_element_for_primitive!(i32);
452impl_vector_element_for_primitive!(i64);
453impl_vector_element_for_primitive!(isize);
454impl_vector_element_for_primitive!(f32);
455impl_vector_element_for_primitive!(f64);
David Tolnay47e239d2020-08-28 00:32:04 -0700456
David Tolnayfc26d6d2021-04-15 21:18:45 -0700457impl_vector_element!(opaque, "string", "CxxString", CxxString);