serviceless 0.4.0

An simple actor model in rust, like actix
Documentation
use async_trait::async_trait;
use futures_core::Stream;
use futures_util::stream::Empty;
use std::future::Future;

use crate::{Context, Envelope, ServiceAddress};

/// [`Empty`] stream of [`Envelope`] for [`Context::new`] when there is no extra envelope source.
pub type EmptyStream<S> = Empty<Envelope<S>>;

/// A service is an running like thread
#[async_trait]
pub trait Service: Send + Sized + 'static {
    /// Extra envelope stream merged with the internal mailbox (see [`Context::with_stream`]).
    type Stream: Stream<Item = Envelope<Self>> + Unpin + Send;

    /// Start a service with the given context
    ///
    /// Returns the address and a future that should be spawned to run the service.
    /// The caller is responsible for spawning the returned future using their async runtime.
    fn start_by_context(
        self,
        ctx: Context<Self, Self::Stream>,
    ) -> (ServiceAddress<Self>, impl Future<Output = ()> + Send) {
        ctx.run(self)
    }

    /// Hook for service started
    async fn started(&mut self, _ctx: &mut Context<Self, Self::Stream>) {}

    /// Hook for service stopped
    async fn stopped(&mut self, _ctx: &mut Context<Self, Self::Stream>) {}
}