Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move blob data provider to postcard; support no_std #878

Merged
merged 4 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 121 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions provider/blob/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ all-features = true
icu_provider = { version = "0.2", path = "../../provider/core", features = ["provider_serde"] }
icu_locid = { version = "0.2", path = "../../components/locid", features = ["serde"] }
serde = { version = "1.0", default-features = false, features = ["alloc"] }
bincode = { version = "1.3.3" }
postcard = { version = "0.7.0" }
erased-serde = { version = "0.3", default-features = false, features = ["alloc"] }
litemap = { version = "0.2.0", path = "../../utils/litemap/", features = ["serde"] }

# For the export feature
log = { version = "0.4", optional = true }

[build-dependencies]
bincode = { version = "1.3.3" }
postcard = { version = "0.7.0" }

[dev-dependencies]
icu = { version = "0.2", path = "../../components/icu" }
Expand All @@ -48,4 +48,5 @@ icu_locid_macros = { version = "0.2", path = "../../components/locid/macros" }
path = "src/lib.rs"

[features]
export = ["log"]
export = ["log", "postcard/alloc", "std"]
std = ["icu_locid/std", "icu_provider/std"]
2 changes: 1 addition & 1 deletion provider/blob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ cargo run --bin=icu4x-datagen -- \
--format blob \
--hello-world-key \
--all-locales \
--out hello_world.bincode
--out hello_world.postcard
```

## Example
Expand Down
24 changes: 9 additions & 15 deletions provider/blob/src/export/blob_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@ impl Drop for BlobExporter<'_> {
}

/// TODO(#837): De-duplicate this code from icu_provider_fs.
fn serialize(
obj: &dyn erased_serde::Serialize,
mut sink: &mut (impl std::io::Write + ?Sized),
) -> Result<(), DataError> {
use bincode::Options;
obj.erased_serialize(&mut <dyn erased_serde::Serializer>::erase(
&mut bincode::Serializer::new(
&mut sink,
bincode::config::DefaultOptions::new().with_fixint_encoding(),
),
))?;
Ok(())
fn serialize(obj: &dyn erased_serde::Serialize) -> Result<Vec<u8>, DataError> {
let mut serializer = postcard::Serializer {
output: postcard::flavors::AllocVec(Vec::new()),
};
obj.erased_serialize(&mut <dyn erased_serde::Serializer>::erase(&mut serializer))?;
Ok(serializer.output.0)
}

impl<'d, 's: 'd> DataExporter<'d, 's, SerdeSeDataStructMarker> for BlobExporter<'_> {
Expand All @@ -57,8 +51,7 @@ impl<'d, 's: 'd> DataExporter<'d, 's, SerdeSeDataStructMarker> for BlobExporter<
) -> Result<(), DataError> {
let path = path_util::resource_path_to_string(&req.resource_path);
log::trace!("Adding: {}", path);
let mut buffer: Vec<u8> = Vec::new();
serialize(obj.get().as_serialize(), &mut buffer)?;
let buffer = serialize(obj.get().as_serialize())?;
self.resources.insert(path, buffer);
Ok(())
}
Expand All @@ -77,7 +70,8 @@ impl<'d, 's: 'd> DataExporter<'d, 's, SerdeSeDataStructMarker> for BlobExporter<
}
let blob = BlobSchema::V001(schema);
log::info!("Serializing blob to output stream...");
serialize(&blob, self.sink.as_mut())?;
let vec = serialize(&blob)?;
self.sink.write(&vec).map_err(|e| e.to_string())?;
self.resources.clear();
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions provider/blob/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
//! exporter.close().expect("Should successfully dump to buffer");
//! }
//!
//! // Assert that the exported data equals the pre-computed hello_world.bincode
//! // Assert that the exported data equals the pre-computed hello_world.postcard
//! let mut expected_buffer: Vec<u8> = Vec::new();
//! std::fs::File::open(concat!(
//! env!("CARGO_MANIFEST_DIR"),
//! "/tests/data/hello_world.bincode"
//! "/tests/data/hello_world.postcard"
//! ))
//! .expect("File should exist")
//! .read_to_end(&mut expected_buffer)
//! .expect("Reading pre-computed bincode buffer");
//! .expect("Reading pre-computed postcard buffer");
//!
//! assert_eq!(buffer, expected_buffer);
//! ```
Expand Down
6 changes: 5 additions & 1 deletion provider/blob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! --format blob \
//! --hello-world-key \
//! --all-locales \
//! --out hello_world.bincode
//! --out hello_world.postcard
//! ```
//!
//! # Example
Expand All @@ -32,6 +32,10 @@
//! [`DataProvider`]: icu_provider::prelude::DataProvider
//! [`icu4x-datagen`]: https://github.com/unicode-org/icu4x/tree/main/tools/datagen#readme

