Skip to content

Remove proc macro hack #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "serde_closure"
version = "0.2.11"
version = "0.2.12"
license = "MIT OR Apache-2.0"
authors = ["Alec Mocatta <[email protected]>"]
categories = ["development-tools","encoding","rust-patterns","network-programming"]
Expand All @@ -14,18 +14,17 @@ This library provides macros that wrap closures to make them serializable and de
"""
repository = "https://github.com/alecmocatta/serde_closure"
homepage = "https://github.com/alecmocatta/serde_closure"
documentation = "https://docs.rs/serde_closure/0.2.11"
documentation = "https://docs.rs/serde_closure/0.2.12"
readme = "README.md"
edition = "2018"

[badges]
azure-devops = { project = "alecmocatta/serde_closure", pipeline = "tests" }
azure-devops = { project = "alecmocatta/serde_closure", pipeline = "tests", build = "10" }
maintenance = { status = "actively-developed" }

[dependencies]
serde_closure_derive = { version = "=0.2.11", path = "serde_closure_derive" }
serde_closure_derive = { version = "=0.2.12", path = "serde_closure_derive" }
serde = { version = "1.0", features = ["derive"] }
proc-macro-hack = "0.5"

[dev-dependencies]
serde_json = "1.0"
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[![Crates.io](https://img.shields.io/crates/v/serde_closure.svg?maxAge=86400)](https://crates.io/crates/serde_closure)
[![MIT / Apache 2.0 licensed](https://img.shields.io/crates/l/serde_closure.svg?maxAge=2592000)](#License)
[![Build Status](https://dev.azure.com/alecmocatta/serde_closure/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/serde_closure/_build/latest?definitionId=10&branchName=master)
[![Build Status](https://dev.azure.com/alecmocatta/serde_closure/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/serde_closure/_build?definitionId=10)

[Docs](https://docs.rs/serde_closure/0.2.11)
[📖 Docs](https://docs.rs/serde_closure/0.2.12) | [💬 Chat](https://constellation.zulipchat.com/#narrow/stream/213236-subprojects)

Serializable and debuggable closures.

Expand All @@ -30,9 +30,9 @@ requires nightly Rust for the `unboxed_closures` and `fn_traits` features (rust
issue [#29625](https://github.com/rust-lang/rust/issues/29625)).

* There are three macros,
[`FnOnce`](https://docs.rs/serde_closure/0.2.11/serde_closure/macro.FnOnce.html),
[`FnMut`](https://docs.rs/serde_closure/0.2.11/serde_closure/macro.FnMut.html)
and [`Fn`](https://docs.rs/serde_closure/0.2.11/serde_closure/macro.Fn.html),
[`FnOnce`](https://docs.rs/serde_closure/0.2.12/serde_closure/macro.FnOnce.html),
[`FnMut`](https://docs.rs/serde_closure/0.2.12/serde_closure/macro.FnMut.html)
and [`Fn`](https://docs.rs/serde_closure/0.2.12/serde_closure/macro.Fn.html),
corresponding to the three types of Rust closure.
* Wrap your closure with one of the macros and it will now implement `Copy`,
`Clone`, `PartialEq`, `Eq`, `Hash`, `PartialOrd`, `Ord`, `Serialize`,
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
endpoint: alecmocatta
default:
rust_toolchain: nightly
rust_lint_toolchain: nightly-2020-04-23
rust_lint_toolchain: nightly-2020-06-09
rust_flags: ''
rust_features: ''
rust_target_check: ''
Expand Down
7 changes: 3 additions & 4 deletions serde_closure_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_closure_derive"
version = "0.2.11"
version = "0.2.12"
license = "MIT OR Apache-2.0"
authors = ["Alec Mocatta <[email protected]>"]
categories = ["development-tools","encoding","rust-patterns","network-programming"]
Expand All @@ -14,18 +14,17 @@ See https://crates.io/crates/serde_closure for documentation.
"""
repository = "https://github.com/alecmocatta/serde_closure"
homepage = "https://github.com/alecmocatta/serde_closure"
documentation = "https://docs.rs/serde_closure/0.2.11"
documentation = "https://docs.rs/serde_closure/0.2.12"
edition = "2018"

[badges]
azure-devops = { project = "alecmocatta/serde_closure", pipeline = "tests" }
azure-devops = { project = "alecmocatta/serde_closure", pipeline = "tests", build = "10" }
maintenance = { status = "actively-developed" }

[lib]
proc-macro = true

[dependencies]
proc-macro2 = { version = "1.0.1", default-features = false, features = ["span-locations"] }
proc-macro-hack = "0.5"
quote = { version = "1.0.2", default-features = false }
syn = { version = "1.0.5", default-features = false, features = ["clone-impls", "full", "parsing", "printing", "proc-macro"] }
20 changes: 10 additions & 10 deletions serde_closure_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
//! Serializable and debuggable closures.
//!
//! **[Crates.io](https://crates.io/crates/serde_closure) │
//! [Repo](https://github.com/alecmocatta/serde_closure)**
//! <p style="font-family: 'Fira Sans',sans-serif;padding:0.3em 0"><strong>
//! <a href="https://crates.io/crates/serde_closure">📦&nbsp;&nbsp;Crates.io</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://github.com/alecmocatta/serde_closure">📑&nbsp;&nbsp;GitHub</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://constellation.zulipchat.com/#narrow/stream/213236-subprojects">💬&nbsp;&nbsp;Chat</a>
//! </strong></p>
//!
//! This library provides macros that wrap closures to make them serializable
//! and debuggable.
//!
//! See [`serde_closure`](https://docs.rs/serde_closure/) for
//! documentation.

