pub struct AnyPayloadProvider {
    key: DataKey,
    data: AnyPayload,
}
Expand description

A data provider that returns clones of a fixed type-erased payload.

AnyPayloadProvider implements AnyProvider, so it can be used in *_with_any_provider constructors across ICU4X.

§Examples

use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::any_payload::AnyPayloadProvider;
use std::borrow::Cow;
use writeable::assert_writeable_eq;

let provider =
    AnyPayloadProvider::from_static::<HelloWorldV1Marker>(&HelloWorldV1 {
        message: Cow::Borrowed("custom hello world"),
    });

// Check that it works:
let formatter = HelloWorldFormatter::try_new_with_any_provider(
    &provider,
    &Default::default(),
)
.expect("key matches");
assert_writeable_eq!(formatter.format(), "custom hello world");

// Requests for invalid keys get MissingDataKey
assert!(matches!(
    provider.load_any(icu_provider::data_key!("foo@1"), Default::default()),
    Err(DataError {
        kind: DataErrorKind::MissingDataKey,
        ..
    })
))

Fields§

§key: DataKey

The DataKey for which to provide data. All others will receive a DataErrorKind::MissingDataKey.

§data: AnyPayload

The AnyPayload to return on matching requests.

Implementations§

source§

impl AnyPayloadProvider

source

pub fn from_owned<M: KeyedDataMarker>(data: M::Yokeable) -> Self

Creates an AnyPayloadProvider with an owned (allocated) payload of the given data.

source

pub fn from_static<M: KeyedDataMarker>(data: &'static M::Yokeable) -> Self

Creates an AnyPayloadProvider with a statically borrowed payload of the given data.

source

pub fn from_payload<M: KeyedDataMarker>(payload: DataPayload<M>) -> Self

Creates an AnyPayloadProvider from an existing DataPayload.

source

pub fn from_any_payload<M: KeyedDataMarker>(payload: AnyPayload) -> Self

Creates an AnyPayloadProvider from an existing AnyPayload.

source

pub fn new_default<M: KeyedDataMarker>() -> Self

Creates an AnyPayloadProvider with the default (allocated) version of the data struct.

Trait Implementations§

source§

impl AnyProvider for AnyPayloadProvider

source§

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

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

impl<M> DataProvider<M> for AnyPayloadProvider
where M: KeyedDataMarker, for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone, M::Yokeable: ZeroFrom<'static, M::Yokeable> + MaybeSendSync,

source§

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

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

impl Debug for AnyPayloadProvider

source§

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

Formats the value using the given formatter. 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<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> 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, 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