Struct intl_memoizer::IntlMemoizer

source ·
pub struct IntlMemoizer { /* private fields */ }
Expand description

IntlMemoizer is designed to handle lazily-initialized references to internationalization formatters.

Constructing a new formatter is often expensive in terms of memory and performance, and the instance is often read-only during its lifetime. The format operations in comparison are relatively cheap.

Because of this relationship, it can be helpful to memoize the constructors, and re-use them across multiple format operations. This strategy is used where all instances of intl APIs such as PluralRules, DateTimeFormat etc. are memoized between all FluentBundle instances.

§Example

For a more complete example of the memoization, see the IntlLangMemoizer documentation. This example provides a higher-level overview.

let mut memoizer = IntlMemoizer::default();

// The memoziation happens per-locale.
let en_us = "en-US".parse().expect("Failed to parse.");
let en_us_memoizer: Rc<IntlLangMemoizer> = memoizer.get_for_lang(en_us);

// These arguments are passed into the constructor for `ExampleFormatter`. The
// construct_args will be used for determining the memoization, but the message
// can be different and re-use the constructed instance.
let construct_args = (String::from("prefix:"),);
let message = "The format operation will run";

// Use the `ExampleFormatter` from the `IntlLangMemoizer` example. It returns a
// string that demonstrates the configuration of the formatter. This step will
// construct a new formatter if needed, and run the format operation.
//
// See `IntlLangMemoizer` for more details on this step.
let en_us_result = en_us_memoizer
    .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
        intl_example.format(message)
    });

// The example formatter constructs a string with diagnostic information about
// the configuration.
assert_eq!(
    en_us_result.unwrap(),
    "prefix: lang(en-US) string(The format operation will run)"
);

// The process can be repeated for a new locale.

let de_de = "de-DE".parse().expect("Failed to parse.");
let de_de_memoizer: Rc<IntlLangMemoizer> = memoizer.get_for_lang(de_de);

let de_de_result = de_de_memoizer
    .with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
        intl_example.format(message)
    });

assert_eq!(
    de_de_result.unwrap(),
    "prefix: lang(de-DE) string(The format operation will run)"
);

Implementations§

source§

impl IntlMemoizer

source

pub fn get_for_lang(&mut self, lang: LanguageIdentifier) -> Rc<IntlLangMemoizer>

Get a IntlLangMemoizer for a given language. If one does not exist for a locale, it will be constructed and weakly retained. See IntlLangMemoizer for more detailed documentation how to use it.

Trait Implementations§

source§

impl Default for IntlMemoizer

source§

fn default() -> IntlMemoizer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.