Struct jumpy::prelude::intl_memoizer::IntlLangMemoizer
pub struct IntlLangMemoizer {
pub(crate) lang: LanguageIdentifier,
pub(crate) map: RefCell<TypeMap>,
}
Expand description
The IntlLangMemoizer
can memoize multiple constructed internationalization
formatters, and their configuration for a single locale. For instance, given “en-US”,
a memorizer could retain 3 DateTimeFormat instances, and a PluralRules.
For memoizing with multiple locales, see IntlMemoizer
.
§Example
The code example does the following steps:
- Create a static counter
- Create an
ExampleFormatter
- Implement
Memoizable
forExampleFormatter
. - Use
IntlLangMemoizer::with_try_get
to runExampleFormatter::format
- Demonstrate the memoization using the static counter
use intl_memoizer::{IntlLangMemoizer, Memoizable};
use unic_langid::LanguageIdentifier;
// Create a static counter so that we can demonstrate the side effects of when
// the memoizer re-constructs an API.
static mut INTL_EXAMPLE_CONSTRUCTS: u32 = 0;
fn increment_constructs() {
unsafe {
INTL_EXAMPLE_CONSTRUCTS += 1;
}
}
fn get_constructs_count() -> u32 {
unsafe { INTL_EXAMPLE_CONSTRUCTS }
}
/// Create an example formatter, that doesn't really do anything useful. In a real
/// implementation, this could be a PluralRules or DateTimeFormat struct.
struct ExampleFormatter {
lang: LanguageIdentifier,
/// This is here to show how to initiate the API with an argument.
prefix: String,
}
impl ExampleFormatter {
/// Perform an example format by printing information about the formatter
/// configuration, and the arguments passed into the individual format operation.
fn format(&self, example_string: &str) -> String {
format!(
"{} lang({}) string({})",
self.prefix, self.lang, example_string
)
}
}
/// Multiple classes of structs may be add1ed to the memoizer, with the restriction
/// that they must implement the `Memoizable` trait.
impl Memoizable for ExampleFormatter {
/// The arguments will be passed into the constructor. Here a single `String`
/// will be used as a prefix to the formatting operation.
type Args = (String,);
/// If the constructor is fallible, than errors can be described here.
type Error = ();
/// This function wires together the `Args` and `Error` type to construct
/// the intl API. In our example, there is
fn construct(lang: LanguageIdentifier, args: Self::Args) -> Result<Self, Self::Error> {
// Keep track for example purposes that this was constructed.
increment_constructs();
Ok(Self {
lang,
prefix: args.0,
})
}
}
// The following demonstrates how these structs are actually used with the memoizer.
// Construct a new memoizer.
let lang = "en-US".parse().expect("Failed to parse.");
let memoizer = IntlLangMemoizer::new(lang);
// These arguments are passed into the constructor for `ExampleFormatter`.
let construct_args = (String::from("prefix:"),);
let message1 = "The format operation will run";
let message2 = "ExampleFormatter will be re-used, when a second format is run";
// Run `IntlLangMemoizer::with_try_get`. The name of the method means "with" an
// intl formatter, "try and get" the result. See the method documentation for
// more details.
let result1 = memoizer
.with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
intl_example.format(message1)
});
// The memoized instance of `ExampleFormatter` will be re-used.
let result2 = memoizer
.with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
intl_example.format(message2)
});
assert_eq!(
result1.unwrap(),
"prefix: lang(en-US) string(The format operation will run)"
);
assert_eq!(
result2.unwrap(),
"prefix: lang(en-US) string(ExampleFormatter will be re-used, when a second format is run)"
);
assert_eq!(
get_constructs_count(),
1,
"The constructor was only run once."
);
let construct_args = (String::from("re-init:"),);
// Since the constructor args changed, `ExampleFormatter` will be re-constructed.
let result1 = memoizer
.with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
intl_example.format(message1)
});
// The memoized instance of `ExampleFormatter` will be re-used.
let result2 = memoizer
.with_try_get::<ExampleFormatter, _, _>(construct_args.clone(), |intl_example| {
intl_example.format(message2)
});
assert_eq!(
result1.unwrap(),
"re-init: lang(en-US) string(The format operation will run)"
);
assert_eq!(
result2.unwrap(),
"re-init: lang(en-US) string(ExampleFormatter will be re-used, when a second format is run)"
);
assert_eq!(
get_constructs_count(),
2,
"The constructor was invalidated and ran again."
);
Fields§
§lang: LanguageIdentifier
§map: RefCell<TypeMap>
Implementations§
§impl IntlLangMemoizer
impl IntlLangMemoizer
pub fn new(lang: LanguageIdentifier) -> IntlLangMemoizer
pub fn new(lang: LanguageIdentifier) -> IntlLangMemoizer
Create a new IntlLangMemoizer
that is unique to a specific
LanguageIdentifier
pub fn with_try_get<I, R, U>(
&self,
construct_args: <I as Memoizable>::Args,
callback: U,
) -> Result<R, <I as Memoizable>::Error>
pub fn with_try_get<I, R, U>( &self, construct_args: <I as Memoizable>::Args, callback: U, ) -> Result<R, <I as Memoizable>::Error>
with_try_get
means with
an internationalization formatter, try
and get
a result.
The (potentially expensive) constructor for the formatter (such as PluralRules or
DateTimeFormat) will be memoized and only constructed once for a given
construct_args
. After that the format operation can be run multiple times
inexpensively.
The first generic argument I
must be provided, but the R
and U
will be
deduced by the typing of the callback
argument that is provided.
I - The memoizable intl object, for instance a PluralRules
instance. This
must implement the Memoizable trait.
R - The return result from the callback U
.
U - The callback function. Takes an instance of I
as the first parameter and
returns the R value.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for IntlLangMemoizer
impl !RefUnwindSafe for IntlLangMemoizer
impl !Send for IntlLangMemoizer
impl !Sync for IntlLangMemoizer
impl Unpin for IntlLangMemoizer
impl !UnwindSafe for IntlLangMemoizer
Blanket Implementations§
§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
[ShaderType
] for self
. When used in [AsBindGroup
]
derives, it is safe to assume that all images in self
exist.§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.