Skip to main content

Service

Trait Service 

Source
pub trait Service: Actor + Default {
    // Provided method
    fn from_registry<'async_trait>(    ) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>
       where Self: Send + 'async_trait { ... }
}
Expand description

Trait define a global service.

The service is a global actor. You can use Actor::from_registry to get the address Addr<A> of the service.

§Examples

use xactor::*;

#[message(result = "i32")]
struct AddMsg(i32);

#[derive(Default)]
struct MyService(i32);

impl Actor for MyService {}

impl Service for MyService {}

#[async_trait::async_trait]
impl Handler<AddMsg> for MyService {
    async fn handle(&mut self, ctx: &mut Context<Self>, msg: AddMsg) -> i32 {
        self.0 += msg.0;
        self.0
    }
}

#[xactor::main]
async fn main() -> Result<()> {
    let mut addr = MyService::from_registry().await?;
    assert_eq!(addr.call(AddMsg(1)).await?, 1);
    assert_eq!(addr.call(AddMsg(5)).await?, 6);
    Ok(())
}

Provided Methods§

Source

fn from_registry<'async_trait>() -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>
where Self: Send + 'async_trait,

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Message<Result = ()>> Service for Broker<T>