Skip to content

Commit

Permalink
feat: extend api with iterator for routes (#41)
Browse files Browse the repository at this point in the history
* feat: extend api with iterator for routes

Forward iterator access to the routes to allow collecting
them as a client. This allows eg. collecting the routes
to subscribe them when using the router in an MQTT usecase.

Signed-off-by: Marc Bodmer <[email protected]>

* feat: also add into_iter()

clippy requests an implementation of into_iter()
when implementing iter()

Signed-off-by: Marc Bodmer <[email protected]>

* feat: implement IntoIterator trait instead of forwarding from vec

Signed-off-by: Marc Bodmer <[email protected]>

* fix: implement IntoIterator for &PathTree<T>

Signed-off-by: Marc Bodmer <[email protected]>

---------

Signed-off-by: Marc Bodmer <[email protected]>
  • Loading branch information
mbodmer authored Jun 6, 2024
1 parent 14dde2b commit ff63bd3
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::str::from_utf8;
use core::{slice::Iter, str::from_utf8};
use smallvec::SmallVec;

mod node;
Expand Down Expand Up @@ -258,6 +258,19 @@ impl<T> PathTree<T> {
from_utf8(&bytes).map(ToString::to_string).ok()
})
}

pub fn iter(&self) -> Iter<'_, (T, Vec<Piece>)> {
self.routes.iter()
}
}

impl<'a, T> IntoIterator for &'a PathTree<T> {
type Item = &'a (T, Vec<Piece>);
type IntoIter = Iter<'a, (T, Vec<Piece>)>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

/// Matched route path infomation.
Expand Down

0 comments on commit ff63bd3

Please sign in to comment.