Crates.io | may_actor |
lib.rs | may_actor |
version | 0.2.3 |
source | src |
created_at | 2017-12-29 08:50:54.223787 |
updated_at | 2024-06-19 14:16:46.328817 |
description | Simple Actor library based on MAY |
homepage | https://github.com/Xudong-Huang/may_actor.git |
repository | https://github.com/Xudong-Huang/may_actor.git |
max_upload_size | |
id | 44797 |
size | 43,698 |
rust native actor library based on may
with this library
extern crate may_actor;
use may_actor::Actor;
fn main() {
struct HelloActor(u32);
let a = Actor::new(HelloActor(0));
a.call(|me| {
me.0 = 10;
println!("hello world");
});
// the with would wait previous messages process done
a.with(|me| println!("actor value is {}", me.0));
}
for a detailed example, please see pi.rs
Actor.call
)You can send messages to the actor with the call
API. It accepts a closure that has the &mut T
as parameter, so that you can change it’s internal state. The closure would be send to a queue inside the actor, and the actor would execute the closure asynchronously by a coroutine that associate with it . This API would not block user’s execution and would return immediately. if panic happens in the closure, it will be caught and ignored in actor's coroutine context.
Actor.with
)You can also synchronously manipulate the actor's internal state by the with
API. It accepts a closure that has the &mut T
as parameter, so that you can view or modify actor's internal state. The closure would be executed by the associated coroutine if there are no other pending messages. And it will block until the closure returns the result to caller. If any panic happens in the closure, it will propagate to the caller's context
Actor.from
)You can transmute a &self type unsafely to it's handle type Actor<T>
. This is convenient when need to get an actor handle in the implementation that need to pass as a function parameter.
However transmute from non actor context would trigger undefined behavior.
Allow panic inside a closure message, and this would not kill the actor, the actor will continue to process successive messages.
The actor can be cloned to get a new handle, this is just like how Arc<T>
works, if all the actor handle got dropped, the associated coroutine will automatically exit.
may_actor
is licensed under either of the following, at your option: