From 49e85934536f211daea24d6f45a2a445da022894 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> Date: Tue, 13 Aug 2024 22:18:15 -0400 Subject: [PATCH] Reduce cloning and simplify some iteration --- leptos-fluent/src/lib.rs | 52 +++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/leptos-fluent/src/lib.rs b/leptos-fluent/src/lib.rs index 4238f8d7..7b1efb94 100644 --- a/leptos-fluent/src/lib.rs +++ b/leptos-fluent/src/lib.rs @@ -284,8 +284,8 @@ use fluent_templates::{ LanguageIdentifier, StaticLoader, }; use leptos::{ - component, use_context, Attribute, IntoAttribute, IntoView, Oco, RwSignal, - Signal, SignalGet, SignalSet, SignalWith, + component, use_context, with, Attribute, IntoAttribute, IntoView, Oco, + RwSignal, Signal, SignalGet, SignalSet, }; #[cfg(feature = "ssr")] use leptos::{view, SignalGetUntracked}; @@ -466,10 +466,12 @@ impl I18n { impl core::fmt::Debug for I18n { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("I18n") - .field("language", &self.language.get()) + let language = self.language; + with!(|language| f + .debug_struct("I18n") + .field("language", language) .field("languages", &self.languages) - .finish() + .finish()) } } @@ -576,16 +578,15 @@ pub fn i18n() -> I18n { #[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", skip_all))] #[doc(hidden)] pub fn tr_impl(text_id: &str) -> String { - let i18n = expect_i18n(); - let lang_id = i18n.language.get().id.clone(); - let found = i18n.translations.with(|translations| { - for tr in translations { - if let Some(result) = tr.try_lookup(&lang_id, text_id) { - return Some(result); - } - } - - None + let I18n { + language, + translations, + .. + } = expect_i18n(); + let found = with!(|translations, language| { + translations + .iter() + .find_map(|tr| tr.try_lookup(&language.id, text_id)) }); #[cfg(feature = "tracing")] @@ -618,18 +619,15 @@ pub fn tr_with_args_impl( text_id: &str, args: &std::collections::HashMap, ) -> String { - let i18n = expect_i18n(); - let lang_id = i18n.language.get().id.clone(); - let found = i18n.translations.with(|translations| { - for tr in translations { - if let Some(result) = - tr.try_lookup_with_args(&lang_id, text_id, args) - { - return Some(result); - } - } - - None + let I18n { + language, + translations, + .. + } = expect_i18n(); + let found = with!(|translations, language| { + translations + .iter() + .find_map(|tr| tr.try_lookup_with_args(&language.id, text_id, args)) }); #[cfg(feature = "tracing")]