Skip to content

Commit

Permalink
0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 committed Dec 8, 2019
1 parent dffa50b commit 1479499
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
[package]
name = "tokio-serde"
version = "0.5.1"
version = "0.6.0"
edition = "2018"
authors = ["Carl Lerche <[email protected]>"]
authors = [
"Carl Lerche <[email protected]>",
"Artem Vorotnikov <[email protected]>",
"Bastian Köcher <[email protected]>",
]
license = "MIT/Apache-2.0"
readme = "README.md"
keywords = []
Expand All @@ -22,19 +26,19 @@ derivative = { version = "1", optional = true }
futures = "0.3"
pin-project = "0.4"
serde = { version = "1", optional = true }
serde_bincode = { package = "bincode", version = "1", optional = true }
bincode-crate = { package = "bincode", version = "1", optional = true }
serde_json = { version = "1", optional = true }
serde_messagepack = { package = "rmp-serde", version = "0.14", optional = true }
rmp-serde = { version = "0.14", optional = true }

[dev-dependencies]
tokio = { version = "0.2", features = ["full"] }
tokio-util = { version = "0.2", features = ["full"] }
static_assertions = "1.1.0"

[features]
bincode = ["derivative", "serde", "serde_bincode"]
bincode = ["derivative", "serde", "bincode-crate"]
json = ["derivative", "serde", "serde_json"]
messagepack = ["derivative", "serde", "serde_messagepack"]
messagepack = ["derivative", "serde", "rmp-serde"]

[[example]]
name = "client"
Expand Down
4 changes: 3 additions & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Copyright (c) 2018 Carl Lerche
Copyright (c) 2017 Carl Lerche
Copyright (c) 2018 Bastian Köcher
Copyright (c) 2019 Artem Vorotnikov

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
Utilities needed to easily implement a Tokio transport using [serde] for
serialization and deserialization of frame values.

