Skip to content

Commit

Permalink
Merge pull request #14 from alecmocatta/forgot-any
Browse files Browse the repository at this point in the history
Add more convenience impls of (de)serialize on Box<dyn trait>
  • Loading branch information
mergify[bot] authored Aug 28, 2019
2 parents 22bb82c + 6c24ec9 commit de127d4
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_traitobject"
version = "0.1.5"
version = "0.1.6"
license = "MIT OR Apache-2.0"
authors = ["Alec Mocatta <alec@mocatta.net>"]
categories = ["development-tools","encoding","rust-patterns","network-programming"]
Expand All @@ -12,7 +12,7 @@ This library enables the serialization and deserialization of trait objects such
"""
repository = "https://github.com/alecmocatta/serde_traitobject"
homepage = "https://github.com/alecmocatta/serde_traitobject"
documentation = "https://docs.rs/serde_traitobject/0.1.5"
documentation = "https://docs.rs/serde_traitobject/0.1.6"
readme = "README.md"
edition = "2018"

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
[![MIT / Apache 2.0 licensed](https://img.shields.io/crates/l/serde_traitobject.svg?maxAge=2592000)](#License)
[![Build Status](https://dev.azure.com/alecmocatta/serde_traitobject/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/serde_traitobject/_build/latest?branchName=master)

[Docs](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/)
[Docs](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/)

**Serializable and deserializable trait objects.**

This library enables the serialization and deserialization of trait objects so they can be sent between other processes running the same binary.

For example, if you have multiple forks of a process, or the same binary running on each of a cluster of machines, this library lets you send trait objects between them.

Any trait can be made (de)serializable when made into a trait object by adding this crate's [Serialize](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.Serialize.html) and [Deserialize](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.Deserialize.html) traits as supertraits:
Any trait can be made (de)serializable when made into a trait object by adding this crate's [Serialize](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.Serialize.html) and [Deserialize](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.Deserialize.html) traits as supertraits:

```rust
trait MyTrait: serde_traitobject::Serialize + serde_traitobject::Deserialize {
Expand All @@ -28,12 +28,12 @@ struct Message(#[serde(with = "serde_traitobject")] Box<dyn MyTrait>);
And that's it! The two traits are automatically implemented for all `T: serde::Serialize` and all `T: serde::de::DeserializeOwned`, so as long as all implementors of your trait are themselves serializable then you're good to go.

There are two ways to (de)serialize your trait object:
* Apply the `#[serde(with = "serde_traitobject")]` [field attribute](https://serde.rs/attributes.html), which instructs serde to use this crate's [serialize](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/fn.serialize.html) and [deserialize](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/fn.deserialize.html) functions;
* The [Box](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/struct.Box.html), [Rc](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/struct.Rc.html) and [Arc](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/struct.Arc.html) structs, which are simple wrappers around their stdlib counterparts that automatically handle (de)serialization without needing the above annotation;
* Apply the `#[serde(with = "serde_traitobject")]` [field attribute](https://serde.rs/attributes.html), which instructs serde to use this crate's [serialize](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/fn.serialize.html) and [deserialize](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/fn.deserialize.html) functions;
* The [Box](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/struct.Box.html), [Rc](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/struct.Rc.html) and [Arc](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/struct.Arc.html) structs, which are simple wrappers around their stdlib counterparts that automatically handle (de)serialization without needing the above annotation;

Additionally, there are several convenience traits implemented that extend their stdlib counterparts:

* [Any](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.Any.html), [Debug](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.Debug.html), [Display](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.Display.html), [Error](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.Error.html), [Fn](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.Fn.html), [FnMut](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.FnMut.html), [FnOnce](https://docs.rs/serde_traitobject/0.1.5/serde_traitobject/trait.FnOnce.html)
* [Any](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.Any.html), [Debug](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.Debug.html), [Display](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.Display.html), [Error](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.Error.html), [Fn](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.Fn.html), [FnMut](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.FnMut.html), [FnOnce](https://docs.rs/serde_traitobject/0.1.6/serde_traitobject/trait.FnOnce.html)

These are automatically implemented on all implementors of their stdlib counterparts that also implement `serde::Serialize` and `serde::de::DeserializeOwned`.

Expand Down
144 changes: 144 additions & 0 deletions src/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,39 @@ where
}
}

impl serde::ser::Serialize for boxed::Box<dyn Any + 'static> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Any + 'static> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn Any + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}
impl serde::ser::Serialize for boxed::Box<dyn Any + Send + 'static> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Any + Send + 'static> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn Any + Send + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}

/// A convenience trait implemented on all (de)serializable implementors of [`std::error::Error`].
///
/// It can be made into a trait object which is then (de)serializable.
Expand Down Expand Up @@ -480,6 +513,22 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Error + 'static> {
<Box<dyn Error + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}
impl serde::ser::Serialize for boxed::Box<dyn Error + Send + 'static> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Error + Send + 'static> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn Error + Send + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}

/// A convenience trait implemented on all (de)serializable implementors of [`std::fmt::Display`].
///
Expand Down Expand Up @@ -522,6 +571,22 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Display + 'static> {
<Box<dyn Display + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}
impl serde::ser::Serialize for boxed::Box<dyn Display + Send + 'static> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Display + Send + 'static> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn Display + Send + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}

/// A convenience trait implemented on all (de)serializable implementors of [`std::fmt::Debug`].
///
Expand Down Expand Up @@ -564,6 +629,22 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Debug + 'static> {
<Box<dyn Debug + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}
impl serde::ser::Serialize for boxed::Box<dyn Debug + Send + 'static> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Debug + Send + 'static> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn Debug + Send + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}

/// A convenience trait implemented on all (de)serializable implementors of [`std::ops::FnOnce`].
///
Expand Down Expand Up @@ -591,6 +672,27 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
<Box<dyn FnOnce<Args, Output = Output> + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}
impl<Args: 'static, Output: 'static> serde::ser::Serialize
for boxed::Box<dyn FnOnce<Args, Output = Output> + Send + 'static>
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
for boxed::Box<dyn FnOnce<Args, Output = Output> + Send + 'static>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn FnOnce<Args, Output = Output> + Send + 'static>>::deserialize(deserializer)
.map(|x| x.0)
}
}

/// A convenience trait implemented on all (de)serializable implementors of [`std::ops::FnMut`].
///
Expand Down Expand Up @@ -618,6 +720,27 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
<Box<dyn FnMut<Args, Output = Output> + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}
impl<Args: 'static, Output: 'static> serde::ser::Serialize
for boxed::Box<dyn FnMut<Args, Output = Output> + Send + 'static>
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
for boxed::Box<dyn FnMut<Args, Output = Output> + Send + 'static>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn FnMut<Args, Output = Output> + Send + 'static>>::deserialize(deserializer)
.map(|x| x.0)
}
}

/// A convenience trait implemented on all (de)serializable implementors of [`std::ops::Fn`].
///
Expand Down Expand Up @@ -645,3 +768,24 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
<Box<dyn Fn<Args, Output = Output> + 'static>>::deserialize(deserializer).map(|x| x.0)
}
}
impl<Args: 'static, Output: 'static> serde::ser::Serialize
for boxed::Box<dyn Fn<Args, Output = Output> + Send + 'static>
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serialize(&self, serializer)
}
}
impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
for boxed::Box<dyn Fn<Args, Output = Output> + Send + 'static>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<Box<dyn Fn<Args, Output = Output> + Send + 'static>>::deserialize(deserializer)
.map(|x| x.0)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
//!
//! This crate currently requires Rust nightly.

#![doc(html_root_url = "https://docs.rs/serde_traitobject/0.1.5")]
#![doc(html_root_url = "https://docs.rs/serde_traitobject/0.1.6")]
#![feature(
coerce_unsized,
core_intrinsics,
Expand Down

0 comments on commit de127d4

Please sign in to comment.