Replace TryFrom macro impls with trait magic

This commit is contained in:
asonix 2020-04-02 13:45:56 -05:00
parent 0e6b874d5d
commit 4ab09044ab
7 changed files with 113 additions and 63 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "activitystreams" name = "activitystreams"
description = "Activity Streams in Rust" description = "Activity Streams in Rust"
version = "0.5.0-alpha.15" version = "0.5.0-alpha.16"
license = "GPL-3.0" license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams" repository = "https://git.asonix.dog/Aardwolf/activitystreams"
@ -17,7 +17,7 @@ primitives = ["chrono", "mime", "serde", "thiserror", "url"]
types = ["derive", "kinds", "primitives"] types = ["derive", "kinds", "primitives"]
[dependencies] [dependencies]
activitystreams-derive = { version = "0.5.0-alpha.7", path = "activitystreams-derive", optional = true} activitystreams-derive = { version = "0.5.0-alpha.8", path = "activitystreams-derive", optional = true}
chrono = { version = "0.4", optional = true } chrono = { version = "0.4", optional = true }
mime = { version = "0.3", optional = true } mime = { version = "0.3", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true }

View file

@ -9,7 +9,7 @@ __A set of Traits and Types that make up the ActivityStreams and ActivityPub spe
First, add ActivityStreams to your dependencies First, add ActivityStreams to your dependencies
```toml ```toml
activitystreams = "0.5.0-alpha.15" activitystreams = "0.5.0-alpha.16"
``` ```
### Types ### Types
@ -177,7 +177,7 @@ There are a number of features that can be disabled in this crate. By default, e
enabled. enabled.
```toml ```toml
activitystreams = { version = "0.5.0-alpha.15", default-features = "false", features = ["derive"] } activitystreams = { version = "0.5.0-alpha.16", default-features = "false", features = ["derive"] }
``` ```
| feature | what you get | | feature | what you get |

View file

