Skip to content

Commit dc2f4b4

Browse files
authored
Merge pull request http-rs#22 from sfackler/master
Expose an iterator over path parameters
2 parents 1a689f6 + fc1553c commit dc2f4b4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use nfa::NFA;
66
use nfa::CharacterClass;
77
use std::collections::BTreeMap;
8+
use std::collections::btree_map;
89
use std::cmp::Ordering;
910
use std::ops::Index;
1011

@@ -75,6 +76,10 @@ impl Params {
7576
pub fn find(&self, key: &str) -> Option<&str> {
7677
self.map.get(key).map(|s| &s[..])
7778
}
79+
80+
pub fn iter<'a>(&'a self) -> Iter<'a> {
81+
Iter(self.map.iter())
82+
}
7883
}
7984

8085
impl<'a> Index<&'a str> for Params {
@@ -87,6 +92,30 @@ impl<'a> Index<&'a str> for Params {
8792
}
8893
}
8994

95+
impl<'a> IntoIterator for &'a Params {
96+
type IntoIter = Iter<'a>;
97+
type Item = (&'a str, &'a str);
98+
99+
fn into_iter(self) -> Iter<'a> {
100+
self.iter()
101+
}
102+
}
103+
104+
pub struct Iter<'a>(btree_map::Iter<'a, String, String>);
105+
106+
impl<'a> Iterator for Iter<'a> {
107+
type Item = (&'a str, &'a str);
108+
109+
#[inline]
110+
fn next(&mut self) -> Option<(&'a str, &'a str)> {
111+
self.0.next().map(|(k, v)| (&**k, &**v))
112+
}
113+
114+
fn size_hint(&self) -> (usize, Option<usize>) {
115+
self.0.size_hint()
116+
}
117+
}
118+
90119
pub struct Match<T> {
91120
pub handler: T,
92121
pub params: Params

0 commit comments

Comments
 (0)