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>

source

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,

source

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>

source

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);
source

pub fn inner(&self) -> &P

Returns a reference to the inner provider, bypassing fallback.

source

pub fn inner_mut(&mut self) -> &mut P

Returns a mutable reference to the inner provider.

source

pub fn into_inner(self) -> P

Returns ownership of the inner provider to the caller.

source

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,

source§

fn load_any( &self, key: DataKey, base_req: DataRequest<'_>, ) -> Result<AnyResponse, DataError>

Loads an AnyPayload according to the key and request.
source§

impl<P> BufferProvider for LocaleFallbackProvider<P>
where P: BufferProvider,

source§

fn load_buffer( &self, key: DataKey, base_req: DataRequest<'_>, ) -> Result<DataResponse<BufferMarker>, DataError>

Loads a DataPayload<BufferMarker> according to the key and request.
source§

impl<P: Clone> Clone for LocaleFallbackProvider<P>

source§

fn clone(&self) -> LocaleFallbackProvider<P>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

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>

Query the provider for data, returning the result. Read more
source§

impl<P: Debug> Debug for LocaleFallbackProvider<P>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<P, M> DynamicDataProvider<M> for LocaleFallbackProvider<P>

source§

fn load_data( &self, key: DataKey, base_req: DataRequest<'_>, ) -> Result<DataResponse<M>, DataError>

Query the provider for data, returning the result. 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<P> AsDowncastingAnyProvider for P
where P: AnyProvider + ?Sized,

source§

fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>

Returns an object implementing DynamicDataProvider<M> when called on AnyProvider
source§

impl<P> AsDynamicDataProviderAnyMarkerWrap for P

source§

fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>

Returns an object implementing AnyProvider when called on DynamicDataProvider<AnyMarker>
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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> Filterable for T

source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. 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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

source§

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>,

source§

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.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T