From 3cff5410cd2229250a0744a86e0f41f6e8c74c69 Mon Sep 17 00:00:00 2001 From: Marc Bodmer Date: Thu, 6 Jun 2024 14:41:54 +0200 Subject: [PATCH 1/4] 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 --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f3941cc..f3ab375 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -258,6 +258,10 @@ impl PathTree { from_utf8(&bytes).map(ToString::to_string).ok() }) } + + pub fn iter(&self) -> Iter<'_, (T, Vec)> { + self.routes.iter() + } } /// Matched route path infomation. From 176e2a312c2e640059c47759183d262f4bf4cd21 Mon Sep 17 00:00:00 2001 From: Marc Bodmer Date: Thu, 6 Jun 2024 15:43:04 +0200 Subject: [PATCH 2/4] feat: also add into_iter() clippy requests an implementation of into_iter() when implementing iter() Signed-off-by: Marc Bodmer --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f3ab375..75e7e4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,7 +140,7 @@ extern crate alloc; use alloc::{ string::{String, ToString}, - vec::Vec, + vec::{IntoIter, Vec}, }; use core::{slice::Iter, str::from_utf8}; use smallvec::SmallVec; @@ -262,6 +262,10 @@ impl PathTree { pub fn iter(&self) -> Iter<'_, (T, Vec)> { self.routes.iter() } + + pub fn into_iter(self) -> IntoIter<(T, Vec)> { + self.routes.into_iter() + } } /// Matched route path infomation. From 8e0e0e2d6fb1c96d65ebc560e0946da08e0c12fd Mon Sep 17 00:00:00 2001 From: Marc Bodmer Date: Thu, 6 Jun 2024 16:16:13 +0200 Subject: [PATCH 3/4] feat: implement IntoIterator trait instead of forwarding from vec Signed-off-by: Marc Bodmer --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 75e7e4f..5a5df07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -262,8 +262,13 @@ impl PathTree { pub fn iter(&self) -> Iter<'_, (T, Vec)> { self.routes.iter() } +} + +impl IntoIterator for PathTree { + type Item = (T, Vec); + type IntoIter = IntoIter<(T, Vec)>; - pub fn into_iter(self) -> IntoIter<(T, Vec)> { + fn into_iter(self) -> Self::IntoIter { self.routes.into_iter() } } From a894c9aa6850a46bd372bba85a6c7b186b784014 Mon Sep 17 00:00:00 2001 From: Marc Bodmer Date: Thu, 6 Jun 2024 16:49:07 +0200 Subject: [PATCH 4/4] fix: implement IntoIterator for &PathTree Signed-off-by: Marc Bodmer --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5a5df07..7b73a6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,7 +140,7 @@ extern crate alloc; use alloc::{ string::{String, ToString}, - vec::{IntoIter, Vec}, + vec::Vec, }; use core::{slice::Iter, str::from_utf8}; use smallvec::SmallVec; @@ -264,12 +264,12 @@ impl PathTree { } } -impl IntoIterator for PathTree { - type Item = (T, Vec); - type IntoIter = IntoIter<(T, Vec)>; +impl<'a, T> IntoIterator for &'a PathTree { + type Item = &'a (T, Vec); + type IntoIter = Iter<'a, (T, Vec)>; fn into_iter(self) -> Self::IntoIter { - self.routes.into_iter() + self.iter() } }