Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. /*
  2. Storage is a RefCell owning a Box<dyn Any>
  3. Borrow is an enum containing a BorrowRef or a BorrowRefMut
  4. */
  5.  
  6.  
  7. // This is to abstract mutabily from the input
  8. pub trait IterHelper<'a> {
  9. type Vec;
  10. fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec);
  11. }
  12.  
  13. impl<'a, T: 'static> IterHelper<'a> for &T {
  14. type Vec = &'a Vec<T>;
  15. fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec) {
  16. unsafe {storage.unchecked_vec() }
  17. }
  18. }
  19.  
  20. impl<'a, T: 'static> IterHelper<'a> for &mut T {
  21. type Vec = &'a mut Vec<T>;
  22. fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec) {
  23. unsafe {storage.unchecked_vec_mut() }
  24. }
  25. }
  26.  
  27. // This is to abstract mutabily from the fields of the Iter struct
  28. pub trait GetHelper<'a> {
  29. type Out;
  30. fn get(&'a mut self, index: usize) -> Self::Out;
  31. }
  32.  
  33. impl<'a, T: 'a> GetHelper<'a> for &Vec<T> {
  34. type Out = &'a T;
  35. fn get(&'a mut self, index: usize) -> Self::Out {
  36. &self[index]
  37. }
  38. }
  39.  
  40. impl<'a, T: 'a> GetHelper<'a> for &mut Vec<T> {
  41. type Out = &'a mut T;
  42. fn get(&'a mut self, index: usize) -> Self::Out {
  43. &mut self[index]
  44. }
  45. }
  46.  
  47. // From this point everything is inside a macro
  48.  
  49. // Iter struct
  50. #[allow(non_snake_case)]
  51. pub struct IterAB<'a, A, B>
  52. where A: IterHelper<'a>, B: IterHelper<'a> {
  53. A: <A as IterHelper<'a>>::Vec,
  54. B: <B as IterHelper<'a>>::Vec,
  55. indices: std::slice::Iter<'a, [usize; 2]>,
  56. _borrow: [Borrow<'a>; 2],
  57. }
  58.  
  59. impl<'a, A: 'static, B: 'static> GATIterator for IterAB<'a, A, B>
  60. // has to be 'a because of the struct but it should return 's
  61. where A: IterHelper<'a>, B: IterHelper<'a> {
  62. type Item = Tuple2Family<<A as IterHelper<'a>>::Family, <B as IterHelper<'a>>::Family>;
  63.  
  64. fn next<'s>(&'s mut self) -> Option<<Self::Item as Family<'s>>::Out>
  65. where Self::Item: Family<'s> {
  66. self.indices.next().map(move |&[a, b]| {
  67. (
  68. (&mut self.A).get(a),
  69. (&mut self.B).get(b),
  70. )
  71. })
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement