blob: 3742fa43d7c3a6bf223379d316ecc32584c31480 [file] [log] [blame]
David Tolnay4b91eaa2020-04-24 14:19:22 -07001use cxxbridge_macro::impl_vector_element_for_primitive;
David Tolnay4f7e6fa2020-04-24 11:52:44 -07002use std::mem;
3
David Tolnaye90be1d2020-04-24 11:45:57 -07004/// Binding to C++ `std::vector<T>`.
Myron Ahneba35cf2020-02-05 19:41:51 +07005///
6/// # Invariants
7///
8/// As an invariant of this API and the static analysis of the cxx::bridge
David Tolnay5fe93632020-04-24 12:31:00 -07009/// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in
10/// Rust code we will only ever look at a vector behind a reference or smart
11/// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`.
David Tolnay4f7e6fa2020-04-24 11:52:44 -070012#[repr(C, packed)]
David Tolnaye90be1d2020-04-24 11:45:57 -070013pub struct CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070014 _private: [T; 0],
15}
16
David Tolnayc3ed3a62020-04-24 13:34:50 -070017impl<T: VectorElement> CxxVector<T> {
David Tolnaycdc87962020-04-24 13:45:59 -070018 /// Returns the number of elements in the vector.
David Tolnayc01d0a02020-04-24 13:30:44 -070019 pub fn len(&self) -> usize {
David Tolnay0e084662020-04-24 14:02:51 -070020 T::__vector_size(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070021 }
22
David Tolnaycdc87962020-04-24 13:45:59 -070023 /// Returns true if the vector contains no elements.
Myron Ahneba35cf2020-02-05 19:41:51 +070024 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070025 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070026 }
27
David Tolnaycdc87962020-04-24 13:45:59 -070028 /// Returns a reference to an element at the given position, or `None` if
29 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070030 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070031 if pos < self.len() {
David Tolnay31ad0be2020-04-24 13:29:51 -070032 Some(unsafe { T::__get_unchecked(self, pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070033 } else {
34 None
35 }
36 }
37
David Tolnay4944f2f2020-04-24 13:46:12 -070038 /// Returns a reference to an element without doing bounds checking.
39 ///
40 /// This is generally not recommended, use with caution! Calling this method
41 /// with an out-of-bounds index is undefined behavior even if the resulting
42 /// reference is not used.
43 pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
44 T::__get_unchecked(self, pos)
45 }
46
David Tolnaycdc87962020-04-24 13:45:59 -070047 /// Appends an element to the back of the vector.
Myron Ahneba35cf2020-02-05 19:41:51 +070048 pub fn push_back(&mut self, item: &T) {
David Tolnay0a63b4c2020-04-24 13:04:26 -070049 T::__push_back(self, item);
Myron Ahneba35cf2020-02-05 19:41:51 +070050 }
51}
52
David Tolnay3d88bdc2020-04-24 13:48:18 -070053pub struct Iter<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070054 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070055 index: usize,
56}
57
David Tolnayc3ed3a62020-04-24 13:34:50 -070058impl<'a, T: VectorElement> IntoIterator for &'a CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070059 type Item = &'a T;
David Tolnay3d88bdc2020-04-24 13:48:18 -070060 type IntoIter = Iter<'a, T>;
Myron Ahneba35cf2020-02-05 19:41:51 +070061
62 fn into_iter(self) -> Self::IntoIter {
David Tolnay3d88bdc2020-04-24 13:48:18 -070063 Iter { v: self, index: 0 }
Myron Ahneba35cf2020-02-05 19:41:51 +070064 }
65}
66
David Tolnay3d88bdc2020-04-24 13:48:18 -070067impl<'a, T: VectorElement> Iterator for Iter<'a, T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070068 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070069
Myron Ahneba35cf2020-02-05 19:41:51 +070070 fn next(&mut self) -> Option<Self::Item> {
71 self.index = self.index + 1;
72 self.v.get(self.index - 1)
73 }
74}
75
David Tolnay5104c862020-04-24 13:26:01 -070076// Methods are private; not intended to be implemented outside of cxxbridge
77// codebase.
David Tolnay1b341192020-04-24 13:04:04 -070078#[doc(hidden)]
David Tolnayc3ed3a62020-04-24 13:34:50 -070079pub unsafe trait VectorElement: Sized {
80 unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self;
David Tolnay0e084662020-04-24 14:02:51 -070081 fn __vector_size(v: &CxxVector<Self>) -> usize;
David Tolnayc3ed3a62020-04-24 13:34:50 -070082 fn __push_back(v: &CxxVector<Self>, item: &Self);
David Tolnay1b341192020-04-24 13:04:04 -070083}
84
David Tolnay4b91eaa2020-04-24 14:19:22 -070085impl_vector_element_for_primitive!(u8);
86impl_vector_element_for_primitive!(u16);
87impl_vector_element_for_primitive!(u32);
88impl_vector_element_for_primitive!(u64);
89impl_vector_element_for_primitive!(usize);
90impl_vector_element_for_primitive!(i8);
91impl_vector_element_for_primitive!(i16);
92impl_vector_element_for_primitive!(i32);
93impl_vector_element_for_primitive!(i64);
94impl_vector_element_for_primitive!(isize);
95impl_vector_element_for_primitive!(f32);
96impl_vector_element_for_primitive!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -070097
98const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());