Browse Source

Use xtra-productivity from comit-network

shutdown-taker-if-no-maker-present
Mariusz Klochowicz 3 years ago
parent
commit
a6392c6d00
No known key found for this signature in database GPG Key ID: 470C865699C8D4D
  1. 19
      Cargo.lock
  2. 3
      Cargo.toml
  3. 2
      daemon/Cargo.toml
  4. 17
      xtra_productivity/Cargo.toml
  5. 107
      xtra_productivity/src/lib.rs
  6. 27
      xtra_productivity/tests/pass/actor_with_generics.rs
  7. 62
      xtra_productivity/tests/pass/can_handle_message.rs
  8. 6
      xtra_productivity/tests/ui.rs

19
Cargo.lock

@ -3391,20 +3391,6 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "trybuild"
version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "150e726dc059e6fbd4fce3288f5bb3cf70128cf63b0dde23b938a3cad810fb23"
dependencies = [
"glob",
"lazy_static",
"serde",
"serde_json",
"termcolor",
"toml",
]
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.14.0" version = "0.14.0"
@ -3846,13 +3832,10 @@ dependencies = [
[[package]] [[package]]
name = "xtra_productivity" name = "xtra_productivity"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/comit-network/xtra-productivity?rev=eda11a682dbbf16a9004cfe5e30baa1c60062a67#eda11a682dbbf16a9004cfe5e30baa1c60062a67"
dependencies = [ dependencies = [
"async-trait",
"quote", "quote",
"syn", "syn",
"tokio",
"trybuild",
"xtra",
] ]
[[package]] [[package]]

3
Cargo.toml

@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["daemon", "xtra_productivity"] members = ["daemon"]
resolver = "2" resolver = "2"
[patch.crates-io] [patch.crates-io]
@ -7,3 +7,4 @@ rocket = { git = "https://github.com/SergioBenitez/Rocket" } # Need to patch roc
xtra = { git = "https://github.com/Restioson/xtra" } # Latest master has crucial patches. xtra = { git = "https://github.com/Restioson/xtra" } # Latest master has crucial patches.
secp256k1-zkp = { git = "https://github.com/ElementsProject/rust-secp256k1-zkp" } # Latest master has crucial patches. secp256k1-zkp = { git = "https://github.com/ElementsProject/rust-secp256k1-zkp" } # Latest master has crucial patches.
maia = { git = "https://github.com/comit-network/maia", rev = "70fc548da0fe4f34478fb34ec437fa9a434c7ee3" } maia = { git = "https://github.com/comit-network/maia", rev = "70fc548da0fe4f34478fb34ec437fa9a434c7ee3" }
xtra_productivity = { git = "https://github.com/comit-network/xtra-productivity", rev = "eda11a682dbbf16a9004cfe5e30baa1c60062a67" }

2
daemon/Cargo.toml

@ -46,7 +46,7 @@ tracing-subscriber = { version = "0.2", default-features = false, features = ["f
uuid = { version = "0.8", features = ["serde", "v4"] } uuid = { version = "0.8", features = ["serde", "v4"] }
x25519-dalek = { version = "1.1" } x25519-dalek = { version = "1.1" }
xtra = { version = "0.6", features = ["with-tokio-1"] } xtra = { version = "0.6", features = ["with-tokio-1"] }
xtra_productivity = { path = "../xtra_productivity" } xtra_productivity = { version = "0.1.0" }
[features] [features]
# Feature flag to enable tests that take longer to compile. # Feature flag to enable tests that take longer to compile.

17
xtra_productivity/Cargo.toml

@ -1,17 +0,0 @@
[package]
name = "xtra_productivity"
version = "0.1.0"
edition = "2018"
[lib]
proc-macro = true
[dependencies]
quote = "1"
syn = { version = "1", features = ["full"] }
[dev-dependencies]
async-trait = "0.1.51"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
trybuild = "1"
xtra = { version = "0.6", features = ["with-tokio-1"] }

107
xtra_productivity/src/lib.rs

@ -1,107 +0,0 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{FnArg, GenericParam, ImplItem, ItemImpl, MetaNameValue, ReturnType};
#[proc_macro_attribute]
pub fn xtra_productivity(attribute: TokenStream, item: TokenStream) -> TokenStream {
let block = syn::parse::<ItemImpl>(item).unwrap();
let want_message_impl = if attribute.is_empty() {
true
} else {
let attribute = syn::parse::<MetaNameValue>(attribute).unwrap();
if !attribute.path.is_ident("message_impl") {
panic!(
"Unexpected attribute {:?}",
attribute.path.get_ident().unwrap()
)
}
matches!(
attribute.lit,
syn::Lit::Bool(syn::LitBool { value: true, .. })
)
};
let actor = block.self_ty;
let generic_params = &block.generics.params;
let generic_types = block
.generics
.params
.iter()
.filter_map(|param| match param {
GenericParam::Type(ty) => Some(ty.ident.clone()),
_ => None,
})
.collect::<Vec<_>>();
let additional_bounds = block
.generics
.where_clause
.map(|bounds| {
let predicates = bounds.predicates;
quote! {
#predicates
}
})
.unwrap_or_default();
let code = block
.items
.into_iter()
.filter_map(|block_item| match block_item {
ImplItem::Method(method) => Some(method),
_ => None,
})
.map(|method| {
let message_arg = method.sig.inputs[1].clone();
let message_type = match message_arg {
// receiver represents self
FnArg::Receiver(_) => unreachable!("cannot have receiver on second position"),
FnArg::Typed(ref typed) => typed.ty.clone()
};
let method_return = method.sig.output;
let method_block = method.block;
let context_arg = method.sig.inputs.iter().nth(2).map(|fn_arg| quote! { #fn_arg }).unwrap_or_else(|| quote! {
_ctx: &mut xtra::Context<Self>
});
let result_type = match method_return {
ReturnType::Default => quote! { () },
ReturnType::Type(_, ref t) => quote! { #t }
};
let message_impl = if want_message_impl {
quote! {
impl xtra::Message for #message_type {
type Result = #result_type;
}
}
} else {
quote! {}
};
quote! {
#message_impl
#[async_trait::async_trait]
impl<#generic_params> xtra::Handler<#message_type> for #actor
where
#additional_bounds
#(#generic_types: Send + 'static),*
{
async fn handle(&mut self, #message_arg, #context_arg) #method_return #method_block
}
}
}).collect::<Vec<_>>();
(quote! {
#(#code)*
})
.into()
}

27
xtra_productivity/tests/pass/actor_with_generics.rs

@ -1,27 +0,0 @@
use std::marker::PhantomData;
use xtra_productivity::xtra_productivity;
struct ActorWithParam<C> {
ty: PhantomData<C>,
}
struct DummyMessage;
trait Foo {}
impl<C: 'static + Send> xtra::Actor for ActorWithParam<C> {}
// Dummy actor, xtra::Handler and xtra::Message impls generated by xtra_productivity
#[xtra_productivity]
impl<C> ActorWithParam<C>
where
C: Foo,
{
pub fn handle_dummy_message(&mut self, _message: DummyMessage) {
assert_impls_foo::<C>();
}
}
fn assert_impls_foo<T: Foo>() {}
fn main() {}

62
xtra_productivity/tests/pass/can_handle_message.rs

@ -1,62 +0,0 @@
use xtra::spawn::TokioGlobalSpawnExt;
use xtra::Actor;
use xtra_productivity::xtra_productivity;
struct DummyActor;
impl xtra::Actor for DummyActor {}
#[derive(Clone)]
struct DummyMessage;
struct DummyMessageWithContext;
// Dummy actor, xtra::Handler and xtra::Message impls generated by xtra_productivity
#[xtra_productivity]
impl DummyActor {
pub fn handle_dummy_message(&mut self, message: DummyMessage) -> i32 {
let _ = message.clone();
0
}
pub fn handle_dummy_message_with_context(
&mut self,
_message: DummyMessageWithContext,
context: &mut xtra::Context<Self>,
) {
let _ = context.address();
}
}
fn is_i32(_: i32) {}
struct DummyMessageWithoutMessageImpl;
#[xtra_productivity(message_impl = false)]
impl DummyActor {
pub fn handle_dummy_message_without_message_impl(
&mut self,
_message: DummyMessageWithoutMessageImpl,
) {
}
}
impl xtra::Message for DummyMessageWithoutMessageImpl {
type Result = ();
}
#[tokio::main]
async fn main() {
// Create dummy actor
let dummy_actor = DummyActor.create(None).spawn_global();
// Send message to dummy actor
let i32 = dummy_actor.send(DummyMessage).await.unwrap();
is_i32(i32);
dummy_actor.send(DummyMessageWithContext).await.unwrap();
dummy_actor
.send(DummyMessageWithoutMessageImpl)
.await
.unwrap();
}

6
xtra_productivity/tests/ui.rs

@ -1,6 +0,0 @@
#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
t.pass("tests/pass/*.rs");
}
Loading…
Cancel
Save