Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Storage is a RefCell owning a Box<dyn Any>
- Borrow is an enum containing a BorrowRef or a BorrowRefMut
- */
- // This is to abstract mutabily from the input
- pub trait IterHelper<'a> {
- type Vec;
- fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec);
- }
- impl<'a, T: 'static> IterHelper<'a> for &T {
- type Vec = &'a Vec<T>;
- fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec) {
- unsafe {storage.unchecked_vec() }
- }
- }
- impl<'a, T: 'static> IterHelper<'a> for &mut T {
- type Vec = &'a mut Vec<T>;
- fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec) {
- unsafe {storage.unchecked_vec_mut() }
- }
- }
- // This is to abstract mutabily from the fields of the Iter struct
- pub trait GetHelper<'a> {
- type Out;
- fn get(&'a mut self, index: usize) -> Self::Out;
- }
- impl<'a, T: 'a> GetHelper<'a> for &Vec<T> {
- type Out = &'a T;
- fn get(&'a mut self, index: usize) -> Self::Out {
- &self[index]
- }
- }
- impl<'a, T: 'a> GetHelper<'a> for &mut Vec<T> {
- type Out = &'a mut T;
- fn get(&'a mut self, index: usize) -> Self::Out {
- &mut self[index]
- }
- }
- // From this point everything is inside a macro
- // Iter struct
- #[allow(non_snake_case)]
- pub struct IterAB<'a, A, B>
- where A: IterHelper<'a>, B: IterHelper<'a> {
- A: <A as IterHelper<'a>>::Vec,
- B: <B as IterHelper<'a>>::Vec,
- indices: std::slice::Iter<'a, [usize; 2]>,
- _borrow: [Borrow<'a>; 2],
- }
- impl<'a, A: 'static, B: 'static> GATIterator for IterAB<'a, A, B>
- // has to be 'a because of the struct but it should return 's
- where A: IterHelper<'a>, B: IterHelper<'a> {
- type Item = Tuple2Family<<A as IterHelper<'a>>::Family, <B as IterHelper<'a>>::Family>;
- fn next<'s>(&'s mut self) -> Option<<Self::Item as Family<'s>>::Out>
- where Self::Item: Family<'s> {
- self.indices.next().map(move |&[a, b]| {
- (
- (&mut self.A).get(a),
- (&mut self.B).get(b),
- )
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement