diff --git a/Cargo.toml b/Cargo.toml index 5f99f29e..1e95c0d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ authors = ["Mark Poliakov "] [dependencies] core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" } compiler_builtins = { version = "0.1", optional = true } +serde = { version = "1.0.193", features = ["derive"], optional = true } [features] default = [] diff --git a/src/io/mod.rs b/src/io/mod.rs index 1a61fae6..b3081e6f 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -81,6 +81,7 @@ pub enum DeviceRequest { // Missing docs: self-explanatory names #[allow(missing_docs)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[repr(C)] pub enum KeyboardKey { Char(u8), @@ -101,18 +102,35 @@ pub enum KeyboardKey { /// Alternative representation for [KeyboardKey] #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[repr(transparent)] pub struct KeyboardKeyCode(u16); /// Descibes a single event produced by a keyboard device // Missing docs: self-explanatory names #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, Debug)] pub enum KeyboardKeyEvent { Pressed(KeyboardKey), Released(KeyboardKey), } +/// Specifies where a message should be delivered on a channel +#[derive(Clone, Copy, Debug)] +pub enum MessageDestination { + /// Broadcast everywhere, including the sender. + /// + /// # Note + /// + /// May cause a feedback loop if the sender is also a subscriber of the channel. + All, + /// Broadcast everywhere, except the sender + AllExceptSelf, + /// Send to a specific subscriber ID + Specific(u32), +} + impl KeyboardKey { /// Converts [KeyboardKey] to its related [KeyboardKeyCode] pub const fn code(self) -> KeyboardKeyCode { @@ -261,3 +279,24 @@ impl From for GroupId { Self(value) } } + +impl From for u64 { + fn from(value: MessageDestination) -> Self { + match value { + MessageDestination::All => u64::MAX, + MessageDestination::AllExceptSelf => u64::MAX - 1, + MessageDestination::Specific(id) => id as u64, + } + } +} + +impl From for MessageDestination { + fn from(value: u64) -> Self { + match value { + 0..=const { u32::MAX as u64 } => Self::Specific(value as u32), + const { u64::MAX - 1 } => Self::AllExceptSelf, + u64::MAX => Self::All, + _ => panic!("Unexpected MessageDestination value: {:?}", value), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 075a31fd..ce350eeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,10 @@ #![allow( clippy::new_without_default, clippy::should_implement_trait, - clippy::module_inception + clippy::module_inception, + incomplete_features )] -#![feature(trace_macros, const_trait_impl)] +#![feature(trace_macros, const_trait_impl, inline_const_pat)] #![deny(missing_docs)] #[cfg(feature = "alloc")]