pub trait ApiModule<S: AppState>: Send + 'static {
type Command: Debug + Send;
type CommandResponse: Debug + Send;
type Handle: Clone + Send + Sync + 'static;
// Required methods
fn new(
shared_state: Arc<S>,
command_receiver: AsyncReceiver<Self::Command>,
command_responder: AsyncSender<Self::CommandResponse>,
message_receiver: AsyncReceiver<Arc<Message>>,
to_ws_sender: AsyncSender<Message>,
runner_command_tx: AsyncSender<RunnerCommand>,
) -> Self
where Self: Sized;
fn create_handle(
sender: AsyncSender<Self::Command>,
receiver: AsyncReceiver<Self::CommandResponse>,
) -> Self::Handle;
fn run<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn rule(state: Arc<S>) -> Box<dyn Rule + Send + Sync>;
// Provided methods
fn new_combined(
shared_state: Arc<S>,
command_receiver: AsyncReceiver<Self::Command>,
command_responder: AsyncSender<Self::Command>,
command_response_receiver: AsyncReceiver<Self::CommandResponse>,
command_response_responder: AsyncSender<Self::CommandResponse>,
message_receiver: AsyncReceiver<Arc<Message>>,
to_ws_sender: AsyncSender<Message>,
runner_command_tx: AsyncSender<RunnerCommand>,
) -> (Self, Self::Handle)
where Self: Sized { ... }
fn callback(
_shared_state: Arc<S>,
_command_receiver: AsyncReceiver<Self::Command>,
_command_responder: AsyncSender<Self::CommandResponse>,
_message_receiver: AsyncReceiver<Arc<Message>>,
_to_ws_sender: AsyncSender<Message>,
) -> CoreResult<Option<Box<dyn ReconnectCallback<S>>>> { ... }
}Expand description
The contract for a self-contained, concurrent API module.
Generic over the AppState for type-safe access to shared data.
Required Associated Types§
Sourcetype CommandResponse: Debug + Send
type CommandResponse: Debug + Send
This specific CommandResponse type this module produces.
Required Methods§
Sourcefn new(
shared_state: Arc<S>,
command_receiver: AsyncReceiver<Self::Command>,
command_responder: AsyncSender<Self::CommandResponse>,
message_receiver: AsyncReceiver<Arc<Message>>,
to_ws_sender: AsyncSender<Message>,
runner_command_tx: AsyncSender<RunnerCommand>,
) -> Selfwhere
Self: Sized,
fn new(
shared_state: Arc<S>,
command_receiver: AsyncReceiver<Self::Command>,
command_responder: AsyncSender<Self::CommandResponse>,
message_receiver: AsyncReceiver<Arc<Message>>,
to_ws_sender: AsyncSender<Message>,
runner_command_tx: AsyncSender<RunnerCommand>,
) -> Selfwhere
Self: Sized,
Creates a new instance of the module.
Sourcefn create_handle(
sender: AsyncSender<Self::Command>,
receiver: AsyncReceiver<Self::CommandResponse>,
) -> Self::Handle
fn create_handle( sender: AsyncSender<Self::Command>, receiver: AsyncReceiver<Self::CommandResponse>, ) -> Self::Handle
Creates a new handle for this module. This is used to send commands to the module.
§Arguments
sender: The sender channel for commands.receiver: The receiver channel for command responses.
Sourcefn run<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn run<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
The main run loop for the module’s background task.
Sourcefn rule(state: Arc<S>) -> Box<dyn Rule + Send + Sync>
fn rule(state: Arc<S>) -> Box<dyn Rule + Send + Sync>
Route only messages for which this returns true.
This function is used to determine whether a message should be processed by this module.
It allows for flexible and reusable rules that can be applied to different modules.
The main difference between this and the LightweightModule rule is that
this rule also takes the shared state as an argument, allowing for more complex
routing logic that can depend on the current state of the application.
Provided Methods§
fn new_combined(
shared_state: Arc<S>,
command_receiver: AsyncReceiver<Self::Command>,
command_responder: AsyncSender<Self::Command>,
command_response_receiver: AsyncReceiver<Self::CommandResponse>,
command_response_responder: AsyncSender<Self::CommandResponse>,
message_receiver: AsyncReceiver<Arc<Message>>,
to_ws_sender: AsyncSender<Message>,
runner_command_tx: AsyncSender<RunnerCommand>,
) -> (Self, Self::Handle)where
Self: Sized,
Sourcefn callback(
_shared_state: Arc<S>,
_command_receiver: AsyncReceiver<Self::Command>,
_command_responder: AsyncSender<Self::CommandResponse>,
_message_receiver: AsyncReceiver<Arc<Message>>,
_to_ws_sender: AsyncSender<Message>,
) -> CoreResult<Option<Box<dyn ReconnectCallback<S>>>>
fn callback( _shared_state: Arc<S>, _command_receiver: AsyncReceiver<Self::Command>, _command_responder: AsyncSender<Self::CommandResponse>, _message_receiver: AsyncReceiver<Arc<Message>>, _to_ws_sender: AsyncSender<Message>, ) -> CoreResult<Option<Box<dyn ReconnectCallback<S>>>>
An optional callback that can be executed when a reconnection event occurs. This function is useful for modules that need to perform specific actions when a reconnection happens, such as reinitializing state or resending messages. It allows for custom behavior to be defined that can be executed in the context of the module, providing flexibility and extensibility to the module’s functionality.
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.