1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-22 21:01:36 +03:00

bidi: add helper for using Direction with Iterator

This commit is contained in:
Wez Furlong 2022-01-30 12:13:50 -07:00
parent 98f35bbf24
commit c80ee319e2

View File

@ -29,4 +29,36 @@ impl Direction {
Self::LeftToRight => BidiClass::LeftToRight,
}
}
/// Given a DoubleEndedIterator, returns an iterator that will
/// either walk it in its natural order if Direction==LeftToRight,
/// or in reverse order if Direction==RightToLeft
pub fn iter<I: DoubleEndedIterator<Item = T>, T>(self, iter: I) -> DirectionIter<I, T> {
DirectionIter::wrap(iter, self)
}
}
pub enum DirectionIter<I: DoubleEndedIterator<Item = T>, T> {
LTR(I),
RTL(std::iter::Rev<I>),
}
impl<I: DoubleEndedIterator<Item = T>, T> DirectionIter<I, T> {
pub fn wrap(iter: I, direction: Direction) -> Self {
match direction {
Direction::LeftToRight => Self::LTR(iter),
Direction::RightToLeft => Self::RTL(iter.rev()),
}
}
}
impl<I: DoubleEndedIterator<Item = T>, T> Iterator for DirectionIter<I, T> {
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::LTR(i) => i.next(),
Self::RTL(i) => i.next(),
}
}
}