pub trait BufferProvider {
// Required method
fn load_buffer(
&self,
key: DataKey,
req: DataRequest<'_>,
) -> Result<DataResponse<BufferMarker>, DataError>;
}
Expand description
A data provider that returns opaque bytes.
Generally, these bytes are expected to be deserializable with Serde. To get an object
implementing DataProvider
via Serde, use as_deserializing()
.
Passing a BufferProvider
to a *_with_buffer_provider
constructor requires enabling
the deserialization Cargo feature for the expected format(s):
deserialize_json
deserialize_postcard_1
deserialize_bincode_1
Along with DataProvider
, this is one of the two foundational traits in this crate.
BufferProvider
can be made into a trait object. It is used over FFI.
§Examples
use icu_locid::langid;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use std::borrow::Cow;
let buffer_provider = HelloWorldProvider.into_json_provider();
let req = DataRequest {
locale: &langid!("de").into(),
metadata: Default::default(),
};
// Deserializing manually
assert_eq!(
serde_json::from_slice::<HelloWorldV1>(
buffer_provider
.load_buffer(HelloWorldV1Marker::KEY, req)
.expect("load should succeed")
.take_payload()
.unwrap()
.get()
)
.expect("should deserialize"),
HelloWorldV1 {
message: Cow::Borrowed("Hallo Welt"),
},
);
// Deserialize automatically
let deserializing_provider: &dyn DataProvider<HelloWorldV1Marker> =
&buffer_provider.as_deserializing();
assert_eq!(
deserializing_provider
.load(req)
.expect("load should succeed")
.take_payload()
.unwrap()
.get(),
&HelloWorldV1 {
message: Cow::Borrowed("Hallo Welt"),
},
);
Required Methods§
Sourcefn load_buffer(
&self,
key: DataKey,
req: DataRequest<'_>,
) -> Result<DataResponse<BufferMarker>, DataError>
fn load_buffer( &self, key: DataKey, req: DataRequest<'_>, ) -> Result<DataResponse<BufferMarker>, DataError>
Loads a DataPayload
<
BufferMarker
>
according to the key and request.