-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
97 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// A macro for creating iterators that can have statically known | ||
/// different types. Useful for iterating over tree children, where | ||
/// each tree node variant yields a different iterator type. | ||
#[macro_export] | ||
macro_rules! multi_iterator { | ||
($Iter:ident { $($Variant:ident),* $(,)? }) => { | ||
#[derive(Debug, Clone)] | ||
enum $Iter<$($Variant),*> { | ||
$($Variant { iter: $Variant }),* | ||
} | ||
|
||
impl<$($Variant),*> $Iter<$($Variant),*> { | ||
$( | ||
#[allow(non_snake_case)] | ||
fn $Variant(iter: impl IntoIterator<IntoIter = $Variant>) -> Self { | ||
$Iter::$Variant { iter: iter.into_iter() } | ||
} | ||
)* | ||
} | ||
|
||
impl<T, $($Variant: Iterator<Item = T>),*> Iterator for $Iter<$($Variant),*> { | ||
type Item = T; | ||
fn next(&mut self) -> Option<T> { | ||
match self { $($Iter::$Variant { iter } => iter.next()),* } | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
match self { $($Iter::$Variant { iter } => iter.size_hint()),* } | ||
} | ||
} | ||
|
||
impl<T, $($Variant: DoubleEndedIterator<Item = T>),*> DoubleEndedIterator for $Iter<$($Variant),*> { | ||
fn next_back(&mut self) -> Option<T> { | ||
match self { $($Iter::$Variant { iter } => iter.next_back()),* } | ||
} | ||
} | ||
|
||
impl<T, $($Variant: ExactSizeIterator<Item = T>),*> ExactSizeIterator for $Iter<$($Variant),*> {} | ||
}; | ||
} |