Skip to content

Commit

Permalink
[refactor] #3911: Migrate iroha_futures_derive to syn 2.0
Browse files Browse the repository at this point in the history
Also, a drive-by fix to silence the "unused" lints in some other feature combinations

Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 authored and 6r1d committed Oct 17, 2023
1 parent 99b659e commit 93ffbff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion crypto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! This module contains structures and implementations related to the cryptographic parts of the Iroha.
#![cfg_attr(not(feature = "std"), no_std)]
// in no_std some code gets cfg-ed out, so we silence the warnings
#![cfg_attr(not(feature = "std"), allow(unused, unused_tuple_struct_fields))]
#![allow(clippy::arithmetic_side_effects)]

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -120,6 +119,10 @@ impl FromStr for Algorithm {

/// Options for key generation
#[cfg(not(feature = "ffi_import"))]
#[cfg_attr(
any(not(feature = "std"), feature = "ffi_import"),
allow(unused_tuple_struct_fields)
)]
#[derive(Debug, Clone)]
enum KeyGenOption {
/// Use seed
Expand Down
6 changes: 4 additions & 2 deletions futures/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ telemetry = []
proc-macro = true

[dependencies]
syn = { workspace = true, features = ["default", "full"] }
iroha_macro_utils = { workspace = true }

syn2 = { workspace = true, features = ["default", "full"] }
quote = { workspace = true }
proc-macro2 = { workspace = true }
proc-macro-error = { workspace = true }
manyhow = { workspace = true }
39 changes: 25 additions & 14 deletions futures/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
clippy::std_instead_of_core
)]

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::{abort, proc_macro_error};
use iroha_macro_utils::Emitter;
use manyhow::{emit, manyhow};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Generics, ItemFn, ReturnType, Signature};
use syn2::{Generics, ItemFn, ReturnType, Signature};

fn impl_telemetry_future(
emitter: &mut Emitter,
ItemFn {
attrs,
vis,
sig,
block,
}: ItemFn,
) -> TokenStream2 {
) -> TokenStream {
let Signature {
asyncness,
ident,
Expand All @@ -34,8 +35,9 @@ fn impl_telemetry_future(
} = sig;

if asyncness.is_none() {
abort!(
asyncness,
emit!(
emitter,
ident,
"Only async functions can be instrumented for `telemetry_future`"
);
}
Expand All @@ -57,14 +59,23 @@ fn impl_telemetry_future(
}

/// Macro for wrapping future for getting telemetry info about poll times and numbers
#[proc_macro_error]
#[manyhow]
#[proc_macro_attribute]
pub fn telemetry_future(_args: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemFn);
if cfg!(feature = "telemetry") {
impl_telemetry_future(input)
pub fn telemetry_future(args: TokenStream, input: TokenStream) -> TokenStream {
let mut emitter = Emitter::new();

if !args.is_empty() {
emit!(emitter, args, "Unexpected arguments")
}

let Some(input) = emitter.handle(syn2::parse2(input)) else {
return emitter.finish_token_stream();
};
let result = if cfg!(feature = "telemetry") {
impl_telemetry_future(&mut emitter, input)
} else {
quote! { #input }
}
.into()
};

emitter.finish_token_stream_with(result)
}

0 comments on commit 93ffbff

Please sign in to comment.