Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. use either::Either; // 1.5.2
  2. use std::iter;
  3.  
  4. #[derive(Debug, Default)]
  5. pub struct Data<X, Y> {
  6. head: Option<Y>,
  7. pairs: Vec<(X, Y)>,
  8. tail: Option<X>,
  9. }
  10.  
  11. impl<X, Y> Data<X, Y> {
  12. pub fn iter(&self) -> impl Iterator<Item = Either<&X, &Y>> {
  13. let head = self.head.iter().map(Either::Right);
  14.  
  15. let pairs = self.pairs.iter().flat_map(|(a, b)| {
  16. let a = iter::once(Either::Left(a));
  17. let b = iter::once(Either::Right(b));
  18. a.chain(b)
  19. });
  20.  
  21. let tail = self.tail.iter().map(Either::Left);
  22.  
  23. head.chain(pairs).chain(tail)
  24. }
  25. }
  26.  
  27. #[derive(Debug)]
  28. struct A;
  29. #[derive(Debug)]
  30. struct B;
  31.  
  32. fn main() {
  33. let data = Data {
  34. head: Some(B),
  35. pairs: vec![(A, B)],
  36. tail: None,
  37. };
  38.  
  39. let _ : Vec<_> = data.iter().collect();
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement