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