blob: 0be971d54e5893f08e49605688d49e79032b0731 [file] [log] [blame]
David Tolnay4f7e6fa2020-04-24 11:52:44 -07001use std::mem;
2
Myron Ahneba35cf2020-02-05 19:41:51 +07003pub trait VectorTarget<T> {
David Tolnaye90be1d2020-04-24 11:45:57 -07004 fn get_unchecked(v: &CxxVector<T>, pos: usize) -> &T
Myron Ahneba35cf2020-02-05 19:41:51 +07005 where
6 Self: Sized;
David Tolnaye90be1d2020-04-24 11:45:57 -07007 fn vector_length(v: &CxxVector<T>) -> usize
Myron Ahneba35cf2020-02-05 19:41:51 +07008 where
9 Self: Sized;
David Tolnaye90be1d2020-04-24 11:45:57 -070010 fn push_back(v: &CxxVector<T>, item: &T)
Myron Ahneba35cf2020-02-05 19:41:51 +070011 where
12 Self: Sized;
13}
14
David Tolnaye90be1d2020-04-24 11:45:57 -070015/// Binding to C++ `std::vector<T>`.
Myron Ahneba35cf2020-02-05 19:41:51 +070016///
17/// # Invariants
18///
19/// As an invariant of this API and the static analysis of the cxx::bridge
David Tolnay5fe93632020-04-24 12:31:00 -070020/// macro, in Rust code we can never obtain a `CxxVector` by value. Instead in
21/// Rust code we will only ever look at a vector behind a reference or smart
22/// pointer, as in `&CxxVector<T>` or `UniquePtr<CxxVector<T>>`.
David Tolnay4f7e6fa2020-04-24 11:52:44 -070023#[repr(C, packed)]
David Tolnaye90be1d2020-04-24 11:45:57 -070024pub struct CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070025 _private: [T; 0],
26}
27
David Tolnaye90be1d2020-04-24 11:45:57 -070028impl<T: VectorTarget<T>> CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070029 /// Returns the length of the vector in bytes.
30 pub fn size(&self) -> usize {
31 T::vector_length(self)
32 }
33
34 pub fn get_unchecked(&self, pos: usize) -> &T {
35 T::get_unchecked(self, pos)
36 }
37
38 /// Returns true if `self` has a length of zero bytes.
39 pub fn is_empty(&self) -> bool {
40 self.size() == 0
41 }
42
43 pub fn get(&self, pos: usize) -> Option<&T> {
44 if pos < self.size() {
45 Some(self.get_unchecked(pos))
46 } else {
47 None
48 }
49 }
50
51 pub fn push_back(&mut self, item: &T) {
52 T::push_back(self, item);
53 }
54}
55
Myron Ahneba35cf2020-02-05 19:41:51 +070056pub struct VectorIntoIterator<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070057 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070058 index: usize,
59}
60
David Tolnaye90be1d2020-04-24 11:45:57 -070061impl<'a, T: VectorTarget<T>> IntoIterator for &'a CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070062 type Item = &'a T;
63 type IntoIter = VectorIntoIterator<'a, T>;
64
65 fn into_iter(self) -> Self::IntoIter {
66 VectorIntoIterator { v: self, index: 0 }
67 }
68}
69
70impl<'a, T: VectorTarget<T>> Iterator for VectorIntoIterator<'a, T> {
71 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070072
Myron Ahneba35cf2020-02-05 19:41:51 +070073 fn next(&mut self) -> Option<Self::Item> {
74 self.index = self.index + 1;
75 self.v.get(self.index - 1)
76 }
77}
78
79cxxbridge_macro::vector_builtin!(u8);
80cxxbridge_macro::vector_builtin!(u16);
81cxxbridge_macro::vector_builtin!(u32);
82cxxbridge_macro::vector_builtin!(u64);
83cxxbridge_macro::vector_builtin!(usize);
84cxxbridge_macro::vector_builtin!(i8);
85cxxbridge_macro::vector_builtin!(i16);
86cxxbridge_macro::vector_builtin!(i32);
87cxxbridge_macro::vector_builtin!(i64);
88cxxbridge_macro::vector_builtin!(isize);
89cxxbridge_macro::vector_builtin!(f32);
90cxxbridge_macro::vector_builtin!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -070091
92const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());