blob: 6cf927b5ddce068d8c3e2e00f7a2a36b3c98fe52 [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
20/// macro, in Rust code we can never obtain a `Vector` by value. C++'s vector
21/// requires a move constructor and may hold internal pointers, which is not
22/// compatible with Rust's move behavior. Instead in Rust code we will only ever
23/// look at a Vector through a reference or smart pointer, as in `&Vector`
24/// or `UniquePtr<Vector>`.
David Tolnay4f7e6fa2020-04-24 11:52:44 -070025#[repr(C, packed)]
David Tolnaye90be1d2020-04-24 11:45:57 -070026pub struct CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070027 _private: [T; 0],
28}
29
David Tolnaye90be1d2020-04-24 11:45:57 -070030impl<T: VectorTarget<T>> CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070031 /// Returns the length of the vector in bytes.
32 pub fn size(&self) -> usize {
33 T::vector_length(self)
34 }
35
36 pub fn get_unchecked(&self, pos: usize) -> &T {
37 T::get_unchecked(self, pos)
38 }
39
40 /// Returns true if `self` has a length of zero bytes.
41 pub fn is_empty(&self) -> bool {
42 self.size() == 0
43 }
44
45 pub fn get(&self, pos: usize) -> Option<&T> {
46 if pos < self.size() {
47 Some(self.get_unchecked(pos))
48 } else {
49 None
50 }
51 }
52
53 pub fn push_back(&mut self, item: &T) {
54 T::push_back(self, item);
55 }
56}
57
David Tolnaye90be1d2020-04-24 11:45:57 -070058unsafe impl<T> Send for CxxVector<T> where T: Send + VectorTarget<T> {}
Myron Ahneba35cf2020-02-05 19:41:51 +070059
60pub struct VectorIntoIterator<'a, T> {
David Tolnaye90be1d2020-04-24 11:45:57 -070061 v: &'a CxxVector<T>,
Myron Ahneba35cf2020-02-05 19:41:51 +070062 index: usize,
63}
64
David Tolnaye90be1d2020-04-24 11:45:57 -070065impl<'a, T: VectorTarget<T>> IntoIterator for &'a CxxVector<T> {
Myron Ahneba35cf2020-02-05 19:41:51 +070066 type Item = &'a T;
67 type IntoIter = VectorIntoIterator<'a, T>;
68
69 fn into_iter(self) -> Self::IntoIter {
70 VectorIntoIterator { v: self, index: 0 }
71 }
72}
73
74impl<'a, T: VectorTarget<T>> Iterator for VectorIntoIterator<'a, T> {
75 type Item = &'a T;
David Tolnay85db5a02020-04-25 13:17:27 -070076
Myron Ahneba35cf2020-02-05 19:41:51 +070077 fn next(&mut self) -> Option<Self::Item> {
78 self.index = self.index + 1;
79 self.v.get(self.index - 1)
80 }
81}
82
83cxxbridge_macro::vector_builtin!(u8);
84cxxbridge_macro::vector_builtin!(u16);
85cxxbridge_macro::vector_builtin!(u32);
86cxxbridge_macro::vector_builtin!(u64);
87cxxbridge_macro::vector_builtin!(usize);
88cxxbridge_macro::vector_builtin!(i8);
89cxxbridge_macro::vector_builtin!(i16);
90cxxbridge_macro::vector_builtin!(i32);
91cxxbridge_macro::vector_builtin!(i64);
92cxxbridge_macro::vector_builtin!(isize);
93cxxbridge_macro::vector_builtin!(f32);
94cxxbridge_macro::vector_builtin!(f64);
David Tolnay4f7e6fa2020-04-24 11:52:44 -070095
96const_assert_eq!(1, mem::align_of::<CxxVector<usize>>());