Skip to main content

ChannelKind

Trait ChannelKind 

Source
pub trait ChannelKind:
    Debug
    + Clone
    + Copy
    + Hash
    + Eq
    + Send
    + Sync
    + Serialize
    + DeserializeOwned
    + 'static {
    // Required methods
    fn priority(&self) -> u8;
    fn wire_id(&self) -> u8;
    fn from_wire_id(id: u8) -> Option<Self>;
    fn from_name(s: &str) -> Option<Self>;
    fn name(&self) -> &'static str;
    fn is_system(&self) -> bool;
    fn all() -> &'static [Self];
}
Expand description

Trait for defining multiplexed channel types.

Consumers implement this trait to define their own channel taxonomy. Each channel has a priority, a wire ID for binary encoding, and string representations for JSON/SSE serialization.

§Example

use pushwire_core::ChannelKind;

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
enum MyChannel { Events, Control, System }

impl ChannelKind for MyChannel {
    fn priority(&self) -> u8 {
        match self {
            MyChannel::System | MyChannel::Events => 0,
            MyChannel::Control => 1,
        }
    }
    fn wire_id(&self) -> u8 {
        match self {
            MyChannel::Events => 0x01,
            MyChannel::Control => 0x02,
            MyChannel::System => 0x05,
        }
    }
    fn from_wire_id(id: u8) -> Option<Self> {
        match id {
            0x01 => Some(MyChannel::Events),
            0x02 => Some(MyChannel::Control),
            0x05 => Some(MyChannel::System),
            _ => None,
        }
    }
    fn from_name(s: &str) -> Option<Self> {
        match s {
            "events" => Some(MyChannel::Events),
            "control" => Some(MyChannel::Control),
            "system" => Some(MyChannel::System),
            _ => None,
        }
    }
    fn name(&self) -> &'static str {
        match self {
            MyChannel::Events => "events",
            MyChannel::Control => "control",
            MyChannel::System => "system",
        }
    }
    fn is_system(&self) -> bool { matches!(self, MyChannel::System) }
    fn all() -> &'static [Self] { &[Self::Events, Self::Control, Self::System] }
}

Required Methods§

Source

fn priority(&self) -> u8

Priority for outbound queue ordering. Lower values = higher priority. Typical mapping: 0 = high (system, critical UI), 1 = normal, 2 = low (telemetry).

Source

fn wire_id(&self) -> u8

Numeric ID for the binary wire format. Must be unique per channel.

Source

fn from_wire_id(id: u8) -> Option<Self>

Decode from binary wire ID. Returns None for unknown IDs.

Source

fn from_name(s: &str) -> Option<Self>

Parse from canonical string name (for SSE query params, JSON fields).

Source

fn name(&self) -> &'static str

Canonical string name for this channel.

Source

fn is_system(&self) -> bool

Whether this channel is the system/control channel. The system channel handles auth, ping/pong, ACK, and subscription management. Exactly one channel should return true.

Source

fn all() -> &'static [Self]

All valid channel variants. Used for cursor initialization and enumeration.

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§