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_locale_core::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/v3.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_locale_core::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/v3.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 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, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<DataResponse<BufferMarker>, DataError>

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

impl DynamicDryDataProvider<BufferMarker> for BlobDataProvider

source§

fn dry_load_data( &self, marker: DataMarkerInfo, req: DataRequest<'_>, ) -> Result<DataResponseMetadata, DataError>

This method goes through the motions of load_data, but only returns the metadata. Read more
source§

impl IterableDynamicDataProvider<BufferMarker> for BlobDataProvider

source§

fn iter_ids_for_marker( &self, marker: DataMarkerInfo, ) -> Result<BTreeSet<DataIdentifierCow<'_>>, DataError>

Given a [DataMarkerInfo], returns a set of [DataIdentifierCow].

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
§

impl<P> AsDeserializingBufferProvider for P
where P: BufferProvider + ?Sized,

§

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

source§

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

source§

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

source§

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.
§

impl<P> BufferProvider for P
where P: DynamicDataProvider<BufferMarker> + ?Sized,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,