From a341cddd0c5cbbfa81974a8751d00d8f853f3ac3 Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Thu, 13 Oct 2022 19:31:08 +1100 Subject: [PATCH] ready-cache: Allow iteration over ready services 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 8070d7807..282f2bb83 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.