actix_handler_macro
Helper macros for using Actix.
use Message;
use ;
;
type ExampleContext = Context;
actor_derive; expand_addr; expand_impl_handlers; expand_method_handlers; options; utils;
expand_impl_handlers::expand_item_impl; options::{parse_options, Options}; proc_macro::TokenStream; quote::quote; syn::{parse_macro_input, AttributeArgs, DeriveInput, Item}; utils::compilation_error;
Allows writing Actix actors with impl blocks.
Motivation
Actix can be quite verbose when declaring handlers:
use Actor;
use Context;
use Handler;
use Message;
;
actix_derive_macro
reads an impl
block and generates a bit of this code for each method.
Usage
use ;
use ;
;
type ExampleContext = ;
This will expand a Handler<Greeting>
impl for each method in Example.
Actor ...Addr
trait
It'll also output a trait GreetingAddr
and its implementation for Addr<Example>
with
convenience methods:
// Example output
trait GreetingAddr {
fn greet(self: &Self, msg: Greeting) -> actix::prelude::Request<Example, Greeting>;
}
RecipientRequest
Optionally, the trait can use a actix::Recipient
and return a actix::RecipientRequest
.
use ;
use ;
;
type ExampleContext = Context;
#[actix_handler(use_recipient)]
will expand the GreetingAddr
trait as:
// Example output
trait GreetingAddr {
fn greet(self: &Self, msg: Greeting) -> actix::RecipientRequest<Greeting>;
}
A mock of the actor could be implemented as:
use actix::Message;
use actix_handler_macro::Actor;
#[derive(Actor)]
struct Example;
#[derive(Message)]
#[rtype(result = "String")]
struct Greeting;
#[derive(Actor)]
struct ExampleMock {
mocker: actix::Addr<actix::actors::mocker::Mocker<Example>>,
}
impl GreetingAddr for ExampleMock {
fn greet(self: &Self, msg: Greeting) -> actix::prelude::RecipientRequest<Greeting> {
self.mocker.clone().recipient().send(msg)
}
}