1 | use intl_memoizer::{concurrent::IntlLangMemoizer, Memoizable}; |
2 | use rustc_hash::FxHashMap; |
3 | use unic_langid::LanguageIdentifier; |
4 | |
5 | use crate::bundle::FluentBundle; |
6 | use crate::memoizer::MemoizerKind; |
7 | use crate::types::FluentType; |
8 | |
9 | impl<R> FluentBundle<R, IntlLangMemoizer> { |
10 | /// A constructor analogous to [`FluentBundle::new`] but operating |
11 | /// on a concurrent version of [`IntlLangMemoizer`] over [`Mutex`](std::sync::Mutex). |
12 | /// |
13 | /// # Example |
14 | /// |
15 | /// ``` |
16 | /// use fluent_bundle::bundle::FluentBundle; |
17 | /// use fluent_bundle::FluentResource; |
18 | /// use unic_langid::langid; |
19 | /// |
20 | /// let langid_en = langid!("en-US" ); |
21 | /// let mut bundle: FluentBundle<FluentResource, _> = |
22 | /// FluentBundle::new_concurrent(vec![langid_en]); |
23 | /// ``` |
24 | pub fn new_concurrent(locales: Vec<LanguageIdentifier>) -> Self { |
25 | let first_locale = locales.get(0).cloned().unwrap_or_default(); |
26 | Self { |
27 | locales, |
28 | resources: vec![], |
29 | entries: FxHashMap::default(), |
30 | intls: IntlLangMemoizer::new(first_locale), |
31 | use_isolating: true, |
32 | transform: None, |
33 | formatter: None, |
34 | } |
35 | } |
36 | } |
37 | |
38 | impl MemoizerKind for IntlLangMemoizer { |
39 | fn new(lang: LanguageIdentifier) -> Self |
40 | where |
41 | Self: Sized, |
42 | { |
43 | Self::new(lang) |
44 | } |
45 | |
46 | fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error> |
47 | where |
48 | Self: Sized, |
49 | I: Memoizable + Send + Sync + 'static, |
50 | I::Args: Send + Sync + 'static, |
51 | U: FnOnce(&I) -> R, |
52 | { |
53 | self.with_try_get(args, cb) |
54 | } |
55 | |
56 | fn stringify_value(&self, value: &dyn FluentType) -> std::borrow::Cow<'static, str> { |
57 | value.as_string_threadsafe(self) |
58 | } |
59 | } |
60 | |