Trait GenServer

Source
pub trait GenServer:
    Send
    + Sized
    + Clone {
    type CallMsg: Clone + Send + Sized + Sync;
    type CastMsg: Clone + Send + Sized + Sync;
    type OutMsg: Send + Sized;
    type Error: Debug + Send;

    // Provided methods
    fn start(self) -> GenServerHandle<Self> { ... }
    fn start_blocking(self) -> GenServerHandle<Self> { ... }
    fn run(
        self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
    ) -> impl Future<Output = Result<(), GenServerError>> + Send { ... }
    fn init(
        self,
        _handle: &GenServerHandle<Self>,
    ) -> impl Future<Output = Result<Self, Self::Error>> + Send { ... }
    fn main_loop(
        self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
    ) -> impl Future<Output = Result<Self, GenServerError>> + Send { ... }
    fn receive(
        self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
    ) -> impl Future<Output = Result<(Self, bool), GenServerError>> + Send { ... }
    fn handle_call(
        self,
        _message: Self::CallMsg,
        _handle: &GenServerHandle<Self>,
    ) -> impl Future<Output = CallResponse<Self>> + Send { ... }
    fn handle_cast(
        self,
        _message: Self::CastMsg,
        _handle: &GenServerHandle<Self>,
    ) -> impl Future<Output = CastResponse<Self>> + Send { ... }
    fn teardown(
        self,
        _handle: &GenServerHandle<Self>,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
}

Required Associated Types§

Provided Methods§

Source

fn start(self) -> GenServerHandle<Self>

Source

fn start_blocking(self) -> GenServerHandle<Self>

Tokio tasks depend on a coolaborative multitasking model. “work stealing” can’t happen if the task is blocking the thread. As such, for sync compute task or other blocking tasks need to be in their own separate thread, and the OS will manage them through hardware interrupts. Start blocking provides such thread.

Source

fn run( self, handle: &GenServerHandle<Self>, rx: &mut Receiver<GenServerInMsg<Self>>, ) -> impl Future<Output = Result<(), GenServerError>> + Send

Source

fn init( self, _handle: &GenServerHandle<Self>, ) -> impl Future<Output = Result<Self, Self::Error>> + Send

Initialization function. It’s called before main loop. It can be overrided on implementations in case initial steps are required.

Source

fn main_loop( self, handle: &GenServerHandle<Self>, rx: &mut Receiver<GenServerInMsg<Self>>, ) -> impl Future<Output = Result<Self, GenServerError>> + Send

Source

fn receive( self, handle: &GenServerHandle<Self>, rx: &mut Receiver<GenServerInMsg<Self>>, ) -> impl Future<Output = Result<(Self, bool), GenServerError>> + Send

Source

fn handle_call( self, _message: Self::CallMsg, _handle: &GenServerHandle<Self>, ) -> impl Future<Output = CallResponse<Self>> + Send

Source

fn handle_cast( self, _message: Self::CastMsg, _handle: &GenServerHandle<Self>, ) -> impl Future<Output = CastResponse<Self>> + Send

Source

fn teardown( self, _handle: &GenServerHandle<Self>, ) -> impl Future<Output = Result<(), Self::Error>> + Send

Teardown function. It’s called after the stop message is received. It can be overrided on implementations in case final steps are required, like closing streams, stopping timers, etc.

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§