Trait icu_provider::buf::BufferProvider

source ·
pub trait BufferProvider: DynamicDataProvider<BufferMarker> { }
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_locale_core::langid;
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use std::borrow::Cow;

let buffer_provider = HelloWorldProvider.into_json_provider();

// Deserializing manually
assert_eq!(
    serde_json::from_slice::<HelloWorldV1>(
        buffer_provider
            .load_data(
                HelloWorldV1Marker::INFO,
                DataRequest {
                    id: DataIdentifierBorrowed::for_locale(
                        &langid!("de").into()
                    ),
                    ..Default::default()
                }
            )
            .expect("load should succeed")
            .payload
            .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(DataRequest {
            id: DataIdentifierBorrowed::for_locale(&langid!("de").into()),
            ..Default::default()
        })
        .expect("load should succeed")
        .payload
        .get(),
    &HelloWorldV1 {
        message: Cow::Borrowed("Hallo Welt"),
    },
);

Implementors§