blob: c30dd023b8d25c339343b41bdc11771ac82e74c2 [file] [log] [blame]
David Tolnay4f7e6fa2020-04-24 11:52:44 -07001use std::mem;
2
David Tolnay61a9fdf2020-04-24 16:19:42 -07003/// Binding to C++ `std::vector<T, std::allocator<T>>`.
Myron Ahneba35cf2020-02-05 19:41:51 +07004///
5/// # Invariants
6///
7/// As an invariant of this API and the static analysis of the cxx::bridge
David Tolnay5fe93632020-04-24 12:31:00 -07008/// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in
9/// Rust code we will only ever look at a vector behind a reference or smart
10/// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`.
David Tolnay4f7e6fa2020-04-24 11:52:44 -070011#[repr(C, packed)]
David Tolnaye90be1d2020-04-24 11:45:57 -070012pub struct CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070013 _private: [T; 0],
14}
15
David Tolnayc3ed3a62020-04-24 13:34:50 -070016impl<T: VectorElement> CxxVector<T> {
David Tolnaycdc87962020-04-24 13:45:59 -070017 /// Returns the number of elements in the vector.
David Tolnaydd839192020-04-24 16:41:29 -070018 ///
19 /// Matches the behavior of C++ [std::vector\<T\>::size][size].
20 ///
21 /// [size]: https://en.cppreference.com/w/cpp/container/vector/size
David Tolnayc01d0a02020-04-24 13:30:44 -070022 pub fn len(&self) -> usize {
David Tolnay0e084662020-04-24 14:02:51 -070023 T::__vector_size(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070024 }
25
David Tolnaycdc87962020-04-24 13:45:59 -070026 /// Returns true if the vector contains no elements.
David Tolnaydd839192020-04-24 16:41:29 -070027 ///
28 /// Matches the behavior of C++ [std::vector\<T\>::empty][empty].
29 ///
30 /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty
Myron Ahneba35cf2020-02-05 19:41:51 +070031 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070032 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070033 }
34
David Tolnaycdc87962020-04-24 13:45:59 -070035 /// Returns a reference to an element at the given position, or `None` if
36 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070037 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070038 if pos < self.len() {
David Tolnay31ad0be2020-04-24 13:29:51 -070039 Some(unsafe { T::__get_unchecked(self, pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070040 } else {
41 None
42 }
43 }
44
David Tolnay4944f2f2020-04-24 13:46:12 -070045 /// Returns a reference to an element without doing bounds checking.
46 ///
47 /// This is generally not recommended, use with caution! Calling this method
48 /// with an out-of-bounds index is undefined behavior even if the resulting
49 /// reference is not used.
David Tolnaydd839192020-04-24 16:41:29 -070050 ///
51 /// Matches the behavior of C++
52 /// [std::vector\<T\>::operator\[\]][operator_at].
53 ///
54 /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
David Tolnay4944f2f2020-04-24 13:46:12 -070055 pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
56 T::__get_unchecked(self, pos)
57 }
58
David Tolnaycdc87962020-04-24 13:45:59 -070059 /// Appends an element to the back of the vector.
Myron Ahneba35cf2020-02-05 19:41:51 +070060 pub fn push_back(&mut self, item: &T) {
David Tolnay0a63b4c2020-04-24 13:04:26 -070061 T::__push_back(self, item);
Myron Ahneba35cf2020-02-05 19:41:51 +070062 }
63}
64
David Tolnay3d88bdc2020-04-24 13:48:18 -070065pub struct Iter<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070066 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070067 index: usize,
68}
69
David Tolnayc3ed3a62020-04-24 13:34:50 -070070impl<'a, T: VectorElement> IntoIterator for &'a CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070071 type Item = &'a T;
David Tolnay3d88bdc2020-04-24 13:48:18 -070072 type IntoIter = Iter<'a, T>;
Myron Ahneba35cf2020-02-05 19:41:51 +070073
74 fn into_iter(self) -> Self::IntoIter {
David Tolnay3d88bdc2020-04-24 13:48:18 -070075 Iter { v: self, index: 0 }
Myron Ahneba35cf2020-02-05 19:41:51 +070076 }
77}
78
David Tolnay3d88bdc2020-04-24 13:48:18 -070079impl<'a, T: VectorElement> Iterator for Iter<'a, T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070080 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070081
Myron Ahneba35cf2020-02-05 19:41:51 +070082 fn next(&mut self) -> Option<Self::Item> {
83 self.index = self.index + 1;
84 self.v.get(self.index - 1)
85 }
86}
87
David Tolnay5104c862020-04-24 13:26:01 -070088// Methods are private; not intended to be implemented outside of cxxbridge
89// codebase.
David Tolnay1b341192020-04-24 13:04:04 -070090#[doc(hidden)]
David Tolnayc3ed3a62020-04-24 13:34:50 -070091pub unsafe trait VectorElement: Sized {
David Tolnay0e084662020-04-24 14:02:51 -070092 fn __vector_size(v: &CxxVector<Self>) -> usize;
David Tolnaycc75ad22020-04-24 14:45:16 -070093 unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self;
David Tolnayc3ed3a62020-04-24 13:34:50 -070094 fn __push_back(v: &CxxVector<Self>, item: &Self);
David Tolnay1b341192020-04-24 13:04:04 -070095}
96
David Tolnaye4b6a622020-04-24 14:55:42 -070097macro_rules! impl_vector_element_for_primitive {
98 ($ty:ident) => {
99 unsafe impl VectorElement for $ty {
100 fn __vector_size(v: &CxxVector<$ty>) -> usize {
101 extern "C" {
102 attr! {
103 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$size")]
104 fn __vector_size(_: &CxxVector<$ty>) -> usize;
105 }
106 }
107 unsafe { __vector_size(v) }
108 }
109 unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty {
110 extern "C" {
111 attr! {
112 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$get_unchecked")]
113 fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty;
114 }
115 }
116 &*__get_unchecked(v, pos)
117 }
118 fn __push_back(v: &CxxVector<$ty>, item: &$ty) {
119 extern "C" {
120 attr! {
121 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$push_back")]
122 fn __push_back(_: &CxxVector<$ty>, _: &$ty);
123 }
124 }
125 unsafe { __push_back(v, item) }
126 }
127 }
128 };
129}
130
David Tolnay4b91eaa2020-04-24 14:19:22 -0700131impl_vector_element_for_primitive!(u8);
132impl_vector_element_for_primitive!(u16);
133impl_vector_element_for_primitive!(u32);
134impl_vector_element_for_primitive!(u64);
135impl_vector_element_for_primitive!(usize);
136impl_vector_element_for_primitive!(i8);
137impl_vector_element_for_primitive!(i16);
138impl_vector_element_for_primitive!(i32);
139impl_vector_element_for_primitive!(i64);
140impl_vector_element_for_primitive!(isize);
141impl_vector_element_for_primitive!(f32);
142impl_vector_element_for_primitive!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -0700143
144const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());