@@ -22,9 +22,16 @@ pub trait Memoizable {
22
22
/// Type of any errors that can occur during the construction process.
23
23
type Error ;
24
24
25
+ /// Type of shared data provider
26
+ type DataProvider ;
27
+
25
28
/// Construct a formatter. This maps the [`Self::Args`] type to the actual constructor
26
29
/// for an intl formatter.
27
- fn construct ( lang : LanguageIdentifier , args : Self :: Args ) -> Result < Self , Self :: Error >
30
+ fn construct (
31
+ lang : LanguageIdentifier ,
32
+ args : Self :: Args ,
33
+ data_provider : & Self :: DataProvider ,
34
+ ) -> Result < Self , Self :: Error >
28
35
where
29
36
Self : std:: marker:: Sized ;
30
37
}
@@ -92,9 +99,12 @@ pub trait Memoizable {
92
99
/// /// If the construtor is fallible, than errors can be described here.
93
100
/// type Error = ();
94
101
///
102
+ /// /// For accessing shared state to construct
103
+ /// type DataProvider = ();
104
+ ///
95
105
/// /// This function wires together the `Args` and `Error` type to construct
96
106
/// /// the intl API. In our example, there is
97
- /// fn construct(lang: LanguageIdentifier, args: Self::Args) -> Result<Self, Self::Error> {
107
+ /// fn construct(lang: LanguageIdentifier, args: Self::Args, data_provider: &Self::DataProvider ) -> Result<Self, Self::Error> {
98
108
/// // Keep track for example purposes that this was constructed.
99
109
/// increment_constructs();
100
110
///
@@ -121,13 +131,13 @@ pub trait Memoizable {
121
131
/// // more details.
122
132
///
123
133
/// let result1 = memoizer
124
- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
134
+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
125
135
/// intl_example.format(message1)
126
136
/// });
127
137
///
128
138
/// // The memoized instance of `ExampleFormatter` will be re-used.
129
139
/// let result2 = memoizer
130
- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
140
+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
131
141
/// intl_example.format(message2)
132
142
/// });
133
143
///
@@ -149,13 +159,13 @@ pub trait Memoizable {
149
159
///
150
160
/// // Since the constructor args changed, `ExampleFormatter` will be re-constructed.
151
161
/// let result1 = memoizer
152
- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
162
+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
153
163
/// intl_example.format(message1)
154
164
/// });
155
165
///
156
166
/// // The memoized instance of `ExampleFormatter` will be re-used.
157
167
/// let result2 = memoizer
158
- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
168
+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
159
169
/// intl_example.format(message2)
160
170
/// });
161
171
///
@@ -205,7 +215,12 @@ impl IntlLangMemoizer {
205
215
///
206
216
/// U - The callback function. Takes an instance of `I` as the first parameter and
207
217
/// returns the R value.
208
- pub fn with_try_get < I , R , U > ( & self , construct_args : I :: Args , callback : U ) -> Result < R , I :: Error >
218
+ pub fn with_try_get < I , R , U > (
219
+ & self ,
220
+ construct_args : I :: Args ,
221
+ data_provider : & I :: DataProvider ,
222
+ callback : U ,
223
+ ) -> Result < R , I :: Error >
209
224
where
210
225
Self : Sized ,
211
226
I : Memoizable + ' static ,
@@ -222,7 +237,7 @@ impl IntlLangMemoizer {
222
237
let e = match cache. entry ( construct_args. clone ( ) ) {
223
238
Entry :: Occupied ( entry) => entry. into_mut ( ) ,
224
239
Entry :: Vacant ( entry) => {
225
- let val = I :: construct ( self . lang . clone ( ) , construct_args) ?;
240
+ let val = I :: construct ( self . lang . clone ( ) , construct_args, data_provider ) ?;
226
241
entry. insert ( val)
227
242
}
228
243
} ;
@@ -269,7 +284,8 @@ impl IntlLangMemoizer {
269
284
/// # impl Memoizable for ExampleFormatter {
270
285
/// # type Args = (String,);
271
286
/// # type Error = ();
272
- /// # fn construct(lang: LanguageIdentifier, args: Self::Args) -> Result<Self, Self::Error> {
287
+ /// # type DataProvider = ();
288
+ /// # fn construct(lang: LanguageIdentifier, args: Self::Args, data_provider: &Self::DataProvider) -> Result<Self, Self::Error> {
273
289
/// # Ok(Self {
274
290
/// # lang,
275
291
/// # prefix: args.0,
@@ -295,7 +311,7 @@ impl IntlLangMemoizer {
295
311
/// //
296
312
/// // See `IntlLangMemoizer` for more details on this step.
297
313
/// let en_us_result = en_us_memoizer
298
- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
314
+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
299
315
/// intl_example.format(message)
300
316
/// });
301
317
///
@@ -312,7 +328,7 @@ impl IntlLangMemoizer {
312
328
/// let de_de_memoizer: Rc<IntlLangMemoizer> = memoizer.get_for_lang(de_de);
313
329
///
314
330
/// let de_de_result = de_de_memoizer
315
- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
331
+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
316
332
/// intl_example.format(message)
317
333
/// });
318
334
///
@@ -380,7 +396,12 @@ mod tests {
380
396
impl Memoizable for PluralRules {
381
397
type Args = ( PluralRuleType , ) ;
382
398
type Error = & ' static str ;
383
- fn construct ( lang : LanguageIdentifier , args : Self :: Args ) -> Result < Self , Self :: Error > {
399
+ type DataProvider = ( ) ;
400
+ fn construct (
401
+ lang : LanguageIdentifier ,
402
+ args : Self :: Args ,
403
+ _: & Self :: DataProvider ,
404
+ ) -> Result < Self , Self :: Error > {
384
405
Self :: new ( lang, args. 0 )
385
406
}
386
407
}
@@ -394,7 +415,9 @@ mod tests {
394
415
let en_memoizer = memoizer. get_for_lang ( lang. clone ( ) ) ;
395
416
396
417
let result = en_memoizer
397
- . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , |cb| cb. 0 . select ( 5 ) )
418
+ . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , & ( ) , |cb| {
419
+ cb. 0 . select ( 5 )
420
+ } )
398
421
. unwrap ( ) ;
399
422
assert_eq ! ( result, Ok ( PluralCategory :: OTHER ) ) ;
400
423
}
@@ -403,7 +426,9 @@ mod tests {
403
426
let en_memoizer = memoizer. get_for_lang ( lang) ;
404
427
405
428
let result = en_memoizer
406
- . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , |cb| cb. 0 . select ( 5 ) )
429
+ . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , & ( ) , |cb| {
430
+ cb. 0 . select ( 5 )
431
+ } )
407
432
. unwrap ( ) ;
408
433
assert_eq ! ( result, Ok ( PluralCategory :: OTHER ) ) ;
409
434
}
@@ -420,7 +445,7 @@ mod tests {
420
445
let memoizer = Arc :: clone ( & memoizer) ;
421
446
threads. push ( thread:: spawn ( move || {
422
447
memoizer
423
- . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , |cb| {
448
+ . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , & ( ) , |cb| {
424
449
cb. 0 . select ( 5 )
425
450
} )
426
451
. expect ( "Failed to get a PluralRules result." )
0 commit comments