blob: 23cbcc1dbb566858fa85b45edd7c79ce5487338d [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 Tolnay95215192021-04-16 15:40:12 -070011use core::mem::{self, ManuallyDrop, MaybeUninit};
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> {
David Tolnay11eed382021-05-01 14:22:58 -070026 // A thing, because repr(C) structs are not allowed to consist exclusively
27 // of PhantomData fields.
28 _void: [c_void; 0],
29 // The conceptual vector elements to ensure that autotraits are propagated
30 // correctly, e.g. CxxVector is UnwindSafe iff T is.
31 _elements: PhantomData<[T]>,
32 // Prevent unpin operation from Pin<&mut CxxVector<T>> to &mut CxxVector<T>.
David Tolnay95dab1d2020-11-15 14:32:37 -080033 _pinned: PhantomData<PhantomPinned>,
Myron Ahneba35cf2020-02-05 19:41:51 +070034}
35
David Tolnay4074ad22020-04-24 18:20:11 -070036impl<T> CxxVector<T>
37where
38 T: VectorElement,
39{
David Tolnaycdc87962020-04-24 13:45:59 -070040 /// Returns the number of elements in the vector.
David Tolnaydd839192020-04-24 16:41:29 -070041 ///
42 /// Matches the behavior of C++ [std::vector\<T\>::size][size].
43 ///
44 /// [size]: https://en.cppreference.com/w/cpp/container/vector/size
David Tolnayc01d0a02020-04-24 13:30:44 -070045 pub fn len(&self) -> usize {
David Tolnay0e084662020-04-24 14:02:51 -070046 T::__vector_size(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070047 }
48
David Tolnaycdc87962020-04-24 13:45:59 -070049 /// Returns true if the vector contains no elements.
David Tolnaydd839192020-04-24 16:41:29 -070050 ///
51 /// Matches the behavior of C++ [std::vector\<T\>::empty][empty].
52 ///
53 /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty
Myron Ahneba35cf2020-02-05 19:41:51 +070054 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070055 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070056 }
57
David Tolnaycdc87962020-04-24 13:45:59 -070058 /// Returns a reference to an element at the given position, or `None` if
59 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070060 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070061 if pos < self.len() {
David Tolnay93637ca2020-09-24 15:58:20 -040062 Some(unsafe { self.get_unchecked(pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070063 } else {
64 None
65 }
66 }
67
David Tolnay767e00d2020-12-21 17:12:27 -080068 /// Returns a pinned mutable reference to an element at the given position,
69 /// or `None` if out of bounds.
David Tolnay5b395b32020-12-31 10:44:26 -080070 pub fn index_mut(self: Pin<&mut Self>, pos: usize) -> Option<Pin<&mut T>> {
David Tolnay767e00d2020-12-21 17:12:27 -080071 if pos < self.len() {
David Tolnay5b395b32020-12-31 10:44:26 -080072 Some(unsafe { self.index_unchecked_mut(pos) })
David Tolnay767e00d2020-12-21 17:12:27 -080073 } else {
74 None
75 }
76 }
77
David Tolnay4944f2f2020-04-24 13:46:12 -070078 /// Returns a reference to an element without doing bounds checking.
79 ///
80 /// This is generally not recommended, use with caution! Calling this method
81 /// with an out-of-bounds index is undefined behavior even if the resulting
82 /// reference is not used.
David Tolnaydd839192020-04-24 16:41:29 -070083 ///
84 /// Matches the behavior of C++
David Tolnay767e00d2020-12-21 17:12:27 -080085 /// [std::vector\<T\>::operator\[\] const][operator_at].
86 ///
87 /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
88 pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
89 let this = self as *const CxxVector<T> as *mut CxxVector<T>;
90 let ptr = T::__get_unchecked(this, pos) as *const T;
91 &*ptr
92 }
93
94 /// Returns a pinned mutable reference to an element without doing bounds
95 /// checking.
96 ///
97 /// This is generally not recommended, use with caution! Calling this method
98 /// with an out-of-bounds index is undefined behavior even if the resulting
99 /// reference is not used.
100 ///
101 /// Matches the behavior of C++
David Tolnaydd839192020-04-24 16:41:29 -0700102 /// [std::vector\<T\>::operator\[\]][operator_at].
103 ///
104 /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
David Tolnay5b395b32020-12-31 10:44:26 -0800105 pub unsafe fn index_unchecked_mut(self: Pin<&mut Self>, pos: usize) -> Pin<&mut T> {
106 let ptr = T::__get_unchecked(self.get_unchecked_mut(), pos);
David Tolnay767e00d2020-12-21 17:12:27 -0800107 Pin::new_unchecked(&mut *ptr)
David Tolnay93637ca2020-09-24 15:58:20 -0400108 }
109
110 /// Returns a slice to the underlying contiguous array of elements.
David Tolnay181ee912020-12-04 12:15:10 -0800111 pub fn as_slice(&self) -> &[T]
112 where
113 T: ExternType<Kind = Trivial>,
114 {
David Tolnay93637ca2020-09-24 15:58:20 -0400115 let len = self.len();
116 if len == 0 {
David Tolnaya5a14ce2020-09-24 16:02:40 -0400117 // The slice::from_raw_parts in the other branch requires a nonnull
118 // and properly aligned data ptr. C++ standard does not guarantee
119 // that data() on a vector with size 0 would return a nonnull
120 // pointer or sufficiently aligned pointer, so using it would be
121 // undefined behavior. Create our own empty slice in Rust instead
122 // which upholds the invariants.
David Tolnayacc7fb02020-09-24 18:10:09 -0400123 &[]
David Tolnay93637ca2020-09-24 15:58:20 -0400124 } else {
David Tolnay767e00d2020-12-21 17:12:27 -0800125 let this = self as *const CxxVector<T> as *mut CxxVector<T>;
126 let ptr = unsafe { T::__get_unchecked(this, 0) };
David Tolnay93637ca2020-09-24 15:58:20 -0400127 unsafe { slice::from_raw_parts(ptr, len) }
128 }
David Tolnay4944f2f2020-04-24 13:46:12 -0700129 }
David Tolnay4f71cc52020-11-15 23:55:27 -0800130
David Tolnayab1ac882020-12-31 11:54:37 -0800131 /// Returns a slice to the underlying contiguous array of elements by
132 /// mutable reference.
133 pub fn as_mut_slice(self: Pin<&mut Self>) -> &mut [T]
134 where
135 T: ExternType<Kind = Trivial>,
136 {
137 let len = self.len();
138 if len == 0 {
139 &mut []
140 } else {
141 let ptr = unsafe { T::__get_unchecked(self.get_unchecked_mut(), 0) };
142 unsafe { slice::from_raw_parts_mut(ptr, len) }
143 }
144 }
145
David Tolnay4f71cc52020-11-15 23:55:27 -0800146 /// Returns an iterator over elements of type `&T`.
147 pub fn iter(&self) -> Iter<T> {
148 Iter { v: self, index: 0 }
149 }
David Tolnay26a52922020-12-21 17:29:04 -0800150
151 /// Returns an iterator over elements of type `Pin<&mut T>`.
David Tolnay30bea1c2020-12-31 10:41:42 -0800152 pub fn iter_mut(self: Pin<&mut Self>) -> IterMut<T> {
David Tolnay26a52922020-12-21 17:29:04 -0800153 IterMut { v: self, index: 0 }
154 }
David Tolnayfc26d6d2021-04-15 21:18:45 -0700155
156 /// Appends an element to the back of the vector.
157 ///
158 /// Matches the behavior of C++ [std::vector\<T\>::push_back][push_back].
159 ///
160 /// [push_back]: https://en.cppreference.com/w/cpp/container/vector/push_back
161 pub fn push(self: Pin<&mut Self>, value: T)
162 where
163 T: ExternType<Kind = Trivial>,
164 {
165 let mut value = ManuallyDrop::new(value);
166 unsafe {
167 // C++ calls move constructor followed by destructor on `value`.
168 T::__push_back(self, &mut value);
169 }
170 }
David Tolnay95215192021-04-16 15:40:12 -0700171
David Tolnay9b546302021-04-16 16:05:05 -0700172 /// Removes the last element from a vector and returns it, or `None` if the
173 /// vector is empty.
David Tolnay95215192021-04-16 15:40:12 -0700174 pub fn pop(self: Pin<&mut Self>) -> Option<T>
175 where
176 T: ExternType<Kind = Trivial>,
177 {
178 if self.is_empty() {
179 None
180 } else {
181 let mut value = MaybeUninit::uninit();
182 Some(unsafe {
183 T::__pop_back(self, &mut value);
184 value.assume_init()
185 })
186 }
187 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700188}
189
David Tolnayb5d039c2020-12-12 23:21:17 -0800190/// Iterator over elements of a `CxxVector` by shared reference.
191///
192/// The iterator element type is `&'a T`.
David Tolnay3d88bdc2020-04-24 13:48:18 -0700193pub struct Iter<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -0700194 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +0700195 index: usize,
196}
197
David Tolnay4074ad22020-04-24 18:20:11 -0700198impl<'a, T> IntoIterator for &'a CxxVector<T>
199where
200 T: VectorElement,
201{
Myron Ahneba35cf2020-02-05 19:41:51 +0700202 type Item = &'a T;
David Tolnay3d88bdc2020-04-24 13:48:18 -0700203 type IntoIter = Iter<'a, T>;
Myron Ahneba35cf2020-02-05 19:41:51 +0700204
205 fn into_iter(self) -> Self::IntoIter {
David Tolnay4f71cc52020-11-15 23:55:27 -0800206 self.iter()
Myron Ahneba35cf2020-02-05 19:41:51 +0700207 }
208}
209
David Tolnay4074ad22020-04-24 18:20:11 -0700210impl<'a, T> Iterator for Iter<'a, T>
211where
212 T: VectorElement,
213{
Myron Ahneba35cf2020-02-05 19:41:51 +0700214 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -0700215
Myron Ahneba35cf2020-02-05 19:41:51 +0700216 fn next(&mut self) -> Option<Self::Item> {
David Tolnay0d527172020-12-21 17:35:24 -0800217 let next = self.v.get(self.index)?;
218 self.index += 1;
219 Some(next)
Myron Ahneba35cf2020-02-05 19:41:51 +0700220 }
David Tolnay724ac752020-12-13 16:00:48 -0800221
222 fn size_hint(&self) -> (usize, Option<usize>) {
223 let len = self.len();
224 (len, Some(len))
225 }
226}
227
228impl<'a, T> ExactSizeIterator for Iter<'a, T>
229where
230 T: VectorElement,
231{
232 fn len(&self) -> usize {
233 self.v.len() - self.index
234 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700235}
236
David Tolnay526faa22020-12-13 16:10:47 -0800237impl<'a, T> FusedIterator for Iter<'a, T> where T: VectorElement {}
238
David Tolnay26a52922020-12-21 17:29:04 -0800239/// Iterator over elements of a `CxxVector` by pinned mutable reference.
240///
241/// The iterator element type is `Pin<&'a mut T>`.
242pub struct IterMut<'a, T> {
David Tolnay30bea1c2020-12-31 10:41:42 -0800243 v: Pin<&'a mut CxxVector<T>>,
David Tolnay26a52922020-12-21 17:29:04 -0800244 index: usize,
245}
246
David Tolnay30bea1c2020-12-31 10:41:42 -0800247impl<'a, T> IntoIterator for Pin<&'a mut CxxVector<T>>
David Tolnay26a52922020-12-21 17:29:04 -0800248where
249 T: VectorElement,
250{
251 type Item = Pin<&'a mut T>;
252 type IntoIter = IterMut<'a, T>;
253
254 fn into_iter(self) -> Self::IntoIter {
255 self.iter_mut()
256 }
257}
258
259impl<'a, T> Iterator for IterMut<'a, T>
260where
261 T: VectorElement,
262{
263 type Item = Pin<&'a mut T>;
264
265 fn next(&mut self) -> Option<Self::Item> {
David Tolnay5b395b32020-12-31 10:44:26 -0800266 let next = self.v.as_mut().index_mut(self.index)?;
David Tolnay26a52922020-12-21 17:29:04 -0800267 self.index += 1;
268 // Extend lifetime to allow simultaneous holding of nonoverlapping
269 // elements, analogous to slice::split_first_mut.
270 unsafe {
271 let ptr = Pin::into_inner_unchecked(next) as *mut T;
272 Some(Pin::new_unchecked(&mut *ptr))
273 }
274 }
275
276 fn size_hint(&self) -> (usize, Option<usize>) {
277 let len = self.len();
278 (len, Some(len))
279 }
280}
281
282impl<'a, T> ExactSizeIterator for IterMut<'a, T>
283where
284 T: VectorElement,
285{
286 fn len(&self) -> usize {
287 self.v.len() - self.index
288 }
289}
290
291impl<'a, T> FusedIterator for IterMut<'a, T> where T: VectorElement {}
292
David Tolnaya8100ed2020-12-04 12:41:24 -0800293impl<T> Debug for CxxVector<T>
294where
295 T: VectorElement + Debug,
296{
297 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
298 formatter.debug_list().entries(self).finish()
299 }
300}
301
David Tolnay71b8d382021-03-25 02:03:14 -0400302/// Trait bound for types which may be used as the `T` inside of a
303/// `CxxVector<T>` in generic code.
304///
305/// This trait has no publicly callable or implementable methods. Implementing
306/// it outside of the CXX codebase is not supported.
307///
308/// # Example
309///
310/// A bound `T: VectorElement` may be necessary when manipulating [`CxxVector`]
311/// in generic code.
312///
313/// ```
314/// use cxx::vector::{CxxVector, VectorElement};
315/// use std::fmt::Display;
316///
317/// pub fn take_generic_vector<T>(vector: &CxxVector<T>)
318/// where
319/// T: VectorElement + Display,
320/// {
321/// println!("the vector elements are:");
322/// for element in vector {
323/// println!(" • {}", element);
324/// }
325/// }
326/// ```
327///
328/// Writing the same generic function without a `VectorElement` trait bound
329/// would not compile.
David Tolnayc3ed3a62020-04-24 13:34:50 -0700330pub unsafe trait VectorElement: Sized {
David Tolnayb99359b2021-03-25 02:05:20 -0400331 #[doc(hidden)]
David Tolnayac5af502021-03-25 00:29:06 -0400332 fn __typename(f: &mut fmt::Formatter) -> fmt::Result;
David Tolnayb99359b2021-03-25 02:05:20 -0400333 #[doc(hidden)]
David Tolnay0e084662020-04-24 14:02:51 -0700334 fn __vector_size(v: &CxxVector<Self>) -> usize;
David Tolnayb99359b2021-03-25 02:05:20 -0400335 #[doc(hidden)]
David Tolnay767e00d2020-12-21 17:12:27 -0800336 unsafe fn __get_unchecked(v: *mut CxxVector<Self>, pos: usize) -> *mut Self;
David Tolnayb99359b2021-03-25 02:05:20 -0400337 #[doc(hidden)]
David Tolnayfc26d6d2021-04-15 21:18:45 -0700338 unsafe fn __push_back(v: Pin<&mut CxxVector<Self>>, value: &mut ManuallyDrop<Self>) {
339 // Opaque C type vector elements do not get this method because they can
340 // never exist by value on the Rust side of the bridge.
341 let _ = v;
342 let _ = value;
343 unreachable!()
344 }
345 #[doc(hidden)]
David Tolnay95215192021-04-16 15:40:12 -0700346 unsafe fn __pop_back(v: Pin<&mut CxxVector<Self>>, out: &mut MaybeUninit<Self>) {
347 // Opaque C type vector elements do not get this method because they can
348 // never exist by value on the Rust side of the bridge.
349 let _ = v;
350 let _ = out;
351 unreachable!()
352 }
353 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700354 fn __unique_ptr_null() -> *mut c_void;
David Tolnayb99359b2021-03-25 02:05:20 -0400355 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700356 unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void;
David Tolnayb99359b2021-03-25 02:05:20 -0400357 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700358 unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>;
David Tolnayb99359b2021-03-25 02:05:20 -0400359 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700360 unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>;
David Tolnayb99359b2021-03-25 02:05:20 -0400361 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700362 unsafe fn __unique_ptr_drop(repr: *mut c_void);
David Tolnay1b341192020-04-24 13:04:04 -0700363}
364
David Tolnay95215192021-04-16 15:40:12 -0700365macro_rules! vector_element_by_value_methods {
David Tolnayfc26d6d2021-04-15 21:18:45 -0700366 (opaque, $segment:expr, $ty:ty) => {};
367 (trivial, $segment:expr, $ty:ty) => {
368 #[doc(hidden)]
369 unsafe fn __push_back(v: Pin<&mut CxxVector<$ty>>, value: &mut ManuallyDrop<$ty>) {
370 extern "C" {
371 attr! {
372 #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$push_back")]
373 fn __push_back(_: Pin<&mut CxxVector<$ty>>, _: &mut ManuallyDrop<$ty>);
374 }
375 }
376 __push_back(v, value);
377 }
David Tolnay95215192021-04-16 15:40:12 -0700378 #[doc(hidden)]
379 unsafe fn __pop_back(v: Pin<&mut CxxVector<$ty>>, out: &mut MaybeUninit<$ty>) {
380 extern "C" {
381 attr! {
382 #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$pop_back")]
383 fn __pop_back(_: Pin<&mut CxxVector<$ty>>, _: &mut MaybeUninit<$ty>);
384 }
385 }
386 __pop_back(v, out);
387 }
David Tolnayfc26d6d2021-04-15 21:18:45 -0700388 };
389}
390
David Tolnay47e239d2020-08-28 00:32:04 -0700391macro_rules! impl_vector_element {
David Tolnayfc26d6d2021-04-15 21:18:45 -0700392 ($kind:ident, $segment:expr, $name:expr, $ty:ty) => {
David Tolnay9f0e67d2021-05-01 14:09:39 -0700393 const_assert_eq!(0, mem::size_of::<CxxVector<$ty>>());
David Tolnayf0446632020-04-25 11:29:26 -0700394 const_assert_eq!(1, mem::align_of::<CxxVector<$ty>>());
395
David Tolnaye4b6a622020-04-24 14:55:42 -0700396 unsafe impl VectorElement for $ty {
David Tolnayb99359b2021-03-25 02:05:20 -0400397 #[doc(hidden)]
David Tolnayac5af502021-03-25 00:29:06 -0400398 fn __typename(f: &mut fmt::Formatter) -> fmt::Result {
399 f.write_str($name)
400 }
David Tolnayb99359b2021-03-25 02:05:20 -0400401 #[doc(hidden)]
David Tolnaye4b6a622020-04-24 14:55:42 -0700402 fn __vector_size(v: &CxxVector<$ty>) -> usize {
403 extern "C" {
404 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800405 #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$size")]
David Tolnaye4b6a622020-04-24 14:55:42 -0700406 fn __vector_size(_: &CxxVector<$ty>) -> usize;
407 }
408 }
409 unsafe { __vector_size(v) }
410 }
David Tolnayb99359b2021-03-25 02:05:20 -0400411 #[doc(hidden)]
David Tolnay767e00d2020-12-21 17:12:27 -0800412 unsafe fn __get_unchecked(v: *mut CxxVector<$ty>, pos: usize) -> *mut $ty {
David Tolnaye4b6a622020-04-24 14:55:42 -0700413 extern "C" {
414 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800415 #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$get_unchecked")]
David Tolnay767e00d2020-12-21 17:12:27 -0800416 fn __get_unchecked(_: *mut CxxVector<$ty>, _: usize) -> *mut $ty;
David Tolnaye4b6a622020-04-24 14:55:42 -0700417 }
418 }
David Tolnay93637ca2020-09-24 15:58:20 -0400419 __get_unchecked(v, pos)
David Tolnaye4b6a622020-04-24 14:55:42 -0700420 }
David Tolnay95215192021-04-16 15:40:12 -0700421 vector_element_by_value_methods!($kind, $segment, $ty);
David Tolnayb99359b2021-03-25 02:05:20 -0400422 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700423 fn __unique_ptr_null() -> *mut c_void {
424 extern "C" {
425 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800426 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$null")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700427 fn __unique_ptr_null(this: *mut *mut c_void);
428 }
429 }
430 let mut repr = ptr::null_mut::<c_void>();
431 unsafe { __unique_ptr_null(&mut repr) }
432 repr
433 }
David Tolnayb99359b2021-03-25 02:05:20 -0400434 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700435 unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void {
436 extern "C" {
437 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800438 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$raw")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700439 fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>);
440 }
441 }
442 let mut repr = ptr::null_mut::<c_void>();
443 __unique_ptr_raw(&mut repr, raw);
444 repr
445 }
David Tolnayb99359b2021-03-25 02:05:20 -0400446 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700447 unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> {
448 extern "C" {
449 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800450 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$get")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700451 fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>;
452 }
453 }
454 __unique_ptr_get(&repr)
455 }
David Tolnayb99359b2021-03-25 02:05:20 -0400456 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700457 unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> {
458 extern "C" {
459 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800460 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$release")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700461 fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>;
462 }
463 }
464 __unique_ptr_release(&mut repr)
465 }
David Tolnayb99359b2021-03-25 02:05:20 -0400466 #[doc(hidden)]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700467 unsafe fn __unique_ptr_drop(mut repr: *mut c_void) {
468 extern "C" {
469 attr! {
David Tolnay0f0162f2020-11-16 23:43:37 -0800470 #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$drop")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700471 fn __unique_ptr_drop(this: *mut *mut c_void);
472 }
473 }
474 __unique_ptr_drop(&mut repr);
475 }
David Tolnaye4b6a622020-04-24 14:55:42 -0700476 }
477 };
478}
479
David Tolnay47e239d2020-08-28 00:32:04 -0700480macro_rules! impl_vector_element_for_primitive {
481 ($ty:ident) => {
David Tolnayfc26d6d2021-04-15 21:18:45 -0700482 impl_vector_element!(trivial, stringify!($ty), stringify!($ty), $ty);
David Tolnay47e239d2020-08-28 00:32:04 -0700483 };
484}
485
David Tolnay4b91eaa2020-04-24 14:19:22 -0700486impl_vector_element_for_primitive!(u8);
487impl_vector_element_for_primitive!(u16);
488impl_vector_element_for_primitive!(u32);
489impl_vector_element_for_primitive!(u64);
490impl_vector_element_for_primitive!(usize);
491impl_vector_element_for_primitive!(i8);
492impl_vector_element_for_primitive!(i16);
493impl_vector_element_for_primitive!(i32);
494impl_vector_element_for_primitive!(i64);
495impl_vector_element_for_primitive!(isize);
496impl_vector_element_for_primitive!(f32);
497impl_vector_element_for_primitive!(f64);
David Tolnay47e239d2020-08-28 00:32:04 -0700498
David Tolnayfc26d6d2021-04-15 21:18:45 -0700499impl_vector_element!(opaque, "string", "CxxString", CxxString);