Skip to content

Commit

Permalink
Merge pull request #24 from alecmocatta/workaround-object-safe-warning
Browse files Browse the repository at this point in the history
Workaround object safe warning
  • Loading branch information
mergify[bot] authored Nov 9, 2019
2 parents a57dc7e + 1369398 commit 494d1fb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 45 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.2.2"
version = "0.2.3"
license = "MIT OR Apache-2.0"
authors = ["Alec Mocatta <[email protected]>"]
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.2.2"
documentation = "https://docs.rs/serde_traitobject/0.2.3"
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.2.2/serde_traitobject/)
[Docs](https://docs.rs/serde_traitobject/0.2.3/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.2.2/serde_traitobject/trait.Serialize.html) and [Deserialize](https://docs.rs/serde_traitobject/0.2.2/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.2.3/serde_traitobject/trait.Serialize.html) and [Deserialize](https://docs.rs/serde_traitobject/0.2.3/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.2.2/serde_traitobject/fn.serialize.html) and [deserialize](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/fn.deserialize.html) functions;
* The [Box](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/struct.Box.html), [Rc](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/struct.Rc.html) and [Arc](https://docs.rs/serde_traitobject/0.2.2/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.2.3/serde_traitobject/fn.serialize.html) and [deserialize](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/fn.deserialize.html) functions;
* The [Box](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/struct.Box.html), [Rc](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/struct.Rc.html) and [Arc](https://docs.rs/serde_traitobject/0.2.3/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.2.2/serde_traitobject/trait.Any.html), [Debug](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/trait.Debug.html), [Display](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/trait.Display.html), [Error](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/trait.Error.html), [Fn](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/trait.Fn.html), [FnMut](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/trait.FnMut.html), [FnOnce](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/trait.FnOnce.html)
* [Any](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/trait.Any.html), [Debug](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/trait.Debug.html), [Display](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/trait.Display.html), [Error](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/trait.Error.html), [Fn](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/trait.Fn.html), [FnMut](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/trait.FnMut.html), [FnOnce](https://docs.rs/serde_traitobject/0.2.3/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
49 changes: 14 additions & 35 deletions src/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,31 @@ impl Box<dyn Any> {
self.0.into_any()
}
}
#[allow(clippy::use_self)]
impl Box<dyn Any + Send> {
/// Convert into a `std::boxed::Box<dyn std::any::Any + Send>`.
pub fn into_any_send(self) -> boxed::Box<dyn any::Any + Send> {
self.0.into_any_send()
unsafe {
boxed::Box::from_raw(boxed::Box::into_raw(<Box<dyn Any>>::into_any(self)) as *mut _)
}
}
}
#[allow(clippy::use_self)]
impl Box<dyn Any + Sync> {
/// Convert into a `std::boxed::Box<dyn std::any::Any + Sync>`.
pub fn into_any_sync(self) -> boxed::Box<dyn any::Any + Sync> {
self.0.into_any_sync()
unsafe {
boxed::Box::from_raw(boxed::Box::into_raw(<Box<dyn Any>>::into_any(self)) as *mut _)
}
}
}
#[allow(clippy::use_self)]
impl Box<dyn Any + Send + Sync> {
/// Convert into a `std::boxed::Box<dyn std::any::Any + Send + Sync>`.
pub fn into_any_send_sync(self) -> boxed::Box<dyn any::Any + Send + Sync> {
self.0.into_any_send_sync()
unsafe {
boxed::Box::from_raw(boxed::Box::into_raw(<Box<dyn Any>>::into_any(self)) as *mut _)
}
}
}
impl<T: ?Sized + marker::Unsize<U>, U: ?Sized> ops::CoerceUnsized<Box<U>> for Box<T> {}
Expand Down Expand Up @@ -369,18 +378,6 @@ pub trait Any: any::Any + Serialize + Deserialize {
fn as_any_mut(&mut self) -> &mut dyn any::Any;
/// Convert to a `std::boxed::Box<dyn std::any::Any>`.
fn into_any(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any>;
/// Convert to a `std::boxed::Box<dyn std::any::Any + Send>`.
fn into_any_send(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send>
where
Self: Send;
/// Convert to a `std::boxed::Box<dyn std::any::Any + Sync>`.
fn into_any_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Sync>
where
Self: Sync;
/// Convert to a `std::boxed::Box<dyn std::any::Any + Send + Sync>`.
fn into_any_send_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send + Sync>
where
Self: Send + Sync;
}
impl<T> Any for T
where
Expand All @@ -395,32 +392,14 @@ where
fn into_any(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any> {
self
}
fn into_any_send(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send>
where
Self: Send,
{
self
}
fn into_any_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Sync>
where
Self: Sync,
{
self
}
fn into_any_send_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send + Sync>
where
Self: Send + Sync,
{
self
}
}

impl<'a> AsRef<Self> for dyn Any + 'a {
impl AsRef<Self> for dyn Any {
fn as_ref(&self) -> &Self {
self
}
}
impl<'a> AsRef<Self> for dyn Any + Send + 'a {
impl AsRef<Self> for dyn Any + Send {
fn as_ref(&self) -> &Self {
self
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
//!
//! This crate currently requires Rust nightly.
#![doc(html_root_url = "https://docs.rs/serde_traitobject/0.2.2")]
#![doc(html_root_url = "https://docs.rs/serde_traitobject/0.2.3")]
#![feature(
arbitrary_self_types,
coerce_unsized,
Expand All @@ -114,7 +114,7 @@
unused_results,
clippy::pedantic
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
#![allow(where_clauses_object_safety, clippy::must_use_candidate)]
#![allow(clippy::must_use_candidate)]

mod convenience;

Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
unused_results,
clippy::pedantic
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
#![allow(where_clauses_object_safety, clippy::unseparated_literal_suffix)]
#![allow(clippy::unseparated_literal_suffix)]

use serde_closure::Fn;
use serde_derive::{Deserialize, Serialize};
Expand Down

0 comments on commit 494d1fb

Please sign in to comment.