Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use either::Either; // 1.5.2
- use std::iter;
- #[derive(Debug, Default)]
- pub struct Data<X, Y> {
- head: Option<Y>,
- pairs: Vec<(X, Y)>,
- tail: Option<X>,
- }
- impl<X, Y> Data<X, Y> {
- pub fn iter(&self) -> impl Iterator<Item = Either<&X, &Y>> {
- let head = self.head.iter().map(Either::Right);
- let pairs = self.pairs.iter().flat_map(|(a, b)| {
- let a = iter::once(Either::Left(a));
- let b = iter::once(Either::Right(b));
- a.chain(b)
- });
- let tail = self.tail.iter().map(Either::Left);
- head.chain(pairs).chain(tail)
- }
- }
- #[derive(Debug)]
- struct A;
- #[derive(Debug)]
- struct B;
- fn main() {
- let data = Data {
- head: Some(B),
- pairs: vec![(A, B)],
- tail: None,
- };
- let _ : Vec<_> = data.iter().collect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement