blob: b47ec416e84d9af75ecc206323728a4b7d639fad [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 Tolnay4074ad22020-04-24 18:20:11 -070016impl<T> CxxVector<T>
17where
18 T: VectorElement,
19{
David Tolnaycdc87962020-04-24 13:45:59 -070020 /// Returns the number of elements in the vector.
David Tolnaydd839192020-04-24 16:41:29 -070021 ///
22 /// Matches the behavior of C++ [std::vector\<T\>::size][size].
23 ///
24 /// [size]: https://en.cppreference.com/w/cpp/container/vector/size
David Tolnayc01d0a02020-04-24 13:30:44 -070025 pub fn len(&self) -> usize {
David Tolnay0e084662020-04-24 14:02:51 -070026 T::__vector_size(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070027 }
28
David Tolnaycdc87962020-04-24 13:45:59 -070029 /// Returns true if the vector contains no elements.
David Tolnaydd839192020-04-24 16:41:29 -070030 ///
31 /// Matches the behavior of C++ [std::vector\<T\>::empty][empty].
32 ///
33 /// [empty]: https://en.cppreference.com/w/cpp/container/vector/empty
Myron Ahneba35cf2020-02-05 19:41:51 +070034 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070035 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070036 }
37
David Tolnaycdc87962020-04-24 13:45:59 -070038 /// Returns a reference to an element at the given position, or `None` if
39 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070040 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070041 if pos < self.len() {
David Tolnay31ad0be2020-04-24 13:29:51 -070042 Some(unsafe { T::__get_unchecked(self, pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070043 } else {
44 None
45 }
46 }
47
David Tolnay4944f2f2020-04-24 13:46:12 -070048 /// Returns a reference to an element without doing bounds checking.
49 ///
50 /// This is generally not recommended, use with caution! Calling this method
51 /// with an out-of-bounds index is undefined behavior even if the resulting
52 /// reference is not used.
David Tolnaydd839192020-04-24 16:41:29 -070053 ///
54 /// Matches the behavior of C++
55 /// [std::vector\<T\>::operator\[\]][operator_at].
56 ///
57 /// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
David Tolnay4944f2f2020-04-24 13:46:12 -070058 pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
59 T::__get_unchecked(self, pos)
60 }
61
David Tolnaycdc87962020-04-24 13:45:59 -070062 /// Appends an element to the back of the vector.
Myron Ahneba35cf2020-02-05 19:41:51 +070063 pub fn push_back(&mut self, item: &T) {
David Tolnay0a63b4c2020-04-24 13:04:26 -070064 T::__push_back(self, item);
Myron Ahneba35cf2020-02-05 19:41:51 +070065 }
66}
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> {
92 self.index = self.index + 1;
93 self.v.get(self.index - 1)
94 }
95}
96
David Tolnay5104c862020-04-24 13:26:01 -070097// Methods are private; not intended to be implemented outside of cxxbridge
98// codebase.
David Tolnay1b341192020-04-24 13:04:04 -070099#[doc(hidden)]
David Tolnayc3ed3a62020-04-24 13:34:50 -0700100pub unsafe trait VectorElement: Sized {
David Tolnay0e084662020-04-24 14:02:51 -0700101 fn __vector_size(v: &CxxVector<Self>) -> usize;
David Tolnaycc75ad22020-04-24 14:45:16 -0700102 unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self;
David Tolnayc3ed3a62020-04-24 13:34:50 -0700103 fn __push_back(v: &CxxVector<Self>, item: &Self);
David Tolnay1b341192020-04-24 13:04:04 -0700104}
105
David Tolnaye4b6a622020-04-24 14:55:42 -0700106macro_rules! impl_vector_element_for_primitive {
107 ($ty:ident) => {
108 unsafe impl VectorElement for $ty {
109 fn __vector_size(v: &CxxVector<$ty>) -> usize {
110 extern "C" {
111 attr! {
112 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$size")]
113 fn __vector_size(_: &CxxVector<$ty>) -> usize;
114 }
115 }
116 unsafe { __vector_size(v) }
117 }
118 unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty {
119 extern "C" {
120 attr! {
121 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$get_unchecked")]
122 fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty;
123 }
124 }
125 &*__get_unchecked(v, pos)
126 }
127 fn __push_back(v: &CxxVector<$ty>, item: &$ty) {
128 extern "C" {
129 attr! {
130 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$push_back")]
131 fn __push_back(_: &CxxVector<$ty>, _: &$ty);
132 }
133 }
134 unsafe { __push_back(v, item) }
135 }
136 }
137 };
138}
139
David Tolnay4b91eaa2020-04-24 14:19:22 -0700140impl_vector_element_for_primitive!(u8);
141impl_vector_element_for_primitive!(u16);
142impl_vector_element_for_primitive!(u32);
143impl_vector_element_for_primitive!(u64);
144impl_vector_element_for_primitive!(usize);
145impl_vector_element_for_primitive!(i8);
146impl_vector_element_for_primitive!(i16);
147impl_vector_element_for_primitive!(i32);
148impl_vector_element_for_primitive!(i64);
149impl_vector_element_for_primitive!(isize);
150impl_vector_element_for_primitive!(f32);
151impl_vector_element_for_primitive!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -0700152
153const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());