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
impl BlobDataProvider
sourcepub fn try_new_from_blob(blob: Box<[u8]>) -> Result<Self, DataError>
pub fn try_new_from_blob(blob: Box<[u8]>) -> Result<Self, DataError>
Create a BlobDataProvider
from a blob of ICU4X data.
sourcepub fn try_new_from_static_blob(blob: &'static [u8]) -> Result<Self, DataError>
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
impl BufferProvider for BlobDataProvider
source§fn load_buffer(
&self,
key: DataKey,
req: DataRequest<'_>
) -> Result<DataResponse<BufferMarker>, DataError>
fn load_buffer( &self, key: DataKey, req: DataRequest<'_> ) -> Result<DataResponse<BufferMarker>, DataError>
source§impl Clone for BlobDataProvider
impl Clone for BlobDataProvider
source§fn clone(&self) -> BlobDataProvider
fn clone(&self) -> BlobDataProvider
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for BlobDataProvider
impl Debug for BlobDataProvider
source§impl DynamicDataProvider<BufferMarker> for BlobDataProvider
impl DynamicDataProvider<BufferMarker> for BlobDataProvider
source§fn load_data(
&self,
key: DataKey,
req: DataRequest<'_>
) -> Result<DataResponse<BufferMarker>, DataError>
fn load_data( &self, key: DataKey, req: DataRequest<'_> ) -> Result<DataResponse<BufferMarker>, DataError>
source§impl IterableDynamicDataProvider<BufferMarker> for BlobDataProvider
impl IterableDynamicDataProvider<BufferMarker> for BlobDataProvider
source§fn supported_locales_for_key(
&self,
key: DataKey
) -> Result<Vec<DataLocale>, DataError>
fn supported_locales_for_key( &self, key: DataKey ) -> Result<Vec<DataLocale>, DataError>
DataKey
, returns a list of DataLocale
.Auto Trait Implementations§
impl Freeze for BlobDataProvider
impl RefUnwindSafe for BlobDataProvider
impl Send for BlobDataProvider
impl Sync for BlobDataProvider
impl Unpin for BlobDataProvider
impl UnwindSafe for BlobDataProvider
Blanket Implementations§
source§impl<P> AsDeserializingBufferProvider for Pwhere
P: BufferProvider + ?Sized,
impl<P> AsDeserializingBufferProvider for Pwhere
P: BufferProvider + ?Sized,
source§fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>
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