Skip to content

Commit

Permalink
Add bincode format and make bincode an optional dependency (yewstack#806
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jstarry authored and llebout committed Jan 20, 2020
1 parent 9ef02c3 commit df8c19f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ travis-ci = { repository = "yewstack/yew" }

[dependencies]
anymap = "0.12"
bincode = "=1.0.1"
bincode = { version = "~1.2.1", optional = true }
failure = "0.1"
http = "0.1"
indexmap = "1.0.2"
Expand Down Expand Up @@ -58,7 +58,7 @@ doc_test = []
web_test = []
wasm_test = []
services = []
agent = []
agent = ["bincode"]
yaml = ["serde_yaml"]
msgpack = ["rmp-serde"]
cbor = ["serde_cbor"]
Expand Down
2 changes: 1 addition & 1 deletion ci/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fi
cargo test --features wasm_test --target wasm32-unknown-unknown
cargo test --test macro_test
cargo test --test derive_props_test
cargo doc_test
cargo doc_test --all-features
(cd crates/macro && cargo doc_test)
22 changes: 22 additions & 0 deletions src/format/bincode.rs
Original file line number Diff line number Diff line change
@@ -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);
7 changes: 5 additions & 2 deletions src/format/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ macro_rules! text_format {

macro_rules! binary_format {
($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>
where
T: ::serde::Serialize,
{
fn into(self) -> $crate::format::Binary {
$format::to_vec(&self.0).map_err(::failure::Error::from)
$into(&self.0).map_err(::failure::Error::from)
}
}

Expand All @@ -42,7 +45,7 @@ macro_rules! binary_format {
{
fn from(value: $crate::format::Binary) -> Self {
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)),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use failure::Error;
#[macro_use]
pub mod macros;

#[cfg(feature = "bincode")]
pub mod bincode;
#[cfg(feature = "cbor")]
pub mod cbor;
pub mod json;
Expand All @@ -20,6 +22,8 @@ pub mod toml;
#[cfg(feature = "yaml")]
pub mod yaml;

#[cfg(feature = "bincode")]
pub use self::bincode::Bincode;
#[cfg(feature = "cbor")]
pub use self::cbor::Cbor;
pub use self::json::Json;
Expand Down

0 comments on commit df8c19f

Please sign in to comment.