GenServer

Trait GenServer 

Source
pub trait GenServer: Send + Sized {
    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 start_on_thread(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<InitResult<Self>, Self::Error>> + Send { ... }
    fn main_loop(
        self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
    ) -> impl Future<Output = Self> + Send { ... }
    fn receive(
        &mut self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
    ) -> impl Future<Output = bool> + Send { ... }
    fn handle_call(
        &mut self,
        _message: Self::CallMsg,
        _handle: &GenServerHandle<Self>,
    ) -> impl Future<Output = CallResponse<Self>> + Send { ... }
    fn handle_cast(
        &mut self,
        _message: Self::CastMsg,
        _handle: &GenServerHandle<Self>,
    ) -> impl Future<Output = CastResponse> + 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 start_on_thread(self) -> GenServerHandle<Self>

For some “singleton” GenServers that run througout the whole execution of the program, it makes sense to run in their own dedicated thread to avoid interference with the rest of the tasks’ runtime. The use of tokio::task::spawm_blocking is not recommended for these scenarios as it is a limited thread pool better suited for blocking IO tasks that eventually end

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<InitResult<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 = Self> + Send

Source

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

Source

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

Source

fn handle_cast( &mut self, _message: Self::CastMsg, _handle: &GenServerHandle<Self>, ) -> impl Future<Output = CastResponse> + 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§