Skip to content

Commit 494d1fb

Browse files
authored
Merge pull request #24 from alecmocatta/workaround-object-safe-warning
Workaround object safe warning
2 parents a57dc7e + 1369398 commit 494d1fb

File tree

5 files changed

+24
-45
lines changed

5 files changed

+24
-45
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "serde_traitobject"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
license = "MIT OR Apache-2.0"
55
authors = ["Alec Mocatta <[email protected]>"]
66
categories = ["development-tools","encoding","rust-patterns","network-programming"]
@@ -12,7 +12,7 @@ This library enables the serialization and deserialization of trait objects such
1212
"""
1313
repository = "https://github.com/alecmocatta/serde_traitobject"
1414
homepage = "https://github.com/alecmocatta/serde_traitobject"
15-
documentation = "https://docs.rs/serde_traitobject/0.2.2"
15+
documentation = "https://docs.rs/serde_traitobject/0.2.3"
1616
readme = "README.md"
1717
edition = "2018"
1818

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
[![MIT / Apache 2.0 licensed](https://img.shields.io/crates/l/serde_traitobject.svg?maxAge=2592000)](#License)
55
[![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)
66

7-
[Docs](https://docs.rs/serde_traitobject/0.2.2/serde_traitobject/)
7+
[Docs](https://docs.rs/serde_traitobject/0.2.3/serde_traitobject/)
88

99
**Serializable and deserializable trait objects.**
1010

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

1313
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.
1414

15-
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:
15+
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:
1616

1717
```rust
1818
trait MyTrait: serde_traitobject::Serialize + serde_traitobject::Deserialize {
@@ -28,12 +28,12 @@ struct Message(#[serde(with = "serde_traitobject")] Box<dyn MyTrait>);
2828
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.
2929

3030
There are two ways to (de)serialize your trait object:
31-
* 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;
32-
* 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;
31+
* 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;
32+
* 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;
3333

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

36-
* [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)
36+
* [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)
3737

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

src/convenience.rs

+14-35
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,31 @@ impl Box<dyn Any> {
2525
self.0.into_any()
2626
}
2727
}
28+
#[allow(clippy::use_self)]
2829
impl Box<dyn Any + Send> {
2930
/// Convert into a `std::boxed::Box<dyn std::any::Any + Send>`.
3031
pub fn into_any_send(self) -> boxed::Box<dyn any::Any + Send> {
31-
self.0.into_any_send()
32+
unsafe {
33+
boxed::Box::from_raw(boxed::Box::into_raw(<Box<dyn Any>>::into_any(self)) as *mut _)
34+
}
3235
}
3336
}
37+
#[allow(clippy::use_self)]
3438
impl Box<dyn Any + Sync> {
3539
/// Convert into a `std::boxed::Box<dyn std::any::Any + Sync>`.
3640
pub fn into_any_sync(self) -> boxed::Box<dyn any::Any + Sync> {
37-
self.0.into_any_sync()
41+
unsafe {
42+
boxed::Box::from_raw(boxed::Box::into_raw(<Box<dyn Any>>::into_any(self)) as *mut _)
43+
}
3844
}
3945
}
46+
#[allow(clippy::use_self)]
4047
impl Box<dyn Any + Send + Sync> {
4148
/// Convert into a `std::boxed::Box<dyn std::any::Any + Send + Sync>`.
4249
pub fn into_any_send_sync(self) -> boxed::Box<dyn any::Any + Send + Sync> {
43-
self.0.into_any_send_sync()
50+
unsafe {
51+
boxed::Box::from_raw(boxed::Box::into_raw(<Box<dyn Any>>::into_any(self)) as *mut _)
52+
}
4453
}
4554
}
4655
impl<T: ?Sized + marker::Unsize<U>, U: ?Sized> ops::CoerceUnsized<Box<U>> for Box<T> {}
@@ -369,18 +378,6 @@ pub trait Any: any::Any + Serialize + Deserialize {
369378
fn as_any_mut(&mut self) -> &mut dyn any::Any;
370379
/// Convert to a `std::boxed::Box<dyn std::any::Any>`.
371380
fn into_any(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any>;
372-
/// Convert to a `std::boxed::Box<dyn std::any::Any + Send>`.
373-
fn into_any_send(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send>
374-
where
375-
Self: Send;
376-
/// Convert to a `std::boxed::Box<dyn std::any::Any + Sync>`.
377-
fn into_any_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Sync>
378-
where
379-
Self: Sync;
380-
/// Convert to a `std::boxed::Box<dyn std::any::Any + Send + Sync>`.
381-
fn into_any_send_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send + Sync>
382-
where
383-
Self: Send + Sync;
384381
}
385382
impl<T> Any for T
386383
where
@@ -395,32 +392,14 @@ where
395392
fn into_any(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any> {
396393
self
397394
}
398-
fn into_any_send(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send>
399-
where
400-
Self: Send,
401-
{
402-
self
403-
}
404-
fn into_any_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Sync>
405-
where
406-
Self: Sync,
407-
{
408-
self
409-
}
410-
fn into_any_send_sync(self: boxed::Box<Self>) -> boxed::Box<dyn any::Any + Send + Sync>
411-
where
412-
Self: Send + Sync,
413-
{
414-
self
415-
}
416395
}
417396

418-
impl<'a> AsRef<Self> for dyn Any + 'a {
397+
impl AsRef<Self> for dyn Any {
419398
fn as_ref(&self) -> &Self {
420399
self
421400
}
422401
}
423-
impl<'a> AsRef<Self> for dyn Any + Send + 'a {
402+
impl AsRef<Self> for dyn Any + Send {
424403
fn as_ref(&self) -> &Self {
425404
self
426405
}

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
//!
9595
//! This crate currently requires Rust nightly.
9696
97-
#![doc(html_root_url = "https://docs.rs/serde_traitobject/0.2.2")]
97+
#![doc(html_root_url = "https://docs.rs/serde_traitobject/0.2.3")]
9898
#![feature(
9999
arbitrary_self_types,
100100
coerce_unsized,
@@ -114,7 +114,7 @@
114114
unused_results,
115115
clippy::pedantic
116116
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
117-
#![allow(where_clauses_object_safety, clippy::must_use_candidate)]
117+
#![allow(clippy::must_use_candidate)]
118118

119119
mod convenience;
120120

tests/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
unused_results,
1111
clippy::pedantic
1212
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
13-
#![allow(where_clauses_object_safety, clippy::unseparated_literal_suffix)]
13+
#![allow(clippy::unseparated_literal_suffix)]
1414

1515
use serde_closure::Fn;
1616
use serde_derive::{Deserialize, Serialize};

0 commit comments

Comments
 (0)