Skip to content

Commit

Permalink
[refactor] hyperledger-iroha#3911: Migrate iroha_futures_derive to sy…
Browse files Browse the repository at this point in the history
…n 2.0

Also, a drive-by fix to silence the "unused" lints in some other feature combinations

Signed-off-by: Nikita Strygin <dcnick3@users.noreply.github.com>
  • Loading branch information
DCNick3 committed Sep 26, 2023
1 parent a706e72 commit baa592b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 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
4 changes: 3 additions & 1 deletion crypto/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use ursa::{
},
};

use crate::{ffi, Error, PublicKey};
#[cfg(any(feature = "std", feature = "import_ffi"))]
use crate::Error;
use crate::{ffi, PublicKey};
#[cfg(feature = "std")]
use crate::{HashOf, KeyPair};

Expand Down
6 changes: 5 additions & 1 deletion data_model/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
use alloc::{boxed::Box, format, string::String, vec::Vec};
use core::{fmt::Display, time::Duration};

#[cfg(feature = "std")]
#[cfg(feature = "transparent_api")]
use iroha_crypto::KeyPair;

use derive_more::Display;
use getset::Getters;
use iroha_crypto::{HashOf, KeyPair, MerkleTree, SignaturesOf};
use iroha_crypto::{HashOf, MerkleTree, SignaturesOf};
use iroha_data_model_derive::model;
use iroha_macro::FromVariant;
use iroha_schema::IntoSchema;
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 baa592b

Please sign in to comment.