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(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/tests/data/hello_world.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!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/tests/data/hello_world.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

Create a BlobDataProvider from a blob of ICU4X data.

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

Trait Implementations

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.