#![cfg_attr(not(any(test, feature = "std")), no_std)]

extern crate alloc;

mod blob_schema;
mod path_util;
mod static_data_provider;
Expand Down
2 changes: 2 additions & 0 deletions provider/blob/src/path_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use alloc::string::{String, ToString};
use alloc::vec::Vec;
use icu_provider::prelude::*;

pub fn resource_path_to_string(resource_path: &ResourcePath) -> String {
Expand Down
21 changes: 5 additions & 16 deletions provider/blob/src/static_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serde::de::Deserialize;
///
/// # Examples
///
/// Load "hello world" data from a bincode blob statically linked at compile time:
/// Load "hello world" data from a postcard blob statically linked at compile time:
///
/// ```
/// use icu_provider::prelude::*;
Expand All @@ -29,7 +29,7 @@ use serde::de::Deserialize;
///
/// const HELLO_WORLD_BLOB: &[u8] = include_bytes!(concat!(
/// env!("CARGO_MANIFEST_DIR"),
/// "/tests/data/hello_world.bincode"
/// "/tests/data/hello_world.postcard"
/// ));
///
/// let provider = StaticDataProvider::new_from_static_blob(&HELLO_WORLD_BLOB)
Expand All @@ -53,22 +53,11 @@ pub struct StaticDataProvider {
blob: BlobSchema<'static>,
}

/// TODO(#837): De-duplicate this code from icu_provider_fs.
macro_rules! get_bincode_deserializer_zc {
($bytes:tt) => {{
use bincode::Options;
let options = bincode::DefaultOptions::new()
.with_fixint_encoding()
.allow_trailing_bytes();
bincode::de::Deserializer::from_slice($bytes, options)
}};
}

impl StaticDataProvider {
/// Create a [`StaticDataProvider`] from a `'static` blob of ICU4X data.
pub fn new_from_static_blob(blob: &'static [u8]) -> Result<Self, DataError> {
Ok(StaticDataProvider {
blob: BlobSchema::deserialize(&mut get_bincode_deserializer_zc!(blob))
blob: BlobSchema::deserialize(&mut postcard::Deserializer::from_bytes(blob))
.map_err(DataError::new_resc_error)?,
})
}
Expand All @@ -91,7 +80,7 @@ where
{
fn load_payload(&self, req: &DataRequest) -> Result<DataResponse<'d, 's, M>, DataError> {
let file = self.get_file(req)?;
let data = M::Yokeable::deserialize(&mut get_bincode_deserializer_zc!(file))
let data = M::Yokeable::deserialize(&mut postcard::Deserializer::from_bytes(file))
.map_err(DataError::new_resc_error)?;
Ok(DataResponse {
metadata: DataResponseMetadata {
Expand All @@ -110,7 +99,7 @@ impl SerdeDeDataProvider for StaticDataProvider {
) -> Result<DataResponseMetadata, DataError> {
let file = self.get_file(req)?;
receiver.receive_static(&mut erased_serde::Deserializer::erase(
&mut get_bincode_deserializer_zc!(file),
&mut postcard::Deserializer::from_bytes(file),
))?;

Ok(DataResponseMetadata {
Expand Down
Binary file removed provider/blob/tests/data/hello_world.bincode
Binary file not shown.
Binary file added provider/blob/tests/data/hello_world.postcard
Binary file not shown.
Binary file removed provider/testdata/data/testdata.bincode
Binary file not shown.
Binary file added provider/testdata/data/testdata.postcard
Binary file not shown.
Loading