Skip to content

Add proc-macro for convenient creation of Handlers #111

Open
@thomaseizinger

Description

@thomaseizinger

Currently, defining a Handler requires the usage of async_trait and filling in various elements into a trait impl.

In theory however, a macro can learn all required pieces of information from the following code block:

impl Actor {
	pub async fn handle_message(&mut self, message: Message) -> i32 {
		todo!()
	}
}

This needs to be expanded to:

#[async_trait::async_trait]
impl Handler<Message> for Actor {
	type Return = i32;

	fn handle(&mut self, message: Message, _ctx: &mut Context<Self>) -> i32 {
		todo!()
	}
}

This is what https://github.com/comit-network/xtra-productivity does although it is not yet updated to the latest version of xtra where f.e. the Message type is already removed. @klochowicz and myself authored that crate at my last gig. I think it would be nice to pull this into this repository as a separate crate, re-export it through xtra and expose it as xtra::handler so it can be used like:

#[xtra::handler]
impl Actor {
	// function definitions here
}

The main features of the macro are:

  • Creates 1 impl Handler per fn in the impl block
  • Removes duplicate mention of message and return type
  • Allows omitting Context if not used

https://github.com/comit-network/xtra-productivity would need a lot of polish before it can be officially released, esp. in regards to error messages. The implementation isn't too complex so we can also start from scratch if necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions