Git Product home page Git Product logo

actix-derive's Introduction

actix-derive Build Status crates.io

Actix is a rust actor framework.


Features

  • actix-derive adds support for Rust Custom Derive / Macros 1.1 to actix

Usage

use actix_derive::{Message, MessageResponse};

#[derive(MessageResponse)]
struct Added(usize);

#[derive(Message)]
#[rtype(result = "Added")]
struct Sum(usize, usize);

fn main() {}

This code expands into following code:

use actix::{Actor, Context, Handler, System};
use actix_derive::{Message, MessageResponse};

#[derive(MessageResponse)]
struct Added(usize);

#[derive(Message)]
#[rtype(result = "Added")]
struct Sum(usize, usize);

#[derive(Default)]
struct Adder;

impl Actor for Adder {
    type Context = Context<Self>;
}

impl Handler<Sum> for Adder {
    type Result = <Sum as actix::Message>::Result;
    fn handle(&mut self, msg: Sum, _: &mut Self::Context) -> Added {
        Added(msg.0 + msg.1)
    }
}

fn main() {}

License

This project is licensed under either of

at your option.

actix-derive's People

Contributors

ava57r avatar boncheolgu avatar callym avatar dansnow avatar doumanash avatar fafhrd91 avatar johntitor avatar kingoflolz avatar mehcode avatar messense avatar samrg472 avatar yuja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

actix-derive's Issues

naming

i don't like Message for response, let's rename it to response.
also, i dont think we need MessageResult or MessageError

#[derive(Actor)]

Should we add a custom derive for the Actor trait?

#[derive(Actor)]
struct SumActor;

Which expands to:

impl Actor for SumActor {
    type Context = Context<Self>;
}

#[derive(MessageResponse)]

I think it would make sense to add a MessageResponse custom derive, along the lines of the SIMPLE_RESULT macro. I know there's the MessageResult helper, but I prefer not having to wrap my result types.

I've read through the issues #1 and #2, and understand the reluctance to add new macros if they only save a small amount of code, but in this case (as shown by the existence of SIMPLE_RESULT ;-)), I think the boilerplate saved is enough to justify a custom derive.

Using `()` as the default Message result type

What about using () as the default result type? Currently I need to use a lot of boilerplate code with the rtype attribute, like this:

#[derive(Message, Debug)]
#[rtype(result = "()")]
pub struct SomeMessage {
    pub name: String,
}

More advanced macros

I implemented a #[handler] macro to allow you to tag functions as a handler for a type, and generate the necessary code implementing the Handler 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.

#[handler]
impl SumActor {
    #[handle(Sum)]
    fn sum(&mut self, message: Sum, _: &mut Context<Self>) -> Response<Self, Sum> {
        println!("{}", message.0 + message.1);
        Self::reply(message.0 + message.1)
    }
}

Which would expand to:

impl SumActor {
    fn sum(&mut self, message: Sum, _: &mut Context<Self>) -> Response<Self, Sum> {
        println!("{}", message.0 + message.1);
        Self::reply(message.0 + message.1)
    }
}

impl Handler<Sum> for SumActor {
    fn handle(&mut self, message: Sum, context: &mut Context<Self>) -> Response<Self, Sum> {
        self.sum(message, context)
    }
}

Way of specifying none in derive message

Hi,

Is there a way of using this lib to derive a message which has an empty Item as well as an empty Error?

The way I'm currently doing it is

type none = ();

#[derive(Message)]
#[Message(none, none)]
struct Wake;

Thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.