Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. use std::any::TypeId;
  2.  
  3. /*
  4. Storage is a RefCell owning a Box<dyn Any>
  5. Borrow is an enum containing a BorrowRef or a BorrowRefMut
  6. */
  7.  
  8.  
  9. // This is to abstract mutabily from the input
  10. pub trait IterHelper<'a> {
  11. type Vec;
  12. fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec);
  13. }
  14.  
  15. impl<'a, T: 'static> IterHelper<'a> for &T {
  16. type Vec = &'a Vec<T>;
  17. fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec) {
  18. unsafe {storage.unchecked_vec() }
  19. }
  20. }
  21.  
  22. impl<'a, T: 'static> IterHelper<'a> for &mut T {
  23. type Vec = &'a mut Vec<T>;
  24. fn borrow(storage: &'a Storage) -> (Borrow<'a>, Self::Vec) {
  25. unsafe {storage.unchecked_vec_mut() }
  26. }
  27. }
  28.  
  29. // This is to abstract mutabily from the fields of the Iter struct
  30. pub trait GetHelper<'a> {
  31. type Out;
  32. fn get(&'a mut self, index: usize) -> Self::Out;
  33. }
  34.  
  35. impl<'a, T: 'a> GetHelper<'a> for &Vec<T> {
  36. type Out = &'a T;
  37. fn get(&'a mut self, index: usize) -> Self::Out {
  38. &self[index]
  39. }
  40. }
  41.  
  42. impl<'a, T: 'a> GetHelper<'a> for &mut Vec<T> {
  43. type Out = &'a mut T;
  44. fn get(&'a mut self, index: usize) -> Self::Out {
  45. &mut self[index]
  46. }
  47. }
  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