#[server_task_enum]Expand description
§#[server_task_enum]
The #[server_task_enum] macro streamlines the creation of server-side task enums.
When applied to an enum, it automatically implements necessary traits for processing RPC requests,
reducing boilerplate and improving code maintainability.
§Macro Arguments:
The server_task_enum can accept either or both of “req” and “resp” flags, with the following
rules:
- If
reqis specified, the enum is request task, the macro will impl ServerTaskDecode, ServerTaskAction. each variant must have an#[action(...)]attribute. - If “resp” is specified, the enum is response task. The macro will impl ServerTaskEncode, ServerTaskDone
- If both
reqandrespis specified, the response type forServerTaskDecode<R>andServerTaskDone<R>is implicitlySelf(the enum itself).resp_typecan be omitted. - If only “req” is specified (and “resp” is not), then
resp_typemust be provided. Thisresp_typespecifies the type<R>for parameters ofServerTaskDecode<R>andServerTaskDone<R>.
server_task_enum also requires error flag, specified the custom error type for RpcError<E: RpcErrCodec>
§Variant Attributes:
#[action(...)]: Associates one or more RPC action (which can be numeric, string, or enum value) with an enum variant. Multiple actions can be specified (e.g.,#[action(1, 2 )]). If there’s more than one actions. The subtype should store its action and implement ServerTaskAction.
§Example:
use razor_stream::server::task::ServerTaskVariant;
use razor_stream_macros::server_task_enum;
use serde_derive::{Deserialize, Serialize};
use nix::errno::Errno;
#[derive(PartialEq)]
#[repr(u8)]
enum FileAction {
Open=1,
Read=2,
Write=3,
Truncate=4,
}
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
struct FileOpenReq { pub path: String, }
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
struct FileIOReq { pub path: String, pub offset: u64, pub len: u64, }
#[server_task_enum(req, resp, error=Errno)]
#[derive(Debug)]
pub enum FileTask {
#[action(FileAction::Open)]
Open(ServerTaskVariant<FileTask, FileOpenReq, Errno>),
#[action(FileAction::Read, FileAction::Write, FileAction::Truncate)]
Read(ServerTaskVariant<FileTask, FileIOReq, Errno>),
}