Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace actors framework events boilerplate code with macros #237

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pallet-storage-providers-runtime-api = { path = "pallets/providers/runtime-api",

# Local - StorageHub Client (used by the node, can be std or no_std)
shc-actors-framework = { path = "client/actors-framework", default-features = false }
shc-actors-framework-macro = { path = "client/actors-framework-macro", default-features = false }
shc-blockchain-service = { path = "client/blockchain-service", default-features = false }
shc-file-transfer-service = { path = "client/file-transfer-service", default-features = false }
shc-indexer-service = { path = "client/indexer-service", default-features = false }
Expand Down
9 changes: 9 additions & 0 deletions client/actors-framework-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "actors-framework-macro"
version = "0.1.0"
edition = "2018"

[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["full"] }
17 changes: 17 additions & 0 deletions client/actors-framework-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(EventBusMessage)]
pub fn derive_event_bus_message(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;

let expanded = quote! {
impl EventBusMessage for #name {}
};

TokenStream::from(expanded)
}
4 changes: 3 additions & 1 deletion client/actors-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ sc-service = { workspace = true }
sc-utils = { workspace = true }

sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-runtime = { workspace = true }

actors-framework-macro = { workspace = true }
34 changes: 34 additions & 0 deletions client/actors-framework/src/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,37 @@ impl<T: EventBusMessage, E: EventHandler<T> + Send + 'static> EventBusListener<T
spawner.spawn(async move { self.run().await });
}
}

#[macro_export]
macro_rules! define_event_bus {
($provider_name:ident, $($event_type:ty),+) => {
#[derive(Clone, Default)]
pub struct $provider_name {
$(pub $event_type: EventBus<$event_type>,)+
}

$(
impl ProvidesEventBus<$event_type> for $provider_name {
fn event_bus(&self) -> &EventBus<$event_type> {
&self.$event_type
}
}
)+
};
}

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(EventBusMessage)]
pub fn derive_event_bus_message(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;

let expanded = quote! {
impl EventBusMessage for #name {}
};

TokenStream::from(expanded)
}
2 changes: 2 additions & 0 deletions client/blockchain-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ pallet-proofs-dealer = { workspace = true }
pallet-proofs-dealer-runtime-api = { workspace = true }
pallet-storage-providers = { workspace = true }
pallet-storage-providers-runtime-api = { workspace = true }

actors-framework-macro = { workspace = true }
Loading
Loading