pub trait GenServer{
    type CallMsg: Clone + Send + Sized + Sync;
    type CastMsg: Clone + Send + Sized + Sync;
    type OutMsg: Send + Sized;
    type State: Clone + Send;
    type Error: Debug + Send;
    // Provided methods
    fn new() -> Self { ... }
    fn start(initial_state: Self::State) -> GenServerHandle<Self> { ... }
    fn start_blocking(initial_state: Self::State) -> GenServerHandle<Self> { ... }
    fn run(
        &mut self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
        state: Self::State,
    ) -> impl Future<Output = Result<(), GenServerError>> + Send { ... }
    fn init(
        &mut self,
        _handle: &GenServerHandle<Self>,
        state: Self::State,
    ) -> impl Future<Output = Result<Self::State, Self::Error>> + Send { ... }
    fn main_loop(
        &mut self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
        state: Self::State,
    ) -> impl Future<Output = Result<(), GenServerError>> + Send { ... }
    fn receive(
        &mut self,
        handle: &GenServerHandle<Self>,
        rx: &mut Receiver<GenServerInMsg<Self>>,
        state: Self::State,
    ) -> impl Future<Output = Result<(Self::State, bool), GenServerError>> + Send { ... }
    fn handle_call(
        &mut self,
        _message: Self::CallMsg,
        _handle: &GenServerHandle<Self>,
        _state: Self::State,
    ) -> impl Future<Output = CallResponse<Self>> + Send { ... }
    fn handle_cast(
        &mut self,
        _message: Self::CastMsg,
        _handle: &GenServerHandle<Self>,
        _state: Self::State,
    ) -> impl Future<Output = CastResponse<Self>> + Send { ... }
    fn teardown(
        &mut self,
        _handle: &GenServerHandle<Self>,
        _state: Self::State,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
}Required Associated Types§
type CallMsg: Clone + Send + Sized + Sync
type CastMsg: Clone + Send + Sized + Sync
type OutMsg: Send + Sized
type State: Clone + Send
type Error: Debug + Send
Provided Methods§
fn new() -> Self
fn start(initial_state: Self::State) -> GenServerHandle<Self>
Sourcefn start_blocking(initial_state: Self::State) -> GenServerHandle<Self>
 
fn start_blocking(initial_state: Self::State) -> 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.
fn run( &mut self, handle: &GenServerHandle<Self>, rx: &mut Receiver<GenServerInMsg<Self>>, state: Self::State, ) -> impl Future<Output = Result<(), GenServerError>> + Send
Sourcefn init(
    &mut self,
    _handle: &GenServerHandle<Self>,
    state: Self::State,
) -> impl Future<Output = Result<Self::State, Self::Error>> + Send
 
fn init( &mut self, _handle: &GenServerHandle<Self>, state: Self::State, ) -> impl Future<Output = Result<Self::State, Self::Error>> + Send
Initialization function. It’s called before main loop. It can be overrided on implementations in case initial steps are required.
fn main_loop( &mut self, handle: &GenServerHandle<Self>, rx: &mut Receiver<GenServerInMsg<Self>>, state: Self::State, ) -> impl Future<Output = Result<(), GenServerError>> + Send
fn receive( &mut self, handle: &GenServerHandle<Self>, rx: &mut Receiver<GenServerInMsg<Self>>, state: Self::State, ) -> impl Future<Output = Result<(Self::State, bool), GenServerError>> + Send
fn handle_call( &mut self, _message: Self::CallMsg, _handle: &GenServerHandle<Self>, _state: Self::State, ) -> impl Future<Output = CallResponse<Self>> + Send
fn handle_cast( &mut self, _message: Self::CastMsg, _handle: &GenServerHandle<Self>, _state: Self::State, ) -> impl Future<Output = CastResponse<Self>> + Send
Sourcefn teardown(
    &mut self,
    _handle: &GenServerHandle<Self>,
    _state: Self::State,
) -> impl Future<Output = Result<(), Self::Error>> + Send
 
fn teardown( &mut self, _handle: &GenServerHandle<Self>, _state: Self::State, ) -> 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.