blob: a7d3f2fb5d84a908734aaf715bf4b513c3e6ff5e [file] [log] [blame]
David Tolnay47e239d2020-08-28 00:32:04 -07001use crate::cxx_string::CxxString;
David Tolnay3b40b6f2020-04-24 17:58:24 -07002use std::ffi::c_void;
3use std::fmt::{self, Display};
4use std::marker::PhantomData;
David Tolnay4f7e6fa2020-04-24 11:52:44 -07005use std::mem;
David Tolnay3b40b6f2020-04-24 17:58:24 -07006use std::ptr;
David Tolnay4f7e6fa2020-04-24 11:52:44 -07007
David Tolnay61a9fdf2020-04-24 16:19:42 -07008/// Binding to C++ `std::vector<T, std::allocator<T>>`.
Myron Ahneba35cf2020-02-05 19:41:51 +07009///
10/// # Invariants
11///
12/// As an invariant of this API and the static analysis of the cxx::bridge
David Tolnay5fe93632020-04-24 12:31:00 -070013/// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in
14/// Rust code we will only ever look at a vector behind a reference or smart
15/// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`.
David Tolnay4f7e6fa2020-04-24 11:52:44 -070016#[repr(C, packed)]
David Tolnaye90be1d2020-04-24 11:45:57 -070017pub struct CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070018 _private: [T; 0],
19}
20
David Tolnay4074ad22020-04-24 18:20:11 -070021impl<T> CxxVector<T>
22where
23 T: VectorElement,
24{
David Tolnaycdc87962020-04-24 13:45:59 -070025 /// Returns the number of elements in the vector.
David Tolnaydd839192020-04-24 16:41:29 -070026 ///
27 /// Matches the behavior of C++ [std::vector\<T\>::size][size].
28 ///
29 /// [size]: https://en.cppreference.com/w/cpp/container/vector/size
David Tolnayc01d0a02020-04-24 13:30:44 -070030 pub fn len(&self) -> usize {
David Tolnay0e084662020-04-24 14:02:51 -070031 T::__vector_size(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070032 }
33
David Tolnaycdc87962020-04-24 13:45:59 -070034 /// Returns true if the vector contains no elements.
David Tolnaydd839192020-04-24 16:41:29 -070035 ///
36 /// Matches the behavior of C++ [std::vector\<T\>::empty][empty].
37 ///
38 /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty
Myron Ahneba35cf2020-02-05 19:41:51 +070039 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070040 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070041 }
42
David Tolnaycdc87962020-04-24 13:45:59 -070043 /// Returns a reference to an element at the given position, or `None` if
44 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070045 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070046 if pos < self.len() {
David Tolnay31ad0be2020-04-24 13:29:51 -070047 Some(unsafe { T::__get_unchecked(self, pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070048 } else {
49 None
50 }
51 }
52
David Tolnay4944f2f2020-04-24 13:46:12 -070053 /// Returns a reference to an element without doing bounds checking.
54 ///
55 /// This is generally not recommended, use with caution! Calling this method
56 /// with an out-of-bounds index is undefined behavior even if the resulting
57 /// reference is not used.
David Tolnaydd839192020-04-24 16:41:29 -070058 ///
59 /// Matches the behavior of C++
60 /// [std::vector\<T\>::operator\[\]][operator_at].
61 ///
62 /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
David Tolnay4944f2f2020-04-24 13:46:12 -070063 pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
64 T::__get_unchecked(self, pos)
65 }
Myron Ahneba35cf2020-02-05 19:41:51 +070066}
67
David Tolnay3d88bdc2020-04-24 13:48:18 -070068pub struct Iter<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070069 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070070 index: usize,
71}
72
David Tolnay4074ad22020-04-24 18:20:11 -070073impl<'a, T> IntoIterator for &'a CxxVector<T>
74where
75 T: VectorElement,
76{
Myron Ahneba35cf2020-02-05 19:41:51 +070077 type Item = &'a T;
David Tolnay3d88bdc2020-04-24 13:48:18 -070078 type IntoIter = Iter<'a, T>;
Myron Ahneba35cf2020-02-05 19:41:51 +070079
80 fn into_iter(self) -> Self::IntoIter {
David Tolnay3d88bdc2020-04-24 13:48:18 -070081 Iter { v: self, index: 0 }
Myron Ahneba35cf2020-02-05 19:41:51 +070082 }
83}
84
David Tolnay4074ad22020-04-24 18:20:11 -070085impl<'a, T> Iterator for Iter<'a, T>
86where
87 T: VectorElement,
88{
Myron Ahneba35cf2020-02-05 19:41:51 +070089 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070090
Myron Ahneba35cf2020-02-05 19:41:51 +070091 fn next(&mut self) -> Option<Self::Item> {
David Tolnay39ee0ed2020-05-05 10:12:29 -070092 let next = self.v.get(self.index);
93 self.index += 1;
94 next
Myron Ahneba35cf2020-02-05 19:41:51 +070095 }
96}
97
David Tolnay3b40b6f2020-04-24 17:58:24 -070098pub struct TypeName<T> {
99 element: PhantomData<T>,
100}
101
102impl<T> TypeName<T> {
103 pub const fn new() -> Self {
104 TypeName {
105 element: PhantomData,
106 }
107 }
108}
109
110impl<T> Display for TypeName<T>
111where
112 T: VectorElement,
113{
114 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
115 write!(formatter, "CxxVector<{}>", T::__NAME)
116 }
117}
118
David Tolnay5104c862020-04-24 13:26:01 -0700119// Methods are private; not intended to be implemented outside of cxxbridge
120// codebase.
David Tolnay1b341192020-04-24 13:04:04 -0700121#[doc(hidden)]
David Tolnayc3ed3a62020-04-24 13:34:50 -0700122pub unsafe trait VectorElement: Sized {
David Tolnay3b40b6f2020-04-24 17:58:24 -0700123 const __NAME: &'static dyn Display;
David Tolnay0e084662020-04-24 14:02:51 -0700124 fn __vector_size(v: &CxxVector<Self>) -> usize;
David Tolnaycc75ad22020-04-24 14:45:16 -0700125 unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self;
David Tolnay3b40b6f2020-04-24 17:58:24 -0700126 fn __unique_ptr_null() -> *mut c_void;
127 unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void;
128 unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>;
129 unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>;
130 unsafe fn __unique_ptr_drop(repr: *mut c_void);
David Tolnay1b341192020-04-24 13:04:04 -0700131}
132
David Tolnay47e239d2020-08-28 00:32:04 -0700133macro_rules! impl_vector_element {
134 ($segment:expr, $name:expr, $ty:ty) => {
David Tolnayf0446632020-04-25 11:29:26 -0700135 const_assert_eq!(1, mem::align_of::<CxxVector<$ty>>());
136
David Tolnaye4b6a622020-04-24 14:55:42 -0700137 unsafe impl VectorElement for $ty {
David Tolnay47e239d2020-08-28 00:32:04 -0700138 const __NAME: &'static dyn Display = &$name;
David Tolnaye4b6a622020-04-24 14:55:42 -0700139 fn __vector_size(v: &CxxVector<$ty>) -> usize {
140 extern "C" {
141 attr! {
David Tolnay591dcb62020-09-01 23:00:38 -0700142 #[link_name = concat!("cxxbridge04$std$vector$", $segment, "$size")]
David Tolnaye4b6a622020-04-24 14:55:42 -0700143 fn __vector_size(_: &CxxVector<$ty>) -> usize;
144 }
145 }
146 unsafe { __vector_size(v) }
147 }
148 unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty {
149 extern "C" {
150 attr! {
David Tolnay591dcb62020-09-01 23:00:38 -0700151 #[link_name = concat!("cxxbridge04$std$vector$", $segment, "$get_unchecked")]
David Tolnaye4b6a622020-04-24 14:55:42 -0700152 fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty;
153 }
154 }
155 &*__get_unchecked(v, pos)
156 }
David Tolnay3b40b6f2020-04-24 17:58:24 -0700157 fn __unique_ptr_null() -> *mut c_void {
158 extern "C" {
159 attr! {
David Tolnay591dcb62020-09-01 23:00:38 -0700160 #[link_name = concat!("cxxbridge04$unique_ptr$std$vector$", $segment, "$null")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700161 fn __unique_ptr_null(this: *mut *mut c_void);
162 }
163 }
164 let mut repr = ptr::null_mut::<c_void>();
165 unsafe { __unique_ptr_null(&mut repr) }
166 repr
167 }
168 unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void {
169 extern "C" {
170 attr! {
David Tolnay591dcb62020-09-01 23:00:38 -0700171 #[link_name = concat!("cxxbridge04$unique_ptr$std$vector$", $segment, "$raw")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700172 fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>);
173 }
174 }
175 let mut repr = ptr::null_mut::<c_void>();
176 __unique_ptr_raw(&mut repr, raw);
177 repr
178 }
179 unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> {
180 extern "C" {
181 attr! {
David Tolnay591dcb62020-09-01 23:00:38 -0700182 #[link_name = concat!("cxxbridge04$unique_ptr$std$vector$", $segment, "$get")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700183 fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>;
184 }
185 }
186 __unique_ptr_get(&repr)
187 }
188 unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> {
189 extern "C" {
190 attr! {
David Tolnay591dcb62020-09-01 23:00:38 -0700191 #[link_name = concat!("cxxbridge04$unique_ptr$std$vector$", $segment, "$release")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700192 fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>;
193 }
194 }
195 __unique_ptr_release(&mut repr)
196 }
197 unsafe fn __unique_ptr_drop(mut repr: *mut c_void) {
198 extern "C" {
199 attr! {
David Tolnay591dcb62020-09-01 23:00:38 -0700200 #[link_name = concat!("cxxbridge04$unique_ptr$std$vector$", $segment, "$drop")]
David Tolnay3b40b6f2020-04-24 17:58:24 -0700201 fn __unique_ptr_drop(this: *mut *mut c_void);
202 }
203 }
204 __unique_ptr_drop(&mut repr);
205 }
David Tolnaye4b6a622020-04-24 14:55:42 -0700206 }
207 };
208}
209
David Tolnay47e239d2020-08-28 00:32:04 -0700210macro_rules! impl_vector_element_for_primitive {
211 ($ty:ident) => {
212 impl_vector_element!(stringify!($ty), stringify!($ty), $ty);
213 };
214}
215
David Tolnay4b91eaa2020-04-24 14:19:22 -0700216impl_vector_element_for_primitive!(u8);
217impl_vector_element_for_primitive!(u16);
218impl_vector_element_for_primitive!(u32);
219impl_vector_element_for_primitive!(u64);
220impl_vector_element_for_primitive!(usize);
221impl_vector_element_for_primitive!(i8);
222impl_vector_element_for_primitive!(i16);
223impl_vector_element_for_primitive!(i32);
224impl_vector_element_for_primitive!(i64);
225impl_vector_element_for_primitive!(isize);
226impl_vector_element_for_primitive!(f32);
227impl_vector_element_for_primitive!(f64);
David Tolnay47e239d2020-08-28 00:32:04 -0700228
229impl_vector_element!("string", "CxxString", CxxString);