-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework matching of rotues with wildcards. With zip longest method
- Loading branch information
Showing
3 changed files
with
110 additions
and
16 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod router; | |
pub mod server; | ||
pub mod templates; | ||
pub mod workers; | ||
pub mod zip_longest; |
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,46 @@ | ||
//! Tools to handle zipping two iterators | ||
//! and continuing until both are exhausted. | ||
//! Inspired by [itertools::zip_longest](https://doc.servo.org/itertools/zip_longest/index.html) | ||
/// Struct where the next element is an option tuple | ||
/// with two optional elements. | ||
pub struct ZipLongestIter<T, U> { | ||
a: T, | ||
b: U, | ||
} | ||
|
||
impl<T, U> Iterator for ZipLongestIter<T, U> | ||
where | ||
T: Iterator, | ||
U: Iterator, | ||
{ | ||
type Item = (Option<T::Item>, Option<U::Item>); | ||
|
||
/// Returns the next element in the iterator. | ||
/// Returns Some as long as one element is available. | ||
/// If both iterators are exhausted, returns None. | ||
fn next(&mut self) -> Option<Self::Item> { | ||
match (self.a.next(), self.b.next()) { | ||
(None, None) => None, | ||
(a, b) => Some((a, b)), | ||
} | ||
} | ||
} | ||
|
||
pub trait ZipLongest { | ||
fn zip_longest<U>(self, other: U) -> ZipLongestIter<Self, U> | ||
where | ||
Self: Sized, | ||
U: IntoIterator; | ||
} | ||
impl<T> ZipLongest for T | ||
where | ||
T: Iterator, | ||
{ | ||
fn zip_longest<U>(self, other: U) -> ZipLongestIter<Self, U> | ||
where | ||
U: IntoIterator, | ||
{ | ||
ZipLongestIter { a: self, b: other } | ||
} | ||
} |