[Documentation](https://carllerche.github.io/tokio-serde/tokio_serde/index.html)
[Documentation](https://docs.rs/tokio-serde)

## Usage

To use `tokio-serde`, first add this to your `Cargo.toml`:

```toml
[dependencies]
tokio-serde = "0.5"
tokio-serde = "0.6"
```

Next, add this to your crate:

```rust
use tokio_serde::{Serializer, Deserializer, FramedRead, FramedWrite};
use tokio_serde::{Serializer, Deserializer, Framed};
```

[serde]: https://serde.rs
Expand Down
44 changes: 29 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! [`Framed`] along with the upstream [`Stream`] or
//! [`Sink`] that handles the byte encoded frames.
//!
//! By doing this, a transformation pipeline is built. For reading [json], it looks
//! By doing this, a transformation pipeline is built. For reading, it looks
//! something like this:
//!
//! * `tokio_serde::Framed`
Expand All @@ -37,9 +37,12 @@
//! [serde]: https://serde.rs
//! [serde-json]: https://github.com/serde-rs/json
//! [transport]: https://tokio.rs/docs/going-deeper/transports/
//! [length delimited]: https://docs.rs/tokio-util/0.2/tokio_util/codec/length_delimited/index.html
//! [`Serializer`]: trait.Serializer.html
//! [`Deserializer`]: trait.Deserializer.html
//! [`Framed`]: struct.Framed.html
//! [`Stream`]: https://docs.rs/futures/0.3/futures/stream/trait.Stream.html
//! [`Sink`]: https://docs.rs/futures/0.3/futures/sink/trait.Sink.html

use {
bytes::{Bytes, BytesMut},
Expand Down Expand Up @@ -204,7 +207,7 @@ pub trait Deserializer<T> {
/// implementor. One option would be to use [length_delimited] provided by
/// [tokio-util].
///
/// [length_delimited]: http://docs.rs/tokio-util/codec/length_delimited/index.html
/// [length_delimited]: http://docs.rs/tokio-util/0.2/tokio_util/codec/length_delimited/index.html
/// [tokio-util]: http://crates.io/crates/tokio-util
#[pin_project]
pub struct Framed<Transport, Item, SinkItem, Codec> {
Expand Down Expand Up @@ -306,6 +309,10 @@ where
pub type SymmetricallyFramed<Transport, Value, Codec> = Framed<Transport, Value, Value, Codec>;

#[cfg(any(feature = "json", feature = "bincode", feature = "messagepack"))]
#[cfg_attr(
docs,
doc(cfg(any(feature = "json", feature = "bincode", feature = "messagepack")))
)]
pub mod formats {
#[cfg(feature = "bincode")]
pub use self::bincode::*;
Expand All @@ -326,20 +333,25 @@ pub mod formats {
mod bincode {
use super::*;

/// Bincode codec using [bincode](https://docs.rs/bincode) crate.
#[cfg_attr(feature = "docs", doc(cfg(bincode)))]
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct Bincode<Item, SinkItem> {
ghost: PhantomData<(Item, SinkItem)>,
}

#[cfg_attr(feature = "docs", doc(cfg(bincode)))]
pub type SymmetricalBincode<T> = Bincode<T, T>;

impl<Item, SinkItem> Deserializer<Item> for Bincode<Item, SinkItem>
where
for<'a> Item: Deserialize<'a>,
{
type Error = io::Error;

fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result<Item, Self::Error> {
Ok(serde_bincode::deserialize(src)
Ok(bincode_crate::deserialize(src)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?)
}
}
Expand All @@ -351,25 +363,28 @@ pub mod formats {
type Error = io::Error;

fn serialize(self: Pin<&mut Self>, item: &SinkItem) -> Result<Bytes, Self::Error> {
Ok(serde_bincode::serialize(item)
Ok(bincode_crate::serialize(item)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
.into())
}
}

pub type SymmetricalBincode<T> = Bincode<T, T>;
}

#[cfg(feature = "json")]
mod json {
use super::*;

/// JSON codec using [serde_json](https://docs.rs/serde_json) crate.
#[cfg_attr(feature = "docs", doc(cfg(json)))]
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct Json<Item, SinkItem> {
ghost: PhantomData<(Item, SinkItem)>,
}

#[cfg_attr(feature = "docs", doc(cfg(json)))]
pub type SymmetricalJson<T> = Json<T, T>;

impl<Item, SinkItem> Deserializer<Item> for Json<Item, SinkItem>
where
for<'a> Item: Deserialize<'a>,
Expand All @@ -391,8 +406,6 @@ pub mod formats {
serde_json::to_vec(item).map(Into::into)
}
}

pub type SymmetricalJson<T> = Json<T, T>;
}

#[cfg(feature = "messagepack")]
Expand All @@ -401,23 +414,26 @@ pub mod formats {

use std::io;

/// MessagePack codec using [rmp-serde](https://docs.rs/rmp-serde) crate.
#[cfg_attr(feature = "docs", doc(cfg(messagepack)))]
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct MessagePack<Item, SinkItem> {
ghost: PhantomData<(Item, SinkItem)>,
}

#[cfg_attr(feature = "docs", doc(cfg(messagepack)))]
pub type SymmetricalMessagePack<T> = MessagePack<T, T>;

impl<Item, SinkItem> Deserializer<Item> for MessagePack<Item, SinkItem>
where
for<'a> Item: Deserialize<'a>,
{
type Error = io::Error;

fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result<Item, Self::Error> {
Ok(
serde_messagepack::from_read(std::io::Cursor::new(src).reader())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?,
)
Ok(rmp_serde::from_read(std::io::Cursor::new(src).reader())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?)
}
}

Expand All @@ -428,12 +444,10 @@ pub mod formats {
type Error = io::Error;

fn serialize(self: Pin<&mut Self>, item: &SinkItem) -> Result<Bytes, Self::Error> {
Ok(serde_messagepack::to_vec(item)
Ok(rmp_serde::to_vec(item)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
.into())
}
}

pub type SymmetricalMessagePack<T> = MessagePack<T, T>;
}
}

0 comments on commit 1479499

Please sign in to comment.