Skip to content

Commit

Permalink
[x/programs] add cargo fmt to rust linter (#400)
Browse files Browse the repository at this point in the history
* [x/programs] add cargo fmt to rust linter

Signed-off-by: Sam Batschelet <sam.batschelet@avalabs.org>

* [expose_macro] fmt

Signed-off-by: Sam Batschelet <sam.batschelet@avalabs.org>

---------

Signed-off-by: Sam Batschelet <sam.batschelet@avalabs.org>
  • Loading branch information
hexfusion authored Aug 24, 2023
1 parent 2ac391b commit d0f298d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions scripts/rust/tests.lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ fi
# https://rust-lang.github.io/rustup/installation/index.html
# rustup toolchain install nightly --allow-downgrade --profile minimal --component clippy

rustup default stable

cargo fmt --all --verbose -- --check

rustup default nightly

# use rust nightly clippy to check tests and all crate features
Expand Down
13 changes: 7 additions & 6 deletions x/programs/rust/expose_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ extern crate proc_macro;
use core::panic;

use proc_macro::TokenStream;
use quote::{ToTokens, quote};
use syn::{Pat, parse_str, Type, parse_macro_input, FnArg, Ident, ItemFn, PatType};

use quote::{quote, ToTokens};
use syn::{parse_macro_input, parse_str, FnArg, Ident, ItemFn, Pat, PatType, Type};

/// An attribute procedural macro that can be used to expose a function to the host.
/// It does so by wrapping the [item] tokenstream in a new function that can be called by the host.
Expand All @@ -28,7 +27,9 @@ pub fn expose(_: TokenStream, item: TokenStream) -> TokenStream {
let param_type = if is_supported_primitive(ty) {
ty.to_token_stream()
} else {
parse_str::<Type>("i64").expect("valid i64 type").to_token_stream()
parse_str::<Type>("i64")
.expect("valid i64 type")
.to_token_stream()
};
return (param_name, param_type);
}
Expand All @@ -50,11 +51,11 @@ pub fn expose(_: TokenStream, item: TokenStream) -> TokenStream {
let return_type = &input.sig.output;
let output = quote! {
// Need to include the original function in the output, so contract can call itself
#input
#input
#[no_mangle]
pub extern "C" fn #new_name(#(#param_names: #param_types), *) #return_type {
// .into() uses the From() on each argument in the iterator to convert it to the type we want. 70% sure about this statement.
#name(#(#param_names_cloned.into()),*) // This means that every parameter type must implement From<i64>(except for the supported primitive types).
#name(#(#param_names_cloned.into()),*) // This means that every parameter type must implement From<i64>(except for the supported primitive types).
}
};
TokenStream::from(output)
Expand Down

0 comments on commit d0f298d

Please sign in to comment.