Skip to content

Commit

Permalink
Use #[proc_macro] at Rust 1.45+
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed May 5, 2021
1 parent cb0711e commit 8666a25
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
3 changes: 3 additions & 0 deletions futures-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ proc-macro = true

[features]

[build-dependencies]
autocfg = "1"

[dependencies]
proc-macro2 = "1.0"
proc-macro-hack = "0.5.19"
Expand Down
26 changes: 26 additions & 0 deletions futures-macro/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![warn(rust_2018_idioms, single_use_lifetimes)]

use autocfg::AutoCfg;

// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
let cfg = match AutoCfg::new() {
Ok(cfg) => cfg,
Err(e) => {
println!(
"cargo:warning={}: unable to determine rustc version: {}",
env!("CARGO_PKG_NAME"),
e
);
return;
}
};

// Function like procedural macros in expressions patterns statements stabilized in Rust 1.45:
// https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html#stabilizing-function-like-procedural-macros-in-expressions-patterns-and-statements
if cfg.probe_rustc_version(1, 45) {
println!("cargo:rustc-cfg=fn_like_proc_macro");
}
}
13 changes: 8 additions & 5 deletions futures-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,34 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro_hack::proc_macro_hack;

mod join;
mod select;

/// The `join!` macro.
#[proc_macro_hack]
#[cfg_attr(fn_like_proc_macro, proc_macro)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack)]
pub fn join_internal(input: TokenStream) -> TokenStream {
crate::join::join(input)
}

/// The `try_join!` macro.
#[proc_macro_hack]
#[cfg_attr(fn_like_proc_macro, proc_macro)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack)]
pub fn try_join_internal(input: TokenStream) -> TokenStream {
crate::join::try_join(input)
}

/// The `select!` macro.
#[proc_macro_hack]
#[cfg_attr(fn_like_proc_macro, proc_macro)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack)]
pub fn select_internal(input: TokenStream) -> TokenStream {
crate::select::select(input)
}

/// The `select_biased!` macro.
#[proc_macro_hack]
#[cfg_attr(fn_like_proc_macro, proc_macro)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack)]
pub fn select_biased_internal(input: TokenStream) -> TokenStream {
crate::select::select_biased(input)
}
3 changes: 3 additions & 0 deletions futures-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ bilock = []
read-initializer = ["io", "futures-io/read-initializer", "futures-io/unstable"]
write-all-vectored = ["io"]

[build-dependencies]
autocfg = "1"

[dependencies]
futures-core = { path = "../futures-core", version = "=1.0.0-alpha.0", default-features = false }
futures-task = { path = "../futures-task", version = "=0.4.0-alpha.0", default-features = false }
Expand Down
26 changes: 26 additions & 0 deletions futures-util/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![warn(rust_2018_idioms, single_use_lifetimes)]

use autocfg::AutoCfg;

// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
let cfg = match AutoCfg::new() {
Ok(cfg) => cfg,
Err(e) => {
println!(
"cargo:warning={}: unable to determine rustc version: {}",
env!("CARGO_PKG_NAME"),
e
);
return;
}
};

// Function like procedural macros in expressions patterns statements stabilized in Rust 1.45:
// https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html#stabilizing-function-like-procedural-macros-in-expressions-patterns-and-statements
if cfg.probe_rustc_version(1, 45) {
println!("cargo:rustc-cfg=fn_like_proc_macro");
}
}
8 changes: 4 additions & 4 deletions futures-util/src/async_await/join_mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! The `join` macro.

use proc_macro_hack::proc_macro_hack;

macro_rules! document_join_macro {
($join:item $try_join:item) => {
/// Polls multiple futures simultaneously, returning a tuple
Expand Down Expand Up @@ -81,12 +79,14 @@ macro_rules! document_join_macro {
}
}

#[allow(unreachable_pub)]
#[doc(hidden)]
#[proc_macro_hack(support_nested, only_hack_old_rustc)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack(support_nested))]
pub use futures_macro::join_internal;

#[allow(unreachable_pub)]
#[doc(hidden)]
#[proc_macro_hack(support_nested, only_hack_old_rustc)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack(support_nested))]
pub use futures_macro::try_join_internal;

document_join_macro! {
Expand Down
8 changes: 4 additions & 4 deletions futures-util/src/async_await/select_mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! The `select` macro.

use proc_macro_hack::proc_macro_hack;

macro_rules! document_select_macro {
// This branch is required for `futures 0.3.1`, from before select_biased was introduced
($select:item) => {
Expand Down Expand Up @@ -309,12 +307,14 @@ macro_rules! document_select_macro {
}

#[cfg(feature = "std")]
#[allow(unreachable_pub)]
#[doc(hidden)]
#[proc_macro_hack(support_nested, only_hack_old_rustc)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack(support_nested))]
pub use futures_macro::select_internal;

#[allow(unreachable_pub)]
#[doc(hidden)]
#[proc_macro_hack(support_nested, only_hack_old_rustc)]
#[cfg_attr(not(fn_like_proc_macro), proc_macro_hack::proc_macro_hack(support_nested))]
pub use futures_macro::select_biased_internal;

document_select_macro! {
Expand Down

0 comments on commit 8666a25

Please sign in to comment.