From a4e1a9ac980f8a9ab0c9b30350349e1b3c7de48c Mon Sep 17 00:00:00 2001 From: Brooks Date: Tue, 27 Feb 2024 20:01:29 -0500 Subject: [PATCH] Adds AccountsIndex::get_account_info_with_and_then() (#35336) --- accounts-db/src/accounts_index.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/accounts-db/src/accounts_index.rs b/accounts-db/src/accounts_index.rs index 7a5c75669..b021881d4 100644 --- a/accounts-db/src/accounts_index.rs +++ b/accounts-db/src/accounts_index.rs @@ -1143,18 +1143,30 @@ impl + Into> AccountsIndex { ancestors: Option<&Ancestors>, max_root: Option, should_add_to_in_mem_cache: bool, - mut callback: impl FnMut((Slot, T)) -> R, + callback: impl FnOnce((Slot, T)) -> R, ) -> Option { self.get_and_then(pubkey, |entry| { let callback_result = entry.and_then(|entry| { - let slot_list = entry.slot_list.read().unwrap(); - self.latest_slot(ancestors, &slot_list, max_root) - .map(|found_index| callback(slot_list[found_index])) + self.get_account_info_with_and_then(entry, ancestors, max_root, callback) }); (should_add_to_in_mem_cache, callback_result) }) } + /// Gets the account info (and slot) in `entry`, with `ancestors` and `max_root`, + /// and applies `callback` to it + fn get_account_info_with_and_then( + &self, + entry: &AccountMapEntryInner, + ancestors: Option<&Ancestors>, + max_root: Option, + callback: impl FnOnce((Slot, T)) -> R, + ) -> Option { + let slot_list = entry.slot_list.read().unwrap(); + self.latest_slot(ancestors, &slot_list, max_root) + .map(|found_index| callback(slot_list[found_index])) + } + /// Gets the index's entry for `pubkey` and clones it /// /// Prefer `get_and_then()` whenever possible.