| 1 | //! Contains thread-safe variants. | 
| 2 | use super::*; | 
|---|
| 3 | use std::sync::Mutex; | 
|---|
| 4 |  | 
|---|
| 5 | /// A thread-safe version of the [`intl_memoizer::IntlLangMemoizer`](super::IntlLangMemoizer). | 
|---|
| 6 | /// See the single-thread version for more documentation. | 
|---|
| 7 | #[ derive(Debug)] | 
|---|
| 8 | pub struct IntlLangMemoizer { | 
|---|
| 9 | lang: LanguageIdentifier, | 
|---|
| 10 | map: Mutex<type_map::concurrent::TypeMap>, | 
|---|
| 11 | } | 
|---|
| 12 |  | 
|---|
| 13 | impl IntlLangMemoizer { | 
|---|
| 14 | /// Create a new [`IntlLangMemoizer`] that is unique to a specific [`LanguageIdentifier`] | 
|---|
| 15 | pub fn new(lang: LanguageIdentifier) -> Self { | 
|---|
| 16 | Self { | 
|---|
| 17 | lang, | 
|---|
| 18 | map: Mutex::new(type_map::concurrent::TypeMap::new()), | 
|---|
| 19 | } | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | /// Lazily initialize and run a formatter. See | 
|---|
| 23 | /// [`intl_memoizer::IntlLangMemoizer::with_try_get`](../struct.IntlLangMemoizer.html#method.with_try_get) | 
|---|
| 24 | /// for documentation. | 
|---|
| 25 | pub fn with_try_get<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error> | 
|---|
| 26 | where | 
|---|
| 27 | Self: Sized, | 
|---|
| 28 | I: Memoizable + Sync + Send + 'static, | 
|---|
| 29 | I::Args: Send + Sync + 'static, | 
|---|
| 30 | U: FnOnce(&I) -> R, | 
|---|
| 31 | { | 
|---|
| 32 | let mut map = self.map.lock().unwrap(); | 
|---|
| 33 | let cache = map | 
|---|
| 34 | .entry::<HashMap<I::Args, I>>() | 
|---|
| 35 | .or_insert_with(HashMap::new); | 
|---|
| 36 |  | 
|---|
| 37 | let e = match cache.entry(args.clone()) { | 
|---|
| 38 | Entry::Occupied(entry) => entry.into_mut(), | 
|---|
| 39 | Entry::Vacant(entry) => { | 
|---|
| 40 | let val = I::construct(self.lang.clone(), args)?; | 
|---|
| 41 | entry.insert(val) | 
|---|
| 42 | } | 
|---|
| 43 | }; | 
|---|
| 44 | Ok(cb(e)) | 
|---|
| 45 | } | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|