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
impl AnyPayloadProvider
sourcepub fn from_owned<M: KeyedDataMarker>(data: M::Yokeable) -> Selfwhere
M::Yokeable: MaybeSendSync,
pub fn from_owned<M: KeyedDataMarker>(data: M::Yokeable) -> Selfwhere
M::Yokeable: MaybeSendSync,
Creates an AnyPayloadProvider
with an owned (allocated) payload of the given data.
sourcepub fn from_static<M: KeyedDataMarker>(data: &'static M::Yokeable) -> Self
pub fn from_static<M: KeyedDataMarker>(data: &'static M::Yokeable) -> Self
Creates an AnyPayloadProvider
with a statically borrowed payload of the given data.
sourcepub fn from_payload<M: KeyedDataMarker>(payload: DataPayload<M>) -> Selfwhere
M::Yokeable: MaybeSendSync,
pub fn from_payload<M: KeyedDataMarker>(payload: DataPayload<M>) -> Selfwhere
M::Yokeable: MaybeSendSync,
Creates an AnyPayloadProvider
from an existing DataPayload
.
sourcepub fn from_any_payload<M: KeyedDataMarker>(payload: AnyPayload) -> Self
pub fn from_any_payload<M: KeyedDataMarker>(payload: AnyPayload) -> Self
Creates an AnyPayloadProvider
from an existing AnyPayload
.
sourcepub fn new_default<M: KeyedDataMarker>() -> Self
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
impl AnyProvider for AnyPayloadProvider
source§fn load_any(
&self,
key: DataKey,
_: DataRequest<'_>,
) -> Result<AnyResponse, DataError>
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 AnyPayloadProviderwhere
M: KeyedDataMarker,
for<'a> YokeTraitHack<<M::Yokeable as Yokeable<'a>>::Output>: Clone,
M::Yokeable: ZeroFrom<'static, M::Yokeable> + MaybeSendSync,
impl<M> DataProvider<M> for AnyPayloadProviderwhere
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>
fn load(&self, req: DataRequest<'_>) -> Result<DataResponse<M>, DataError>
Query the provider for data, returning the result. Read more
Auto Trait Implementations§
impl Freeze for AnyPayloadProvider
impl !RefUnwindSafe for AnyPayloadProvider
impl !Send for AnyPayloadProvider
impl !Sync for AnyPayloadProvider
impl Unpin for AnyPayloadProvider
impl !UnwindSafe for AnyPayloadProvider
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>
Returns an object implementing
DynamicDataProvider<M>
when called on AnyProvider
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
Mutably borrows from an owned value. Read more
source§impl<T> Filterable for T
impl<T> Filterable for T
source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>
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