| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame^] | 1 | pub trait VectorTarget<T> { |
| 2 | fn get_unchecked(v: &RealVector<T>, pos: usize) -> &T |
| 3 | where |
| 4 | Self: Sized; |
| 5 | fn vector_length(v: &RealVector<T>) -> usize |
| 6 | where |
| 7 | Self: Sized; |
| 8 | fn push_back(v: &RealVector<T>, item: &T) |
| 9 | where |
| 10 | Self: Sized; |
| 11 | } |
| 12 | |
| 13 | /// Binding to C++ `std::vector`. |
| 14 | /// |
| 15 | /// # Invariants |
| 16 | /// |
| 17 | /// As an invariant of this API and the static analysis of the cxx::bridge |
| 18 | /// macro, in Rust code we can never obtain a `Vector` by value. C++'s vector |
| 19 | /// requires a move constructor and may hold internal pointers, which is not |
| 20 | /// compatible with Rust's move behavior. Instead in Rust code we will only ever |
| 21 | /// look at a Vector through a reference or smart pointer, as in `&Vector` |
| 22 | /// or `UniquePtr<Vector>`. |
| 23 | #[repr(C)] |
| 24 | pub struct RealVector<T> { |
| 25 | _private: [T; 0], |
| 26 | } |
| 27 | |
| 28 | impl<T: VectorTarget<T>> RealVector<T> { |
| 29 | /// 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 | |
| 56 | unsafe impl<T> Send for RealVector<T> where T: Send + VectorTarget<T> {} |
| 57 | |
| 58 | pub struct VectorIntoIterator<'a, T> { |
| 59 | v: &'a RealVector<T>, |
| 60 | index: usize, |
| 61 | } |
| 62 | |
| 63 | impl<'a, T: VectorTarget<T>> IntoIterator for &'a RealVector<T> { |
| 64 | type Item = &'a T; |
| 65 | type IntoIter = VectorIntoIterator<'a, T>; |
| 66 | |
| 67 | fn into_iter(self) -> Self::IntoIter { |
| 68 | VectorIntoIterator { v: self, index: 0 } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl<'a, T: VectorTarget<T>> Iterator for VectorIntoIterator<'a, T> { |
| 73 | type Item = &'a T; |
| 74 | fn next(&mut self) -> Option<Self::Item> { |
| 75 | self.index = self.index + 1; |
| 76 | self.v.get(self.index - 1) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | cxxbridge_macro::vector_builtin!(u8); |
| 81 | cxxbridge_macro::vector_builtin!(u16); |
| 82 | cxxbridge_macro::vector_builtin!(u32); |
| 83 | cxxbridge_macro::vector_builtin!(u64); |
| 84 | cxxbridge_macro::vector_builtin!(usize); |
| 85 | cxxbridge_macro::vector_builtin!(i8); |
| 86 | cxxbridge_macro::vector_builtin!(i16); |
| 87 | cxxbridge_macro::vector_builtin!(i32); |
| 88 | cxxbridge_macro::vector_builtin!(i64); |
| 89 | cxxbridge_macro::vector_builtin!(isize); |
| 90 | cxxbridge_macro::vector_builtin!(f32); |
| 91 | cxxbridge_macro::vector_builtin!(f64); |