Expand description
Derive macros for Sioc: generate typed Socket.IO event and acknowledgement types.
The four core traits are intentionally separate so you only derive what each use-site needs:
| Trait | Derive | Needed for |
|---|---|---|
EventType | #[derive(EventType)] | event name constant + ack/binary policy |
AckType | #[derive(AckType)] | ack binary policy |
SerializePayload | #[derive(SerializePayload)] | emitting (outbound) |
DeserializePayload | #[derive(DeserializePayload)] | receiving (inbound) |
#[derive(EventRouter)] is a convenience that generates TryFrom<DynEvent> for
a dispatcher enum whose variants each wrap an Event<E>.
§Struct-level attributes (#[sioc(...)])
| Attribute | Applies to | Effect |
|---|---|---|
event(name = "str") | EventType | Override the event name (default: snake_case of struct name). |
event(ack = Type) | EventType | Set ack policy to HasAck<Type>; default is NoAck. |
event(binary) | EventType | Set binary policy to HasBinary; default is NoBinary. |
ack(binary) | AckType | Set binary policy to HasBinary; default is NoBinary. |
strict | DeserializePayload | Reject payloads with more elements than fields (error on trailing data). |
§Field-level attributes (#[sioc(...)])
| Attribute | Applies to | Effect |
|---|---|---|
flatten | SerializePayload, DeserializePayload | Serialize each element of this collection field as a separate array element; deserialize all remaining elements into it. Place last. |
§Example
ⓘ
// ignore: sioc-macros cannot dev-depend on sioc (circular); see sioc crate for runnable examples.
use sioc::prelude::*;
// Emit-only: only SerializePayload is required.
#[derive(EventType, SerializePayload)]
struct Ping;
// Recv-only: only DeserializePayload is required.
#[derive(EventType, DeserializePayload)]
struct Pong;
// Bidirectional with an explicit name.
#[derive(EventType, SerializePayload, DeserializePayload)]
#[sioc(event(name = "chat_message"))]
struct ChatMessage {
text: String,
}
// Ack receive-only.
#[derive(AckType, DeserializePayload)]
struct ChatAck {
ok: bool,
}Derive Macros§
- AckType
- Derives the
AckTypetrait, providing the binary policy associated type. - Deserialize
Payload - Derives the
DeserializePayloadtrait. - Event
Router - Derives
TryFrom<DynEvent>for a dispatcher enum. - Event
Type - Derives the
EventTypetrait, providing the event name constant and ack/binary policy associated types. - Serialize
Payload - Derives the
SerializePayloadtrait.