blob: f7185bb035a76405c901a56b7667a4a2d658bbc6 [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 Tolnay67ba0b52020-04-24 12:43:39 -070016impl<T: VectorElement<T>> CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070017 /// Returns the length of the vector in bytes.
18 pub fn size(&self) -> usize {
19 T::vector_length(self)
20 }
21
22 pub fn get_unchecked(&self, pos: usize) -> &T {
23 T::get_unchecked(self, pos)
24 }
25
26 /// Returns true if `self` has a length of zero bytes.
27 pub fn is_empty(&self) -> bool {
28 self.size() == 0
29 }
30
31 pub fn get(&self, pos: usize) -> Option<&T> {
32 if pos < self.size() {
33 Some(self.get_unchecked(pos))
34 } else {
35 None
36 }
37 }
38
39 pub fn push_back(&mut self, item: &T) {
40 T::push_back(self, item);
41 }
42}
43
Myron Ahneba35cf2020-02-05 19:41:51 +070044pub struct VectorIntoIterator<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070045 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070046 index: usize,
47}
48
David Tolnay67ba0b52020-04-24 12:43:39 -070049impl<'a, T: VectorElement<T>> IntoIterator for &'a CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070050 type Item = &'a T;
51 type IntoIter = VectorIntoIterator<'a, T>;
52
53 fn into_iter(self) -> Self::IntoIter {
54 VectorIntoIterator { v: self, index: 0 }
55 }
56}
57
David Tolnay67ba0b52020-04-24 12:43:39 -070058impl<'a, T: VectorElement<T>> Iterator for VectorIntoIterator<'a, T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070059 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070060
Myron Ahneba35cf2020-02-05 19:41:51 +070061 fn next(&mut self) -> Option<Self::Item> {
62 self.index = self.index + 1;
63 self.v.get(self.index - 1)
64 }
65}
66
David Tolnay1b341192020-04-24 13:04:04 -070067#[doc(hidden)]
68pub trait VectorElement<T> {
69 fn get_unchecked(v: &CxxVector<T>, pos: usize) -> &T
70 where
71 Self: Sized;
72 fn vector_length(v: &CxxVector<T>) -> usize
73 where
74 Self: Sized;
75 fn push_back(v: &CxxVector<T>, item: &T)
76 where
77 Self: Sized;
78}
79
Myron Ahneba35cf2020-02-05 19:41:51 +070080cxxbridge_macro::vector_builtin!(u8);
81cxxbridge_macro::vector_builtin!(u16);
82cxxbridge_macro::vector_builtin!(u32);
83cxxbridge_macro::vector_builtin!(u64);
84cxxbridge_macro::vector_builtin!(usize);
85cxxbridge_macro::vector_builtin!(i8);
86cxxbridge_macro::vector_builtin!(i16);
87cxxbridge_macro::vector_builtin!(i32);
88cxxbridge_macro::vector_builtin!(i64);
89cxxbridge_macro::vector_builtin!(isize);
90cxxbridge_macro::vector_builtin!(f32);
91cxxbridge_macro::vector_builtin!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -070092
93const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());