Skip to content

Commit dc485ea

Browse files
authored
Add documentation to intl-memoizer (#275)
1 parent ef71027 commit dc485ea

File tree

3 files changed

+311
-25
lines changed

3 files changed

+311
-25
lines changed

intl-memoizer/README.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,44 @@ APIs such as `PluralRules`, DateTimeFormat` etc. between all `FluentBundle` inst
1313
Usage
1414
-----
1515

16+
The following is a high-level example of how this works, for running examples see
17+
the [docs](https://docs.rs/intl-memoizer/)
18+
1619
```rust
17-
use intl_memoizer::{IntlMemoizer, Memoizable};
18-
use unic_langid::langid;
20+
/// Internationalization formatter should implement the Memoizable trait.
21+
impl Memoizable for NumberFormat {
22+
...
23+
}
1924

20-
use intl_pluralrules::{PluralRules, PluralRuleType, PluralCategory};
25+
// The main memoizer has weak references to all of the per-language memoizers.
26+
let mut memoizer = IntlMemoizer::default();
2127

22-
impl Memoizable for PluralRules {
23-
type Args = (PluralRulesType,);
24-
fn construct(lang: LanguageIdentifier, args: Self::Args) -> Self {
25-
Self::new(lang, args.0)
26-
}
27-
}
28+
// The formatter memoziation happens per-locale.
29+
let lang = "en-US".parse().expect("Failed to parse.");
30+
let lang_memoizer: Rc<IntlLangMemoizer> = memoizer.get_for_lang(en_us);
2831

29-
fn main() {
30-
let lang = langid!("en-US");
32+
// Run the formatter
3133

32-
// A single memoizer for all languages
33-
let mut memoizer = IntlMemoizer::new();
34+
let options: NumberFormatOptions {
35+
minimum_fraction_digits: 3,
36+
maximum_fraction_digits: 5,
37+
};
3438

35-
// A RefCell for a particular language to be used in all `FluentBundle`
36-
// instances.
37-
let mut en_us_memoizer = memoizer.get_for_lang(lang.clone());
39+
// Format pi with the options. This will lazily construct the NumberFormat.
40+
let pi = lang_memoizer
41+
.with_try_get::<NumberFormat, _, _>((options,), |nf| nf.format(3.141592653))
42+
.unwrap()
3843

39-
// Per-call borrow
40-
let mut en_us_memoizer_borrow = en_us_memoizer.borrow_mut();
41-
let cb = en_us_memoizer_borrow.get::<PluralRules>((PluralRulesType::Cardinal,));
42-
assert_eq!(cb.select(1), PluralCategory::One);
43-
}
44+
// The example formatter constructs a string with diagnostic information about
45+
// the configuration.
46+
assert_eq!(text, "3.14159");
47+
48+
// Running it again will use the previous formatter.
49+
let two = lang_memoizer
50+
.with_try_get::<NumberFormat, _, _>((options,), |nf| nf.format(2.0))
51+
.unwrap()
4452

53+
assert_eq!(text, "2.000");
4554
```
4655

4756
Get Involved

intl-memoizer/src/concurrent.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1+
//! Contains thread-safe variants.
12
use super::*;
23
use std::sync::Mutex;
34

5+
/// A thread-safe version of the [`intl_memoizer::IntlLangMemoizer`](super::IntlLangMemoizer).
6+
/// See the single-thread version for more documentation.
47
#[derive(Debug)]
58
pub struct IntlLangMemoizer {
69
lang: LanguageIdentifier,
710
map: Mutex<type_map::concurrent::TypeMap>,
811
}
912

1013
impl IntlLangMemoizer {
14+
/// Create a new [`IntlLangMemoizer`] that is unique to a specific [`LanguageIdentifier`]
1115
pub fn new(lang: LanguageIdentifier) -> Self {
1216
Self {
1317
lang,
1418
map: Mutex::new(type_map::concurrent::TypeMap::new()),
1519
}
1620
}
1721

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.
1825
pub fn with_try_get<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
1926
where
2027
Self: Sized,

0 commit comments

Comments
 (0)