From 9efece4f7a64e39851987eadcce5667dc4919dd0 Mon Sep 17 00:00:00 2001 From: Amanda Graven Date: Mon, 11 Jan 2021 14:12:11 +0100 Subject: [PATCH] Remove unnecessary clones --- matrix_sdk_base/src/rooms/normal.rs | 100 +++++++++++++--------------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/matrix_sdk_base/src/rooms/normal.rs b/matrix_sdk_base/src/rooms/normal.rs index f8d3ba58..af65adc3 100644 --- a/matrix_sdk_base/src/rooms/normal.rs +++ b/matrix_sdk_base/src/rooms/normal.rs @@ -221,65 +221,57 @@ impl Room { /// [spec]: /// async fn calculate_name(&self) -> String { - let name; - let alias; - - { + let summary = { let inner = self.inner.read().unwrap(); - name = inner.base_info.name.clone(); - alias = inner.base_info.canonical_alias.clone(); - } - if let Some(name) = name { - let name = name.trim(); - name.to_string() - } else if let Some(alias) = alias { - let alias = alias.alias().trim(); - alias.to_string() + if let Some(name) = &inner.base_info.name { + let name = name.trim(); + return name.to_string(); + } else if let Some(alias) = &inner.base_info.canonical_alias { + let alias = alias.alias().trim(); + return alias.to_string(); + } + inner.summary.clone() + }; + // TODO what should we do here? We have correct counts only if lazy + // loading is used. + let joined = summary.joined_member_count; + let invited = summary.invited_member_count; + let heroes_count = summary.heroes.len() as u64; + + let is_own_member = |m: &RoomMember| m.user_id() == &*self.own_user_id; + let is_own_user_id = |u: &str| u == self.own_user_id().as_str(); + + let members: Vec = if summary.heroes.is_empty() { + self.active_members() + .await + .filter(|m| future::ready(!is_own_member(m))) + .take(5) + .collect() + .await } else { - // TODO what should we do here? We have correct counts only if lazy - // loading is used. - let summary = { self.inner.read().unwrap().summary.clone() }; + stream::iter(summary.heroes.iter()) + .filter(|u| future::ready(!is_own_user_id(u))) + .filter_map(|u| async move { + let user_id = UserId::try_from(u.as_str()).ok()?; + self.get_member(&user_id).await + }) + .collect() + .await + }; - let joined = summary.joined_member_count; - let invited = summary.invited_member_count; - let heroes_count = summary.heroes.len() as u64; + info!( + "Calculating name for {}, own user {} hero count {} heroes {:#?}", + self.room_id(), + self.own_user_id, + heroes_count, + summary.heroes + ); - let is_own_member = |m: &RoomMember| m.user_id() == &*self.own_user_id; - let is_own_user_id = |u: &str| u == self.own_user_id().as_str(); - - let members: Vec = if summary.heroes.is_empty() { - self.active_members() - .await - .filter(|m| future::ready(!is_own_member(m))) - .take(5) - .collect() - .await - } else { - stream::iter(summary.heroes.iter()) - .filter(|u| future::ready(!is_own_user_id(u))) - .filter_map(|u| async move { - let user_id = UserId::try_from(u.as_str()).ok()?; - self.get_member(&user_id).await - }) - .collect() - .await - }; - - info!( - "Calculating name for {}, own user {} hero count {} heroes {:#?}", - self.room_id(), - self.own_user_id, - heroes_count, - summary.heroes - ); - - self.inner - .read() - .unwrap() - .base_info - .calculate_room_name(joined, invited, members) - } + let inner = self.inner.read().unwrap(); + inner + .base_info + .calculate_room_name(joined, invited, members) } pub(crate) fn clone_info(&self) -> RoomInfo {