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