-
Notifications
You must be signed in to change notification settings - Fork 38
More advanced macros #2
Comments
i see #[derive(Message)]
#[response(usize)]
struct Sum {
a: usize,
b: usize,
}
#[handler]
impl SumActor {
#[handle(Sum)]
fn sum(&mut self, a: usize, b: usize) -> usize {
println!("{}", a + b);
a + b
}
} expands to: impl SumActor {
fn sum(&mut self, a: usize, b: usize) -> usize {
println!("{}", a + b);
a + b
}
}
impl Handler<Sum> for SumActor {
fn handle(&mut self, message: Sum, context: &mut Context<Self>) -> Response<Self, Sum> {
Self::reply(self.sum(message.a, message.b))
}
} |
In your example, how would you go from What about: #[handler]
impl VectorActor {
#[handle(Sum)]
fn magnitude(&mut self, message: Sum) -> usize {
sum.0 + sum.1
}
} With the expansion being: impl Handler<Sum> for SumActor {
fn handle(&mut self, message: Sum, context: &mut Context<Self>) -> Response<Self, Sum> {
Self::reply(self.sum(message))
}
} For messages with #[handle(Message)]
fn reply(&mut self, message: Message) {
// do work here
} This still hides the |
I do not think tuple struct should be supported. and with normal struct we can destructor it by names, which we can take from there is no point to introduce new macro just to reduce code by couple lines. and point of macro should be ergonomic. |
90% use case coverage for new macro should be enough, but this 90% should really useful. |
I've been thinking about this, and have something that works like #[handler]
impl SomeActor {
#[handle]
fn add(&self, sum: Sum) -> usize {
sum.0 + sum.1
}
#[handle]
fn empty(&mut self, _: Empty, ctx: &mut Context<Self>) {
// do whatever here
}
} The message type is taken from the |
I implemented a
#[handler]
macro to allow you to tag functions as a handler for a type, and generate the necessary code implementing theHandler
trait for the type.Is this useful?
It'd require Nightly because it depends on
feature(proc_macro)
, but that can be feature-gated, so the custom derives would still work on Stable.Which would expand to:
The text was updated successfully, but these errors were encountered: