Add bincode format and make bincode an optional dependency (#806)

This commit is contained in:
Justin Starry 2019-12-14 21:41:13 -05:00 committed by GitHub
parent 1390474abd
commit 60cbcf4746
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 5 deletions

View File

@ -20,7 +20,7 @@ travis-ci = { repository = "yewstack/yew" }
[dependencies] [dependencies]
anymap = "0.12" anymap = "0.12"
bincode = "=1.0.1" bincode = { version = "~1.2.1", optional = true }
failure = "0.1" failure = "0.1"
http = "0.1" http = "0.1"
indexmap = "1.0.2" indexmap = "1.0.2"
@ -58,7 +58,7 @@ doc_test = []
web_test = [] web_test = []
wasm_test = [] wasm_test = []
services = [] services = []
agent = [] agent = ["bincode"]
yaml = ["serde_yaml"] yaml = ["serde_yaml"]
msgpack = ["rmp-serde"] msgpack = ["rmp-serde"]
cbor = ["serde_cbor"] cbor = ["serde_cbor"]

View File

@ -12,5 +12,5 @@ fi
cargo test --features wasm_test --target wasm32-unknown-unknown cargo test --features wasm_test --target wasm32-unknown-unknown
cargo test --test macro_test cargo test --test macro_test
cargo test --test derive_props_test cargo test --test derive_props_test
cargo doc_test cargo doc_test --all-features
(cd crates/macro && cargo doc_test) (cd crates/macro && cargo doc_test)

22
src/format/bincode.rs Normal file
View File

@ -0,0 +1,22 @@
//! Contains an implementation of Bincode serialization format.
use bincode;
/// A representation of a Bincode data. Use it as wrapper to
/// set a format you want to use for conversion:
///
/// ```rust
/// // Converts (lazy) data to a Bincode
///# use yew::format::Bincode;
///# fn dont_execute() {
///# let data: String = unimplemented!();
/// let dump = Bincode(&data);
///
/// // Converts Bincode to a data (lazy).
/// let Bincode(data) = dump;
///# }
/// ```
#[derive(Debug)]
pub struct Bincode<T>(pub T);
binary_format!(Bincode, bincode::serialize, bincode::deserialize);

View File

@ -27,12 +27,15 @@ macro_rules! text_format {
macro_rules! binary_format { macro_rules! binary_format {
($type:ident based on $format:ident) => { ($type:ident based on $format:ident) => {
binary_format!($type, $format::to_vec, $format::from_slice);
};
($type:ident, $into:path, $from:path) => {
impl<'a, T> Into<$crate::format::Binary> for $type<&'a T> impl<'a, T> Into<$crate::format::Binary> for $type<&'a T>
where where
T: ::serde::Serialize, T: ::serde::Serialize,
{ {
fn into(self) -> $crate::format::Binary { fn into(self) -> $crate::format::Binary {
$format::to_vec(&self.0).map_err(::failure::Error::from) $into(&self.0).map_err(::failure::Error::from)
} }
} }
@ -42,7 +45,7 @@ macro_rules! binary_format {
{ {
fn from(value: $crate::format::Binary) -> Self { fn from(value: $crate::format::Binary) -> Self {
match value { match value {
Ok(data) => $type($format::from_slice(&data).map_err(::failure::Error::from)), Ok(data) => $type($from(&data).map_err(::failure::Error::from)),
Err(reason) => $type(Err(reason)), Err(reason) => $type(Err(reason)),
} }
} }

View File

@ -9,6 +9,8 @@ use failure::Error;
#[macro_use] #[macro_use]
pub mod macros; pub mod macros;
#[cfg(feature = "bincode")]
pub mod bincode;
#[cfg(feature = "cbor")] #[cfg(feature = "cbor")]
pub mod cbor; pub mod cbor;
pub mod json; pub mod json;
@ -20,6 +22,8 @@ pub mod toml;
#[cfg(feature = "yaml")] #[cfg(feature = "yaml")]
pub mod yaml; pub mod yaml;
#[cfg(feature = "bincode")]
pub use self::bincode::Bincode;
#[cfg(feature = "cbor")] #[cfg(feature = "cbor")]
pub use self::cbor::Cbor; pub use self::cbor::Cbor;
pub use self::json::Json; pub use self::json::Json;