From f0533e07ef1cf2257e5250be988226630cfcf8f1 Mon Sep 17 00:00:00 2001 From: strawberry Date: Tue, 28 May 2024 15:59:08 -0400 Subject: [PATCH] fed: remove unnecessary mutables, use with_capacity in couple more places Signed-off-by: strawberry --- src/api/server_server.rs | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/api/server_server.rs b/src/api/server_server.rs index 7eb4d7cf..277c971d 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -88,15 +88,15 @@ pub(crate) async fn get_server_version_route( // Response type for this endpoint is Json because we need to calculate a // signature for the response pub(crate) async fn get_server_keys_route() -> Result { - let mut verify_keys: BTreeMap = BTreeMap::new(); - verify_keys.insert( + let verify_keys: BTreeMap = BTreeMap::from([( format!("ed25519:{}", services().globals.keypair().version()) .try_into() .expect("found invalid server signing keys in DB"), VerifyKey { key: Base64::new(services().globals.keypair().public_key().to_vec()), }, - ); + )]); + let mut response = serde_json::from_slice( get_server_keys::v2::Response { server_key: Raw::new(&ServerSigningKeys { @@ -375,14 +375,11 @@ pub(crate) async fn send_transaction_message_route( .any(|member| member.server_name() == user_id.server_name()) { for event_id in &user_updates.event_ids { - let mut user_receipts = BTreeMap::new(); - user_receipts.insert(user_id.clone(), user_updates.data.clone()); + let user_receipts = BTreeMap::from([(user_id.clone(), user_updates.data.clone())]); - let mut receipts = BTreeMap::new(); - receipts.insert(ReceiptType::Read, user_receipts); + let receipts = BTreeMap::from([(ReceiptType::Read, user_receipts)]); - let mut receipt_content = BTreeMap::new(); - receipt_content.insert(event_id.to_owned(), receipts); + let receipt_content = BTreeMap::from([(event_id.to_owned(), receipts)]); let event = ReceiptEvent { content: ReceiptEventContent(receipt_content), @@ -633,13 +630,17 @@ pub(crate) async fn get_backfill_route(body: Ruma) -> .max() .ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Event not found."))?; - let limit = body.limit.min(uint!(100)); + let limit = body + .limit + .min(uint!(100)) + .try_into() + .expect("UInt could not be converted to usize"); let all_events = services() .rooms .timeline .pdus_until(user_id!("@doesntmatter:conduit.rs"), &body.room_id, until)? - .take(limit.try_into().unwrap()); + .take(limit); let events = all_events .filter_map(Result::ok) @@ -685,11 +686,17 @@ pub(crate) async fn get_missing_events_route( .event_handler .acl_check(origin, &body.room_id)?; - let mut queued_events = body.latest_events.clone(); - let mut events = Vec::new(); + let limit = body + .limit + .try_into() + .expect("UInt could not be converted to usize"); - let mut i = 0; - while i < queued_events.len() && events.len() < u64::from(body.limit) as usize { + let mut queued_events = body.latest_events.clone(); + // the vec will never have more entries the limit + let mut events = Vec::with_capacity(limit); + + let mut i: usize = 0; + while i < queued_events.len() && events.len() < limit { if let Some(pdu) = services().rooms.timeline.get_pdu_json(&queued_events[i])? { let room_id_str = pdu .get("room_id")