Skip to content

Commit

Permalink
Macro generates extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Apr 12, 2024
1 parent 3da44f6 commit bf986ce
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions golem-rust-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ proc-macro = true
path = "src/lib.rs"

[dependencies]
heck = "0.5.0"
proc-macro2 = "1.0.79"
quote = "1.0.36"
syn = { version = "2.0.58", features = ["extra-traits", "full", "fold"] }
41 changes: 28 additions & 13 deletions golem-rust-macro/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use heck::ToPascalCase;
use proc_macro::TokenStream;
use proc_macro2::Ident;

use quote::quote;
use syn::punctuated::Punctuated;
Expand Down Expand Up @@ -96,7 +98,7 @@ pub fn golem_operation_impl(args: TokenStream, item: TokenStream) -> TokenStream
fnsig.inputs.insert(
0,
parse_quote! {
tx: &mut impl golem_rust::Transaction<#err>
self
},
);

Expand All @@ -107,19 +109,32 @@ pub fn golem_operation_impl(args: TokenStream, item: TokenStream) -> TokenStream
_ => panic!("Expected function to have a return type of Result<_, _>"),
};

let traitname = Ident::new(
&fnsig.ident.to_string().to_pascal_case(),
fnsig.ident.span(),
);

let result = quote! {
#fnsig {
tx.execute(
golem_rust::#operation(
|#input_pattern| {
#body
},
|#compensation_pattern| {
#compensate(#(#compensation_args), *)
}
),
(#(#input_args), *)
)
#ast

trait #traitname {
#fnsig;
}

impl<T: golem_rust::Transaction<#err>> #traitname for &mut T {
#fnsig {
self.execute(
golem_rust::#operation(
|#input_pattern| {
#body
},
|#compensation_pattern| {
#compensate(#(#compensation_args), *)
}
),
(#(#input_args), *)
)
}
}
};

Expand Down
7 changes: 4 additions & 3 deletions golem-rust/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,9 @@ mod macro_tests {
fn tx_test_1() {
let result = fallible_transaction(|tx| {
println!("Executing the annotated function as an operation directly");
test_operation(tx, 1, 0.1)?;
test_operation_2(tx, 1, 0.1)?;
tx.test_operation(1, 0.1)?;
tx.test_operation_2(1, 0.1)?;

Ok(11)
});

Expand All @@ -567,7 +568,7 @@ mod macro_tests {
fn tx_test_2() {
let result = infallible_transaction(|tx| {
println!("Executing the annotated function as an operation directly");
let _ = test_operation(tx, 1, 0.1);
let _ = tx.test_operation(1, 0.1);
11
});

Expand Down

0 comments on commit bf986ce

Please sign in to comment.