blob: 4a5e8e6a20ffacacbec19485d0162fa18d0179b5 [file] [log] [blame]
David Tolnay4f7e6fa2020-04-24 11:52:44 -07001use std::mem;
2
David Tolnaye90be1d2020-04-24 11:45:57 -07003/// Binding to C++ `std::vector<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 Tolnay0a63b4c2020-04-24 13:04:26 -070019 T::__vector_length(self)
Myron Ahneba35cf2020-02-05 19:41:51 +070020 }
21
David Tolnaycdc87962020-04-24 13:45:59 -070022 /// Returns a reference to an element without doing bounds checking.
23 ///
24 /// This is generally not recommended, use with caution! Calling this method
25 /// with an out-of-bounds index is undefined behavior even if the resulting
26 /// reference is not used.
David Tolnay31ad0be2020-04-24 13:29:51 -070027 pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
David Tolnay0a63b4c2020-04-24 13:04:26 -070028 T::__get_unchecked(self, pos)
Myron Ahneba35cf2020-02-05 19:41:51 +070029 }
30
David Tolnaycdc87962020-04-24 13:45:59 -070031 /// Returns true if the vector contains no elements.
Myron Ahneba35cf2020-02-05 19:41:51 +070032 pub fn is_empty(&self) -> bool {
David Tolnayc01d0a02020-04-24 13:30:44 -070033 self.len() == 0
Myron Ahneba35cf2020-02-05 19:41:51 +070034 }
35
David Tolnaycdc87962020-04-24 13:45:59 -070036 /// Returns a reference to an element at the given position, or `None` if
37 /// out of bounds.
Myron Ahneba35cf2020-02-05 19:41:51 +070038 pub fn get(&self, pos: usize) -> Option<&T> {
David Tolnayc01d0a02020-04-24 13:30:44 -070039 if pos < self.len() {
David Tolnay31ad0be2020-04-24 13:29:51 -070040 Some(unsafe { T::__get_unchecked(self, pos) })
Myron Ahneba35cf2020-02-05 19:41:51 +070041 } else {
42 None
43 }
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
Myron Ahneba35cf2020-02-05 19:41:51 +070052pub struct VectorIntoIterator<'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;
59 type IntoIter = VectorIntoIterator<'a, T>;
60
61 fn into_iter(self) -> Self::IntoIter {
62 VectorIntoIterator { v: self, index: 0 }
63 }
64}
65
David Tolnayc3ed3a62020-04-24 13:34:50 -070066impl<'a, T: VectorElement> Iterator for VectorIntoIterator<'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 {
79 unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self;
80 fn __vector_length(v: &CxxVector<Self>) -> usize;
81 fn __push_back(v: &CxxVector<Self>, item: &Self);
David Tolnay1b341192020-04-24 13:04:04 -070082}
83
Myron Ahneba35cf2020-02-05 19:41:51 +070084cxxbridge_macro::vector_builtin!(u8);
85cxxbridge_macro::vector_builtin!(u16);
86cxxbridge_macro::vector_builtin!(u32);
87cxxbridge_macro::vector_builtin!(u64);
88cxxbridge_macro::vector_builtin!(usize);
89cxxbridge_macro::vector_builtin!(i8);
90cxxbridge_macro::vector_builtin!(i16);
91cxxbridge_macro::vector_builtin!(i32);
92cxxbridge_macro::vector_builtin!(i64);
93cxxbridge_macro::vector_builtin!(isize);
94cxxbridge_macro::vector_builtin!(f32);
95cxxbridge_macro::vector_builtin!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -070096
97const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());