[−][src]Struct xactor::Supervisor
Actor supervisor
Supervisor gives the actor the ability to restart after failure. When the actor fails, recreate a new actor instance and replace it.
Methods
impl Supervisor
[src]
pub async fn start<A, F>(f: F) -> Addr<A> where
A: Actor,
F: Fn() -> A + Send + 'static,
[src]
A: Actor,
F: Fn() -> A + Send + 'static,
Start a supervisor
Examples
use xactor::*; use std::time::Duration; use async_std::task; #[message] struct Die; #[message] struct Add; #[message(result = "i32")] struct Get; struct MyActor(i32); impl Actor for MyActor {} #[async_trait::async_trait] impl Handler<Add> for MyActor { async fn handle(&mut self, ctx: &Context<Self>, _: Add) { self.0 += 1; } } #[async_trait::async_trait] impl Handler<Get> for MyActor { async fn handle(&mut self, ctx: &Context<Self>, _: Get) -> i32 { self.0 } } #[async_trait::async_trait] impl Handler<Die> for MyActor { async fn handle(&mut self, ctx: &Context<Self>, _: Die) { ctx.stop(None); } } #[async_std::main] async fn main() -> Result<()> { let mut addr = Supervisor::start(|| MyActor(0)).await; addr.send(Add)?; assert_eq!(addr.call(Get).await?, 1); addr.send(Add)?; assert_eq!(addr.call(Get).await?, 2); addr.send(Die)?; task::sleep(Duration::from_secs(1)).await; // Wait for actor restart assert_eq!(addr.call(Get).await?, 0); Ok(()) }
Auto Trait Implementations
impl RefUnwindSafe for Supervisor
impl Send for Supervisor
impl Sync for Supervisor
impl Unpin for Supervisor
impl UnwindSafe for Supervisor
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,