pub struct LocaleFallbackProvider<P> {
inner: P,
fallbacker: LocaleFallbacker,
}
Expand description
A data provider wrapper that performs locale fallback. This enables arbitrary locales to be handled at runtime.
§Examples
use icu_locid::langid;
use icu_provider::prelude::*;
use icu_provider::hello_world::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;
let req = DataRequest {
locale: &langid!("ja-JP").into(),
metadata: Default::default(),
};
// The provider does not have data for "ja-JP":
DataProvider::<HelloWorldV1Marker>::load(&provider, req).expect_err("No fallback");
// But if we wrap the provider in a fallback provider...
let provider = LocaleFallbackProvider::try_new_unstable(provider)
.expect("Fallback data present");
// ...then we can load "ja-JP" based on "ja" data
let response =
DataProvider::<HelloWorldV1Marker>::load(&provider, req).expect("successful with vertical fallback");
assert_eq!(
response.metadata.locale.unwrap(),
langid!("ja").into(),
);
assert_eq!(
response.payload.unwrap().get().message,
"こんにちは世界",
);
Fields§
§inner: P
§fallbacker: LocaleFallbacker
Implementations§
source§impl<P> LocaleFallbackProvider<P>
impl<P> LocaleFallbackProvider<P>
sourcepub fn try_new_unstable(provider: P) -> Result<Self, DataError>
pub fn try_new_unstable(provider: P) -> Result<Self, DataError>
Create a LocaleFallbackProvider
by wrapping another data provider and then loading
fallback data from it.
If the data provider being wrapped does not contain fallback data, use
LocaleFallbackProvider::new_with_fallbacker
.
source§impl<P> LocaleFallbackProvider<P>where
P: AnyProvider,
impl<P> LocaleFallbackProvider<P>where
P: AnyProvider,
sourcepub fn try_new_with_any_provider(provider: P) -> Result<Self, DataError>
pub fn try_new_with_any_provider(provider: P) -> Result<Self, DataError>
Create a LocaleFallbackProvider
by wrapping another data provider and then loading
fallback data from it.
If the data provider being wrapped does not contain fallback data, use
LocaleFallbackProvider::new_with_fallbacker
.
source§impl<P> LocaleFallbackProvider<P>
impl<P> LocaleFallbackProvider<P>
sourcepub fn new_with_fallbacker(provider: P, fallbacker: LocaleFallbacker) -> Self
pub fn new_with_fallbacker(provider: P, fallbacker: LocaleFallbacker) -> Self
Wrap a provider with an arbitrary fallback engine.
This relaxes the requirement that the wrapped provider contains its own fallback data.
§Examples
use icu_locid::langid;
use icu_locid_transform::LocaleFallbacker;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;
let provider = HelloWorldProvider;
let req = DataRequest {
locale: &langid!("de-CH").into(),
metadata: Default::default(),
};
// There is no "de-CH" data in the `HelloWorldProvider`
DataProvider::<HelloWorldV1Marker>::load(&provider, req)
.expect_err("No data for de-CH");
// `HelloWorldProvider` does not contain fallback data,
// but we can construct a fallbacker with `icu_locid_transform`'s
// compiled data.
let provider = LocaleFallbackProvider::new_with_fallbacker(
provider,
LocaleFallbacker::new().static_to_owned(),
);
// Now we can load the "de-CH" data via fallback to "de".
let german_hello_world: DataPayload<HelloWorldV1Marker> = provider
.load(req)
.expect("Loading should succeed")
.take_payload()
.expect("Data should be present");
assert_eq!("Hallo Welt", german_hello_world.get().message);
sourcepub fn into_inner(self) -> P
pub fn into_inner(self) -> P
Returns ownership of the inner provider to the caller.
sourcefn run_fallback<F1, F2, R>(
&self,
key: DataKey,
base_req: DataRequest<'_>,
f1: F1,
f2: F2,
) -> Result<R, DataError>where
F1: FnMut(DataRequest<'_>) -> Result<R, DataError>,
F2: FnMut(&mut R) -> &mut DataResponseMetadata,
fn run_fallback<F1, F2, R>(
&self,
key: DataKey,
base_req: DataRequest<'_>,
f1: F1,
f2: F2,
) -> Result<R, DataError>where
F1: FnMut(DataRequest<'_>) -> Result<R, DataError>,
F2: FnMut(&mut R) -> &mut DataResponseMetadata,
Run the fallback algorithm with the data request using the inner data provider. Internal function; external clients should use one of the trait impls below.
Function arguments:
- F1 should perform a data load for a single DataRequest and return the result of it
- F2 should map from the provider-specific response type to DataResponseMetadata
Trait Implementations§
source§impl<P> AnyProvider for LocaleFallbackProvider<P>where
P: AnyProvider,
impl<P> AnyProvider for LocaleFallbackProvider<P>where
P: AnyProvider,
source§fn load_any(
&self,
key: DataKey,
base_req: DataRequest<'_>,
) -> Result<AnyResponse, DataError>
fn load_any( &self, key: DataKey, base_req: DataRequest<'_>, ) -> Result<AnyResponse, DataError>
AnyPayload
according to the key and request.source§impl<P> BufferProvider for LocaleFallbackProvider<P>where
P: BufferProvider,
impl<P> BufferProvider for LocaleFallbackProvider<P>where
P: BufferProvider,
source§fn load_buffer(
&self,
key: DataKey,
base_req: DataRequest<'_>,
) -> Result<DataResponse<BufferMarker>, DataError>
fn load_buffer( &self, key: DataKey, base_req: DataRequest<'_>, ) -> Result<DataResponse<BufferMarker>, DataError>
source§impl<P: Clone> Clone for LocaleFallbackProvider<P>
impl<P: Clone> Clone for LocaleFallbackProvider<P>
source§fn clone(&self) -> LocaleFallbackProvider<P>
fn clone(&self) -> LocaleFallbackProvider<P>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<P, M> DataProvider<M> for LocaleFallbackProvider<P>where
P: DataProvider<M>,
M: KeyedDataMarker,
impl<P, M> DataProvider<M> for LocaleFallbackProvider<P>where
P: DataProvider<M>,
M: KeyedDataMarker,
source§fn load(&self, base_req: DataRequest<'_>) -> Result<DataResponse<M>, DataError>
fn load(&self, base_req: DataRequest<'_>) -> Result<DataResponse<M>, DataError>
source§impl<P: Debug> Debug for LocaleFallbackProvider<P>
impl<P: Debug> Debug for LocaleFallbackProvider<P>
source§impl<P, M> DynamicDataProvider<M> for LocaleFallbackProvider<P>where
P: DynamicDataProvider<M>,
M: DataMarker,
impl<P, M> DynamicDataProvider<M> for LocaleFallbackProvider<P>where
P: DynamicDataProvider<M>,
M: DataMarker,
source§fn load_data(
&self,
key: DataKey,
base_req: DataRequest<'_>,
) -> Result<DataResponse<M>, DataError>
fn load_data( &self, key: DataKey, base_req: DataRequest<'_>, ) -> Result<DataResponse<M>, DataError>
Auto Trait Implementations§
impl<P> Freeze for LocaleFallbackProvider<P>where
P: Freeze,
impl<P> RefUnwindSafe for LocaleFallbackProvider<P>where
P: RefUnwindSafe,
impl<P> !Send for LocaleFallbackProvider<P>
impl<P> !Sync for LocaleFallbackProvider<P>
impl<P> Unpin for LocaleFallbackProvider<P>where
P: Unpin,
impl<P> UnwindSafe for LocaleFallbackProvider<P>where
P: UnwindSafe,
Blanket Implementations§
source§impl<P> AsDowncastingAnyProvider for Pwhere
P: AnyProvider + ?Sized,
impl<P> AsDowncastingAnyProvider for Pwhere
P: AnyProvider + ?Sized,
source§fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>
fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>
DynamicDataProvider<M>
when called on AnyProvider
source§impl<P> AsDynamicDataProviderAnyMarkerWrap for P
impl<P> AsDynamicDataProviderAnyMarkerWrap for P
source§fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>
fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>
AnyProvider
when called on DynamicDataProvider<AnyMarker>
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)