From dfca473f3b6e0e02bc39a15b98489cc7cdc4d5c2 Mon Sep 17 00:00:00 2001 From: alecmocatta Date: Wed, 28 Aug 2019 09:44:43 +0100 Subject: [PATCH 1/3] add Any convenience impl overlooked in previous commit --- src/convenience.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/convenience.rs b/src/convenience.rs index 68b9dfe..3c925f2 100644 --- a/src/convenience.rs +++ b/src/convenience.rs @@ -415,6 +415,23 @@ where } } +impl serde::ser::Serialize for boxed::Box { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de> serde::de::Deserialize<'de> for boxed::Box { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + >::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. From b263972e464367f4d5cee4f02878fdc5b9df7050 Mon Sep 17 00:00:00 2001 From: alecmocatta Date: Wed, 28 Aug 2019 09:46:01 +0100 Subject: [PATCH 2/3] v0.1.6 --- Cargo.toml | 4 ++-- README.md | 10 +++++----- src/lib.rs | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9cfc9b1..9ec1b9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] categories = ["development-tools","encoding","rust-patterns","network-programming"] @@ -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" diff --git a/README.md b/README.md index a4d7793..84ae2c8 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![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.** @@ -12,7 +12,7 @@ This library enables the serialization and deserialization of trait objects so t 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 { @@ -28,12 +28,12 @@ struct Message(#[serde(with = "serde_traitobject")] Box); 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`. diff --git a/src/lib.rs b/src/lib.rs index cb5a1d1..cf5c0b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, From 6c24ec908d1bdc0656b5befa9a768a8fbe04d497 Mon Sep 17 00:00:00 2001 From: alecmocatta Date: Wed, 28 Aug 2019 09:59:37 +0100 Subject: [PATCH 3/3] add + Send impls --- src/convenience.rs | 127 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/src/convenience.rs b/src/convenience.rs index 3c925f2..1027b50 100644 --- a/src/convenience.rs +++ b/src/convenience.rs @@ -431,6 +431,22 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box { >::deserialize(deserializer).map(|x| x.0) } } +impl serde::ser::Serialize for boxed::Box { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de> serde::de::Deserialize<'de> for boxed::Box { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + >::deserialize(deserializer).map(|x| x.0) + } +} /// A convenience trait implemented on all (de)serializable implementors of [`std::error::Error`]. /// @@ -497,6 +513,22 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box { >::deserialize(deserializer).map(|x| x.0) } } +impl serde::ser::Serialize for boxed::Box { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de> serde::de::Deserialize<'de> for boxed::Box { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + >::deserialize(deserializer).map(|x| x.0) + } +} /// A convenience trait implemented on all (de)serializable implementors of [`std::fmt::Display`]. /// @@ -539,6 +571,22 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box { >::deserialize(deserializer).map(|x| x.0) } } +impl serde::ser::Serialize for boxed::Box { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de> serde::de::Deserialize<'de> for boxed::Box { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + >::deserialize(deserializer).map(|x| x.0) + } +} /// A convenience trait implemented on all (de)serializable implementors of [`std::fmt::Debug`]. /// @@ -581,6 +629,22 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box { >::deserialize(deserializer).map(|x| x.0) } } +impl serde::ser::Serialize for boxed::Box { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de> serde::de::Deserialize<'de> for boxed::Box { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + >::deserialize(deserializer).map(|x| x.0) + } +} /// A convenience trait implemented on all (de)serializable implementors of [`std::ops::FnOnce`]. /// @@ -608,6 +672,27 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de> + 'static>>::deserialize(deserializer).map(|x| x.0) } } +impl serde::ser::Serialize + for boxed::Box + Send + 'static> +{ + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de> + for boxed::Box + Send + 'static> +{ + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + + Send + 'static>>::deserialize(deserializer) + .map(|x| x.0) + } +} /// A convenience trait implemented on all (de)serializable implementors of [`std::ops::FnMut`]. /// @@ -635,6 +720,27 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de> + 'static>>::deserialize(deserializer).map(|x| x.0) } } +impl serde::ser::Serialize + for boxed::Box + Send + 'static> +{ + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de> + for boxed::Box + Send + 'static> +{ + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + + Send + 'static>>::deserialize(deserializer) + .map(|x| x.0) + } +} /// A convenience trait implemented on all (de)serializable implementors of [`std::ops::Fn`]. /// @@ -662,3 +768,24 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de> + 'static>>::deserialize(deserializer).map(|x| x.0) } } +impl serde::ser::Serialize + for boxed::Box + Send + 'static> +{ + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serialize(&self, serializer) + } +} +impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de> + for boxed::Box + Send + 'static> +{ + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + + Send + 'static>>::deserialize(deserializer) + .map(|x| x.0) + } +}