From e43a3697a17f3b8f92aa4063a4b7289e286f5370 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Thu, 16 Sep 2021 16:17:30 +0200 Subject: [PATCH] rpc: rewrite loops using iterators --- rpc/src/rpc.rs | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/rpc/src/rpc.rs b/rpc/src/rpc.rs index a40b25c3023300..71be7b98681fc8 100644 --- a/rpc/src/rpc.rs +++ b/rpc/src/rpc.rs @@ -355,12 +355,17 @@ impl JsonRpcRequestProcessor { let encoding = config.encoding.unwrap_or(UiAccountEncoding::Base64); check_slice_and_encoding(&encoding, config.data_slice.is_some())?; - let mut accounts: Vec> = Vec::with_capacity(pubkeys.len()); - for pubkey in pubkeys { - let response_account = - get_encoded_account(&bank, &pubkey, encoding, config.data_slice)?; - accounts.push(response_account) - } + let accounts = pubkeys + .into_iter() + .map(|pubkey| { + Ok(get_encoded_account( + &bank, + &pubkey, + encoding, + config.data_slice, + )?) + }) + .collect::>>()?; Ok(new_response(&bank, accounts)) } @@ -394,19 +399,21 @@ impl JsonRpcRequestProcessor { self.get_filtered_program_accounts(&bank, program_id, filters)? } }; - let result = - if program_id == &spl_token_id_v2_0() && encoding == UiAccountEncoding::JsonParsed { - get_parsed_token_accounts(bank.clone(), keyed_accounts.into_iter()).collect() - } else { - let mut encoded_accounts = Vec::with_capacity(keyed_accounts.len()); - for (pubkey, account) in keyed_accounts { - encoded_accounts.push(RpcKeyedAccount { + let result = if program_id == &spl_token_id_v2_0() + && encoding == UiAccountEncoding::JsonParsed + { + get_parsed_token_accounts(bank.clone(), keyed_accounts.into_iter()).collect() + } else { + keyed_accounts + .into_iter() + .map(|(pubkey, account)| { + Ok(RpcKeyedAccount { pubkey: pubkey.to_string(), account: encode_account(&account, &pubkey, encoding, data_slice_config)?, - }); - } - encoded_accounts - }; + }) + }) + .collect::>>()? + }; Ok(result).map(|result| match with_context { true => OptionalContext::Context(new_response(&bank, result)), false => OptionalContext::NoContext(result), @@ -2825,10 +2832,10 @@ pub mod rpc_accounts { max_multiple_accounts ))); } - let mut pubkeys: Vec = Vec::with_capacity(pubkey_strs.len()); - for pubkey_str in pubkey_strs { - pubkeys.push(verify_pubkey(&pubkey_str)?); - } + let pubkeys = pubkey_strs + .into_iter() + .map(|pubkey_str| Ok(verify_pubkey(&pubkey_str)?)) + .collect::>>()?; meta.get_multiple_accounts(pubkeys, config) }