blob: 8a73c7e841a9d3e631a32b78c96be3c50811686e [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 Tolnayc01d0a02020-04-24 13:30:44 -070018 pub fn len(&self) -> usize {
David Tolnay0e084662020-04-24 14:02:51 -070019 T::__vector_size(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070020 }
21
David Tolnaycdc87962020-04-24 13:45:59 -070022 /// Returns true if the vector contains no elements.
Myron Ahneba35cf2020-02-05 19:41:51 +070023 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070024 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070025 }
26
David Tolnaycdc87962020-04-24 13:45:59 -070027 /// Returns a reference to an element at the given position, or `None` if
28 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070029 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070030 if pos < self.len() {
David Tolnay31ad0be2020-04-24 13:29:51 -070031 Some(unsafe { T::__get_unchecked(self, pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070032 } else {
33 None
34 }
35 }
36
David Tolnay4944f2f2020-04-24 13:46:12 -070037 /// Returns a reference to an element without doing bounds checking.
38 ///
39 /// This is generally not recommended, use with caution! Calling this method
40 /// with an out-of-bounds index is undefined behavior even if the resulting
41 /// reference is not used.
42 pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
43 T::__get_unchecked(self, pos)
44 }
45
David Tolnaycdc87962020-04-24 13:45:59 -070046 /// Appends an element to the back of the vector.
Myron Ahneba35cf2020-02-05 19:41:51 +070047 pub fn push_back(&mut self, item: &T) {
David Tolnay0a63b4c2020-04-24 13:04:26 -070048 T::__push_back(self, item);
Myron Ahneba35cf2020-02-05 19:41:51 +070049 }
50}
51
David Tolnay3d88bdc2020-04-24 13:48:18 -070052pub struct Iter<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070053 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070054 index: usize,
55}
56
David Tolnayc3ed3a62020-04-24 13:34:50 -070057impl<'a, T: VectorElement> IntoIterator for &'a CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070058 type Item = &'a T;
David Tolnay3d88bdc2020-04-24 13:48:18 -070059 type IntoIter = Iter<'a, T>;
Myron Ahneba35cf2020-02-05 19:41:51 +070060
61 fn into_iter(self) -> Self::IntoIter {
David Tolnay3d88bdc2020-04-24 13:48:18 -070062 Iter { v: self, index: 0 }
Myron Ahneba35cf2020-02-05 19:41:51 +070063 }
64}
65
David Tolnay3d88bdc2020-04-24 13:48:18 -070066impl<'a, T: VectorElement> Iterator for Iter<'a, T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070067 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070068
Myron Ahneba35cf2020-02-05 19:41:51 +070069 fn next(&mut self) -> Option<Self::Item> {
70 self.index = self.index + 1;
71 self.v.get(self.index - 1)
72 }
73}
74
David Tolnay5104c862020-04-24 13:26:01 -070075// Methods are private; not intended to be implemented outside of cxxbridge
76// codebase.
David Tolnay1b341192020-04-24 13:04:04 -070077#[doc(hidden)]
David Tolnayc3ed3a62020-04-24 13:34:50 -070078pub unsafe trait VectorElement: Sized {
David Tolnay0e084662020-04-24 14:02:51 -070079 fn __vector_size(v: &CxxVector<Self>) -> usize;
David Tolnaycc75ad22020-04-24 14:45:16 -070080 unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self;
David Tolnayc3ed3a62020-04-24 13:34:50 -070081 fn __push_back(v: &CxxVector<Self>, item: &Self);
David Tolnay1b341192020-04-24 13:04:04 -070082}
83
David Tolnaye4b6a622020-04-24 14:55:42 -070084macro_rules! impl_vector_element_for_primitive {
85 ($ty:ident) => {
86 unsafe impl VectorElement for $ty {
87 fn __vector_size(v: &CxxVector<$ty>) -> usize {
88 extern "C" {
89 attr! {
90 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$size")]
91 fn __vector_size(_: &CxxVector<$ty>) -> usize;
92 }
93 }
94 unsafe { __vector_size(v) }
95 }
96 unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty {
97 extern "C" {
98 attr! {
99 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$get_unchecked")]
100 fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty;
101 }
102 }
103 &*__get_unchecked(v, pos)
104 }
105 fn __push_back(v: &CxxVector<$ty>, item: &$ty) {
106 extern "C" {
107 attr! {
108 #[link_name = concat!("cxxbridge02$std$vector$", stringify!($ty), "$push_back")]
109 fn __push_back(_: &CxxVector<$ty>, _: &$ty);
110 }
111 }
112 unsafe { __push_back(v, item) }
113 }
114 }
115 };
116}
117
David Tolnay4b91eaa2020-04-24 14:19:22 -0700118impl_vector_element_for_primitive!(u8);
119impl_vector_element_for_primitive!(u16);
120impl_vector_element_for_primitive!(u32);
121impl_vector_element_for_primitive!(u64);
122impl_vector_element_for_primitive!(usize);
123impl_vector_element_for_primitive!(i8);
124impl_vector_element_for_primitive!(i16);
125impl_vector_element_for_primitive!(i32);
126impl_vector_element_for_primitive!(i64);
127impl_vector_element_for_primitive!(isize);
128impl_vector_element_for_primitive!(f32);
129impl_vector_element_for_primitive!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -0700130
131const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());