From 7d829f198e59bebd018c614907a0302b49b6a844 Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Tue, 18 Oct 2022 04:19:26 +1100 Subject: [PATCH] ready-cache: Allow iteration over ready services (#700) Adds `iter_ready` and `iter_ready_mut` to allow iteration over ready services within the ready_cache. This allows the ready cache to be used for router-like services that wish to direct requests towards specific services. Allowing iteration directly means that cache keys do not have to be redundantly stored separate to the ready_cache. --- tower/src/ready_cache/cache.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tower/src/ready_cache/cache.rs b/tower/src/ready_cache/cache.rs index 8070d780..282f2bb8 100644 --- a/tower/src/ready_cache/cache.rs +++ b/tower/src/ready_cache/cache.rs @@ -197,6 +197,16 @@ where self.ready.get_index_mut(idx).map(|(k, v)| (k, &mut v.0)) } + /// Returns an iterator over the ready keys and services. + pub fn iter_ready(&self) -> impl Iterator { + self.ready.iter().map(|(k, s)| (k, &s.0)) + } + + /// Returns a mutable iterator over the ready keys and services. + pub fn iter_ready_mut(&mut self) -> impl Iterator { + self.ready.iter_mut().map(|(k, s)| (k, &mut s.0)) + } + /// Evicts an item from the cache. /// /// Returns true if a service was marked for eviction.