Add SendMessage destination

This commit is contained in:
Mark Poliakov 2023-12-28 22:33:39 +02:00
parent 8134fc185e
commit f40fc2a5f3
3 changed files with 43 additions and 2 deletions

View File

@ -9,6 +9,7 @@ authors = ["Mark Poliakov <mark@alnyan.me>"]
[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 = []

View File

@ -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<u32> for GroupId {
Self(value)
}
}
impl From<MessageDestination> 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<u64> 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),
}
}
}

View File

@ -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")]