#actor #actor-model #async #message #primitive #handle #junkie

absinthe

你是个 Actor & 异步狂热者吗?那么你的应用程序就需要一些 Absinthe!

3 个版本 (破坏性更新)

0.3.0 2023 年 8 月 5 日
0.2.0 2023 年 7 月 30 日
0.1.0 2023 年 7 月 28 日

#1741 in 异步

MIT/Apache

13KB
150

Absinthe

你是个 Actor & 异步狂热者吗?那么你的应用程序就需要一些 Absinthe!

什么是 Absinthe?

Absinthe 是一个库,允许你在 Rust 中创建 Actor,并使用异步消息与它们进行通信。它提供了 Actor Model 基础设施,并有一个超级宏来轻松创建 Actor。只需编写你的 Rust 代码,Absinthe 就会处理其余部分。

Absinthe 动态演示

use absinthe::prelude::*;

// Actorize any async function with the #[absinthe::actor] attribute
actor! {
    async fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

// It works with generics too!
// Don't think about async requirements, Absinthe will handle it for you
actor! {
    async fn sub<T>(a: T, b: T) 
    where 
        T: std::ops::Sub<Output = i32>
    {
        a - b
    }
}

// OMG ! It works with structs too!
actor! {
    struct Counter {
        count: u32,
    }

    impl Counter {
        fn new() -> Self {
            Self {
                count: 0,
            }
        }

        // Functions routed to the actor are marked with the #[act] attribute
        // By default, the function name is used as the message name, but you can override it
        // with the msg() attribute
        #[act(msg(inc))]
        async fn increment(&mut self) -> u32 {
            self.count += 1;
            self.count
        }

        #[act(msg(dec))]
        async fn decrement(&mut self) -> u32 {
            self.count -= 1;
            self.count
        }
    }
}

#[tokio::main]
async fn main() {
    let add_actor = add();
    let sub_actor = sub::<i32>();
    let counter = Counter::new();
    let counter = absinthe::spawn(counter);

    // send! a message to the actor, and await the response
    // notify! the actor when you don't care about the response
    let res = absinthe::send!(add_actor, 1, 2).await;
    let res = absinthe::send!(sub_actor, res, 2).await;

    // Request and Response enums are generated for each actor, based on #[act] functions
    let res = absinthe::send!(counter, CounterReq::Inc).await;
    let res = absinthe::send!(counter, CounterReq::Dec).await;
}

路线图

  • Actor 函数
  • 泛型 Actor 函数
  • Lambda Actor 包装器
  • Actor 结构体
  • 泛型 Actor 结构体
  • Actor 复制品
  • Actor 监督
  • 跟踪
  • UDP 桥
  • UDP 桥加密隧道
  • UDP 桥节点健康检查
  • RabbitMQ 桥
  • Stomp 桥

依赖

~0.6–12MB
~129K SLoC