Struct icu_provider_blob::BlobDataProvider

source ·
pub struct BlobDataProvider { /* private fields */ }
Expand description

A data provider that reads from serialized blobs of data.

This enables data blobs to be read from arbitrary sources at runtime, allowing code and data to be separated. Alternatively, blobs can also be statically included at compile time.

BlobDataProvider implements BufferProvider, so it can be used in *_with_buffer_provider constructors across ICU4X.

§Sync + Send

This provider uses reference counting internally. When the sync Cargo feature on the icu_provider crate is enabled, it uses Arc instead of Rc, making it Sync + Send.

§Examples

§Dynamic loading

Load “hello world” data from a postcard blob loaded at runtime:

use icu_locid::locale;
use icu_provider::hello_world::HelloWorldFormatter;
use icu_provider_blob::BlobDataProvider;
use writeable::assert_writeable_eq;

// Read an ICU4X data blob dynamically:
let blob = std::fs::read("tests/data/v2.postcard")
    .expect("Reading pre-computed postcard buffer");

// Create a DataProvider from it:
let provider = BlobDataProvider::try_new_from_blob(blob.into_boxed_slice())
    .expect("Deserialization should succeed");

// Check that it works:
let formatter = HelloWorldFormatter::try_new_with_buffer_provider(
    &provider,
    &locale!("la").into(),
)
.expect("locale exists");

assert_writeable_eq!(formatter.format(), "Ave, munde");

§Static loading

Load “hello world” data from a postcard blob statically linked at compile time:

use icu_locid::locale;
use icu_provider::hello_world::HelloWorldFormatter;
use icu_provider_blob::BlobDataProvider;
use writeable::assert_writeable_eq;

// Read an ICU4X data blob statically:
const HELLO_WORLD_BLOB: &[u8] = include_bytes!("../tests/data/v2.postcard");

// Create a DataProvider from it:
let provider = BlobDataProvider::try_new_from_static_blob(HELLO_WORLD_BLOB)
    .expect("Deserialization should succeed");

// Check that it works:
let formatter = HelloWorldFormatter::try_new_with_buffer_provider(
    &provider,
    &locale!("la").into(),
)
.expect("locale exists");

assert_writeable_eq!(formatter.format(), "Ave, munde");

Implementations§

source§

impl BlobDataProvider

source

pub fn try_new_from_blob(blob: Box<[u8]>) -> Result<Self, DataError>

Create a BlobDataProvider from a blob of ICU4X data.

source

pub fn try_new_from_static_blob(blob: &'static [u8]) -> Result<Self, DataError>

Create a BlobDataProvider from a static blob. This is a special case of try_new_from_blob and is allocation-free.

Trait Implementations§

source§

impl BufferProvider for BlobDataProvider

source§

fn load_buffer( &self, key: DataKey, req: DataRequest<'_> ) -> Result<DataResponse<BufferMarker>, DataError>

Loads a DataPayload<BufferMarker> according to the key and request.
source§

impl Clone for BlobDataProvider

source§

fn clone(&self) -> BlobDataProvider

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for BlobDataProvider

source§

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

Formats the value using the given formatter. Read more
source§

impl DynamicDataProvider<BufferMarker> for BlobDataProvider

source§

fn load_data( &self, key: DataKey, req: DataRequest<'_> ) -> Result<DataResponse<BufferMarker>, DataError>

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

impl IterableDynamicDataProvider<BufferMarker> for BlobDataProvider

source§

fn supported_locales_for_key( &self, key: DataKey ) -> Result<Vec<DataLocale>, DataError>

Given a DataKey, returns a list of DataLocale.

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> AsDeserializingBufferProvider for P
where P: BufferProvider + ?Sized,

source§

fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>

Wrap this BufferProvider in a DeserializingBufferProvider.

This requires enabling the deserialization Cargo feature for the expected format(s):

  • deserialize_json
  • deserialize_postcard_1
  • deserialize_bincode_1
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> 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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
where T: Send + Sync,