Trait icu_provider::any::AnyProvider
source · pub trait AnyProvider {
// Required method
fn load_any(
&self,
key: DataKey,
req: DataRequest<'_>,
) -> Result<AnyResponse, DataError>;
}
Expand description
An object-safe data provider that returns data structs cast to dyn Any
trait objects.
§Examples
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use std::borrow::Cow;
let any_provider = HelloWorldProvider.as_any_provider();
let req = DataRequest {
locale: &icu_locid::langid!("de").into(),
metadata: Default::default(),
};
// Downcasting manually
assert_eq!(
any_provider
.load_any(HelloWorldV1Marker::KEY, req)
.expect("load should succeed")
.downcast::<HelloWorldV1Marker>()
.expect("types should match")
.take_payload()
.unwrap()
.get(),
&HelloWorldV1 {
message: Cow::Borrowed("Hallo Welt"),
},
);
// Downcasting automatically
let downcasting_provider: &dyn DataProvider<HelloWorldV1Marker> =
&any_provider.as_downcasting();
assert_eq!(
downcasting_provider
.load(req)
.expect("load should succeed")
.take_payload()
.unwrap()
.get(),
&HelloWorldV1 {
message: Cow::Borrowed("Hallo Welt"),
},
);
Required Methods§
sourcefn load_any(
&self,
key: DataKey,
req: DataRequest<'_>,
) -> Result<AnyResponse, DataError>
fn load_any( &self, key: DataKey, req: DataRequest<'_>, ) -> Result<AnyResponse, DataError>
Loads an AnyPayload
according to the key and request.