pub struct LocaleFallbackProvider<P> { /* private fields */ }
Expand description

A data provider wrapper that performs locale fallback. This enables arbitrary locales to be handled at runtime.

§Examples

use icu_locale::langid;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;

let provider = HelloWorldProvider;

let id = DataIdentifierCow::from_locale(langid!("ja-JP").into());

// The provider does not have data for "ja-JP":
DataProvider::<HelloWorldV1Marker>::load(
    &provider,
    DataRequest {
        id: id.as_borrowed(),
        ..Default::default()
    },
)
.expect_err("No fallback");

// But if we wrap the provider in a fallback provider...
let provider = LocaleFallbackProvider::new(
    provider,
    icu_locale::LocaleFallbacker::new().static_to_owned(),
);

// ...then we can load "ja-JP" based on "ja" data
let response = DataProvider::<HelloWorldV1Marker>::load(
    &provider,
    DataRequest {
        id: id.as_borrowed(),
        ..Default::default()
    },
)
.expect("successful with vertical fallback");

assert_eq!(response.metadata.locale.unwrap(), langid!("ja").into(),);
assert_eq!(response.payload.get().message, "こんにちは世界",);

Implementations§

source§

impl<P> LocaleFallbackProvider<P>

source

pub fn new(provider: P, fallbacker: LocaleFallbacker) -> Self

Wraps a provider with a provider performing fallback under the given fallbacker.

If the underlying provider contains deduplicated data, it is important to use the same fallback data that ExportDriver used.

§Examples
use icu_locale::langid;
use icu_locale::LocaleFallbacker;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;

let provider = HelloWorldProvider;

let id = DataIdentifierCow::from_locale(langid!("de-CH").into());

// There is no "de-CH" data in the `HelloWorldProvider`
DataProvider::<HelloWorldV1Marker>::load(
    &provider,
    DataRequest {
        id: id.as_borrowed(),
        ..Default::default()
    },
)
.expect_err("No data for de-CH");

// `HelloWorldProvider` does not contain fallback data,
// but we can construct a fallbacker with `icu_locale`'s
// compiled data.
let provider = LocaleFallbackProvider::new(
    provider,
    LocaleFallbacker::new().static_to_owned(),
);

// Now we can load the "de-CH" data via fallback to "de".
let german_hello_world: DataResponse<HelloWorldV1Marker> = provider
    .load(DataRequest {
        id: id.as_borrowed(),
        ..Default::default()
    })
    .expect("Loading should succeed");

assert_eq!("Hallo Welt", german_hello_world.payload.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.

Trait Implementations§

source§

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

source§

fn load_any( &self, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<AnyResponse, DataError>

Loads an [AnyPayload] according to the marker 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: DataMarker,

source§

fn load(&self, 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> DryDataProvider<M> for LocaleFallbackProvider<P>
where P: DryDataProvider<M>, M: DataMarker,

source§

fn dry_load( &self, req: DataRequest<'_>, ) -> Result<DataResponseMetadata, DataError>

This method goes through the motions of load, but only returns the metadata. Read more
source§

impl<P, M> DynamicDataProvider<M> for LocaleFallbackProvider<P>
where P: DynamicDataProvider<M>, M: DynamicDataMarker,

source§

fn load_data( &self, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<DataResponse<M>, DataError>

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

impl<P, M> DynamicDryDataProvider<M> for LocaleFallbackProvider<P>
where P: DynamicDryDataProvider<M>, M: DynamicDataMarker,

source§

fn dry_load_data( &self, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<DataResponseMetadata, DataError>

This method goes through the motions of load_data, but only returns the metadata. Read more

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>
where P: Send,

§

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

§

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

§

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

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
§

impl<P> AsDeserializingBufferProvider for P
where P: BufferProvider + ?Sized,

§

fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>

Wrap this [BufferProvider] in a [DeserializingBufferProvider].

This requires enabling the deserialization Cargo feature for the expected format(s):

  • deserialize_json
  • deserialize_postcard_1
  • deserialize_bincode_1
§

impl<P> AsDowncastingAnyProvider for P
where P: AnyProvider + ?Sized,

§

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

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

impl<P> AsDynamicDataProviderAnyMarkerWrap for P
where P: DynamicDataProvider<AnyMarker> + ?Sized,

§

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> 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<P> BufferProvider for P
where P: DynamicDataProvider<BufferMarker> + ?Sized,

§

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

§

impl<T> MaybeSendSync for T
where T: Send + Sync,