#![doc(html_root_url = "https://docs.rs/serde_closure_derive/0.2.11")]
#![doc(html_root_url = "https://docs.rs/serde_closure_derive/0.2.12")]
#![feature(proc_macro_diagnostic)]
#![allow(non_snake_case)] // due to proc-macro-hack can't apply this directly

extern crate proc_macro;

use proc_macro2::{Span, TokenStream};
use proc_macro_hack::proc_macro_hack;
use quote::{quote, ToTokens};
use std::{collections::HashSet, iter, iter::successors, mem::take, str};
use syn::{
parse::{Parse, ParseStream}, parse2, spanned::Spanned, token::Bracket, Arm, Block, Error, Expr, ExprArray, ExprAssign, ExprAssignOp, ExprAsync, ExprAwait, ExprBinary, ExprBlock, ExprBox, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprField, ExprForLoop, ExprGroup, ExprIf, ExprIndex, ExprLet, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprWhile, ExprYield, FieldValue, Ident, Local, Member, Pat, PatBox, PatIdent, PatReference, PatSlice, PatTuple, PatTupleStruct, PatType, Path, PathArguments, PathSegment, Stmt, Type, TypeInfer, TypeReference, UnOp
};

#[proc_macro_hack]
#[proc_macro]
#[allow(non_snake_case)]
pub fn Fn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
syn::parse::<Closure>(input)
.and_then(|closure| impl_fn_once(closure, Kind::Fn))
.unwrap_or_else(|err| err.to_compile_error())
.into()
}
#[proc_macro_hack]
#[proc_macro]
#[allow(non_snake_case)]
pub fn FnMut(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
syn::parse::<Closure>(input)
.and_then(|closure| impl_fn_once(closure, Kind::FnMut))
.unwrap_or_else(|err| err.to_compile_error())
.into()
}
#[proc_macro_hack]
#[proc_macro]
#[allow(non_snake_case)]
pub fn FnOnce(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
syn::parse::<Closure>(input)
.and_then(|closure| impl_fn_once(closure, Kind::FnOnce))
Expand Down
12 changes: 4 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Serializable and debuggable closures.
//!
//! **[Crates.io](https://crates.io/crates/serde_closure) │
//! [Repo](https://github.com/alecmocatta/serde_closure)**
//! <p style="font-family: 'Fira Sans',sans-serif;padding:0.3em 0"><strong>
//! <a href="https://crates.io/crates/serde_closure">📦&nbsp;&nbsp;Crates.io</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://github.com/alecmocatta/serde_closure">📑&nbsp;&nbsp;GitHub</a>&nbsp;&nbsp;│&nbsp;&nbsp;<a href="https://constellation.zulipchat.com/#narrow/stream/213236-subprojects">💬&nbsp;&nbsp;Chat</a>
//! </strong></p>
//!
//! This library provides macros that wrap closures to make them serializable
//! and debuggable.
Expand Down Expand Up @@ -164,7 +165,7 @@
//! automatically serializable and deserializable with
//! [`serde`](https://github.com/serde-rs/serde).

#![doc(html_root_url = "https://docs.rs/serde_closure/0.2.11")]
#![doc(html_root_url = "https://docs.rs/serde_closure/0.2.12")]
#![feature(unboxed_closures, fn_traits)]
#![warn(
missing_copy_implementations,
Expand All @@ -179,15 +180,12 @@
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
#![allow(clippy::inline_always)]

use proc_macro_hack::proc_macro_hack;

/// Macro that wraps a closure, evaluating to a [`FnOnce`](structs::FnOnce)
/// struct that implements [`std::ops::FnOnce`], [`Debug`](std::fmt::Debug),
/// [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize), and
/// various convenience traits.
///
/// See the [readme](self) for examples.
#[proc_macro_hack(fake_call_site)]
pub use serde_closure_derive::FnOnce;

/// Macro that wraps a closure, evaluating to a [`FnMut`](structs::FnMut) struct
Expand All @@ -196,7 +194,6 @@ pub use serde_closure_derive::FnOnce;
/// various convenience traits.
///
/// See the [readme](self) for examples.
#[proc_macro_hack(fake_call_site)]
pub use serde_closure_derive::FnMut;

/// Macro that wraps a closure, evaluating to a [`Fn`](structs::Fn) struct that
Expand All @@ -205,7 +202,6 @@ pub use serde_closure_derive::FnMut;
/// various convenience traits.
///
/// See the [readme](self) for examples.
#[proc_macro_hack(fake_call_site)]
pub use serde_closure_derive::Fn;

#[doc(hidden)]
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![deny(unsafe_code)] // TODO: make this forbid when unsafe in a macro doesn't trigger it (def_site?)
#![allow(clippy::no_effect, clippy::double_parens)]

use serde::{de::DeserializeOwned, Serialize};
use std::{fmt::Debug, mem::size_of};

#[macro_use]
extern crate serde_closure;
use serde_closure::{Fn, FnMut, FnOnce};

#[test]
fn fn_ptr_size() {
Expand Down