@ -1,7 +1,7 @@
[package] [package]
name = "activitystreams-derive" name = "activitystreams-derive"
description = "Derive macros for activitystreams" description = "Derive macros for activitystreams"
version = "0.5.0-alpha.7" version = "0.5.0-alpha.8"
license = "GPL-3.0" license = "GPL-3.0"
authors = ["asonix <asonix.dev@gmail.com>"] authors = ["asonix <asonix.dev@gmail.com>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams" repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

@ -10,7 +10,7 @@ Add the required crates to your `Cargo.toml`
```toml ```toml
# Cargo.toml # Cargo.toml
activitystreams = "0.5.0-alpha.15" activitystreams = "0.5.0-alpha.16"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
``` ```

View file

@ -23,8 +23,8 @@
//! //!
//! First, add `serde` and `activitystreams-derive` to your Cargo.toml //! First, add `serde` and `activitystreams-derive` to your Cargo.toml
//! ```toml //! ```toml
//! activitystreams-derive = "0.5.0-alpha.7" //! activitystreams-derive = "0.5.0-alpha.8"
//! # or activitystreams = "0.5.0-alpha.15" //! # or activitystreams = "0.5.0-alpha.16"
//! serde = { version = "1.0", features = ["derive"] } //! serde = { version = "1.0", features = ["derive"] }
//! ``` //! ```
//! //!
@ -212,29 +212,6 @@ pub fn ref_derive(input: TokenStream) -> TokenStream {
BaseBox::from_concrete(s) BaseBox::from_concrete(s)
} }
} }
impl<T> std::convert::TryFrom<Ext<#name, T>> for BaseBox
where
T: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug,
{
type Error = std::io::Error;
fn try_from(s: Ext<#name, T>) -> Result<Self, Self::Error> {
BaseBox::from_concrete(s)
}
}
impl<T, U> std::convert::TryFrom<Ext<Ext<#name, T>, U>> for BaseBox
where
T: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug,
U: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug,
{
type Error = std::io::Error;
fn try_from(s: Ext<Ext<#name, T>, U>) -> Result<Self, Self::Error> {
BaseBox::from_concrete(s)
}
}
}; };
let trait_impls: proc_macro2::TokenStream = input let trait_impls: proc_macro2::TokenStream = input
.attrs .attrs
@ -261,29 +238,6 @@ pub fn ref_derive(input: TokenStream) -> TokenStream {
#box_name::from_concrete(s) #box_name::from_concrete(s)
} }
} }
impl<T> std::convert::TryFrom<Ext<#name, T>> for #box_name
where
T: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug,
{
type Error = std::io::Error;
fn try_from(s: Ext<#name, T>) -> Result<Self, Self::Error> {
#box_name::from_concrete(s)
}
}
impl<T, U> std::convert::TryFrom<Ext<Ext<#name, T>, U>> for #box_name
where
T: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug,
U: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug,
{
type Error = std::io::Error;
fn try_from(s: Ext<Ext<#name, T>, U>) -> Result<Self, Self::Error> {
#box_name::from_concrete(s)
}
}
}) })
} else { } else {
None None

View file

@ -109,14 +109,14 @@
//! ``` //! ```
use crate::{ use crate::{
activity::{Activity, IntransitiveActivity}, activity::{Activity, ActivityBox, IntransitiveActivity, IntransitiveActivityBox},
actor::Actor, actor::{Actor, ActorBox},
collection::{Collection, CollectionPage}, collection::{Collection, CollectionBox, CollectionPage, CollectionPageBox},
link::Link, link::{Link, LinkBox},
object::Object, object::{Object, ObjectBox},
Base, Base, BaseBox,
}; };
use std::fmt::Debug; use std::{convert::TryFrom, fmt::Debug};
/// Defines an extension to an activitystreams object or link /// Defines an extension to an activitystreams object or link
/// ///
@ -204,6 +204,102 @@ pub trait Extensible<U> {
Self: Sized; Self: Sized;
} }
impl<T, U> TryFrom<Ext<T, U>> for BaseBox
where
T: Base + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
BaseBox::from_concrete(e)
}
}
impl<T, U> TryFrom<Ext<T, U>> for ObjectBox
where
T: Object + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
ObjectBox::from_concrete(e)
}
}
impl<T, U> TryFrom<Ext<T, U>> for LinkBox
where
T: Link + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
LinkBox::from_concrete(e)
}
}
impl<T, U> TryFrom<Ext<T, U>> for CollectionBox
where
T: Collection + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
CollectionBox::from_concrete(e)
}
}
impl<T, U> TryFrom<Ext<T, U>> for CollectionPageBox
where
T: CollectionPage + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
CollectionPageBox::from_concrete(e)
}
}
impl<T, U> TryFrom<Ext<T, U>> for ActivityBox
where
T: Activity + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
ActivityBox::from_concrete(e)
}
}
impl<T, U> TryFrom<Ext<T, U>> for IntransitiveActivityBox
where
T: IntransitiveActivity + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
IntransitiveActivityBox::from_concrete(e)
}
}
impl<T, U> TryFrom<Ext<T, U>> for ActorBox
where
T: Actor + serde::ser::Serialize,
U: Extension<T> + serde::ser::Serialize + Debug,
{
type Error = std::io::Error;
fn try_from(e: Ext<T, U>) -> Result<Self, Self::Error> {
ActorBox::from_concrete(e)
}
}
impl<T, U> Extensible<U> for T impl<T, U> Extensible<U> for T
where where
T: crate::Base, T: crate::Base,

View file

@ -25,7 +25,7 @@
//! //!
//! First, add ActivityStreams to your dependencies //! First, add ActivityStreams to your dependencies
//! ```toml //! ```toml
//! activitystreams = "0.5.0-alpha.15" //! activitystreams = "0.5.0-alpha.16"
//! ``` //! ```
//! //!
//! ### Types //! ### Types
@ -209,7 +209,7 @@
//! enabled. //! enabled.
//! //!
//! ```toml //! ```toml
//! activitystreams = { version = "0.5.0-alpha.15", default-features = "false", features = ["derive"] } //! activitystreams = { version = "0.5.0-alpha.16", default-features = "false", features = ["derive"] }
//! ``` //! ```
//! //!
//! | feature | what you get | //! | feature | what you get |