@@ -22,9 +22,16 @@ pub trait Memoizable {
2222 /// Type of any errors that can occur during the construction process.
2323 type Error ;
2424
25+ /// Type of shared data provider
26+ type DataProvider ;
27+
2528 /// Construct a formatter. This maps the [`Self::Args`] type to the actual constructor
2629 /// 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 >
2835 where
2936 Self : std:: marker:: Sized ;
3037}
@@ -92,9 +99,12 @@ pub trait Memoizable {
9299/// /// If the construtor is fallible, than errors can be described here.
93100/// type Error = ();
94101///
102+ /// /// For accessing shared state to construct
103+ /// type DataProvider = ();
104+ ///
95105/// /// This function wires together the `Args` and `Error` type to construct
96106/// /// 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> {
98108/// // Keep track for example purposes that this was constructed.
99109/// increment_constructs();
100110///
@@ -121,13 +131,13 @@ pub trait Memoizable {
121131/// // more details.
122132///
123133/// let result1 = memoizer
124- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
134+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
125135/// intl_example.format(message1)
126136/// });
127137///
128138/// // The memoized instance of `ExampleFormatter` will be re-used.
129139/// let result2 = memoizer
130- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
140+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
131141/// intl_example.format(message2)
132142/// });
133143///
@@ -149,13 +159,13 @@ pub trait Memoizable {
149159///
150160/// // Since the constructor args changed, `ExampleFormatter` will be re-constructed.
151161/// let result1 = memoizer
152- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
162+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
153163/// intl_example.format(message1)
154164/// });
155165///
156166/// // The memoized instance of `ExampleFormatter` will be re-used.
157167/// let result2 = memoizer
158- /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
168+ /// .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), &(), |intl_example| {
159169/// intl_example.format(message2)
160170/// });
161171///
@@ -205,7 +215,12 @@ impl IntlLangMemoizer {
205215 ///
206216 /// U - The callback function. Takes an instance of `I` as the first parameter and
207217 /// 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 >
209224 where
210225 Self : Sized ,
211226 I : Memoizable + ' static ,
@@ -222,7 +237,7 @@ impl IntlLangMemoizer {
222237 let e = match cache. entry ( construct_args. clone ( ) ) {
223238 Entry :: Occupied ( entry) => entry. into_mut ( ) ,
224239 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 ) ?;
226241 entry. insert ( val)
227242 }
228243 } ;
@@ -269,7 +284,8 @@ impl IntlLangMemoizer {
269284/// # impl Memoizable for ExampleFormatter {
270285/// # type Args = (String,);
271286/// # 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> {
273289/// # Ok(Self {
274290/// # lang,
275291/// # prefix: args.0,
@@ -295,7 +311,7 @@ impl IntlLangMemoizer {
295311/// //
296312/// // See `IntlLangMemoizer` for more details on this step.
297313/// 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| {
299315/// intl_example.format(message)
300316/// });
301317///
@@ -312,7 +328,7 @@ impl IntlLangMemoizer {
312328/// let de_de_memoizer: Rc<IntlLangMemoizer> = memoizer.get_for_lang(de_de);
313329///
314330/// 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| {
316332/// intl_example.format(message)
317333/// });
318334///
@@ -380,7 +396,12 @@ mod tests {
380396 impl Memoizable for PluralRules {
381397 type Args = ( PluralRuleType , ) ;
382398 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 > {
384405 Self :: new ( lang, args. 0 )
385406 }
386407 }
@@ -394,7 +415,9 @@ mod tests {
394415 let en_memoizer = memoizer. get_for_lang ( lang. clone ( ) ) ;
395416
396417 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+ } )
398421 . unwrap ( ) ;
399422 assert_eq ! ( result, Ok ( PluralCategory :: OTHER ) ) ;
400423 }
@@ -403,7 +426,9 @@ mod tests {
403426 let en_memoizer = memoizer. get_for_lang ( lang) ;
404427
405428 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+ } )
407432 . unwrap ( ) ;
408433 assert_eq ! ( result, Ok ( PluralCategory :: OTHER ) ) ;
409434 }
@@ -420,7 +445,7 @@ mod tests {
420445 let memoizer = Arc :: clone ( & memoizer) ;
421446 threads. push ( thread:: spawn ( move || {
422447 memoizer
423- . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , |cb| {
448+ . with_try_get :: < PluralRules , _ , _ > ( ( PluralRuleType :: CARDINAL , ) , & ( ) , |cb| {
424449 cb. 0 . select ( 5 )
425450 } )
426451 . expect ( "Failed to get a PluralRules result." )
0 commit comments