mirror of
https://github.com/bspeice/aeron-rs
synced 2024-12-21 21:38:09 -05:00
Move the control protocol enum to main client lib
Side benefit: forcing documentation
This commit is contained in:
parent
78730221d1
commit
cef3a17d55
@ -4,46 +4,6 @@
|
||||
#![allow(clippy::all)]
|
||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||
|
||||
/// Construct a C-compatible enum out of a set of constants.
|
||||
/// Commonly used for types in Aeron that have fixed values via `#define`,
|
||||
/// but aren't actually enums (e.g. AERON_COMMAND_.*, AERON_ERROR_CODE_.*).
|
||||
/// Behavior is ultimately very similar to `num::FromPrimitive`.
|
||||
macro_rules! define_enum {
|
||||
($(#[$outer:meta])*, $name:ident, [$(($left:ident, $right:expr)),*]) => {
|
||||
#[repr(u32)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
$(#[$outer])*
|
||||
pub enum $name {
|
||||
$($left = $right),*
|
||||
}
|
||||
|
||||
impl ::std::convert::TryFrom<u32> for $name {
|
||||
type Error = ();
|
||||
fn try_from(val: u32) -> Result<$name, ()> {
|
||||
match val {
|
||||
$(v if v == $name::$left as u32 => Ok($name::$left)),*,
|
||||
_ => Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
define_enum!(
|
||||
#[doc = "Command codes used when interacting with the Media Driver"],
|
||||
AeronCommand, [
|
||||
(AddPublication, AERON_COMMAND_ADD_PUBLICATION),
|
||||
(RemovePublication, AERON_COMMAND_REMOVE_PUBLICATION)
|
||||
]
|
||||
);
|
||||
|
||||
define_enum!(
|
||||
#[doc = "Error codes used by the Media Driver control protocol"],
|
||||
AeronControlErrorCode, [
|
||||
(GenericError, AERON_ERROR_CODE_GENERIC_ERROR)
|
||||
]
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
@ -58,12 +18,4 @@ mod tests {
|
||||
assert_eq!(minor, 21);
|
||||
assert_eq!(patch, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn define_enum_try() {
|
||||
assert_eq!(
|
||||
Ok(AeronCommand::AddPublication),
|
||||
AERON_COMMAND_ADD_PUBLICATION.try_into()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
59
src/control_protocol.rs
Normal file
59
src/control_protocol.rs
Normal file
@ -0,0 +1,59 @@
|
||||
//! Utilities for interacting with the control protocol of the Media Driver
|
||||
use aeron_driver_sys::*;
|
||||
|
||||
/// Construct a C-compatible enum out of a set of constants.
|
||||
/// Commonly used for types in Aeron that have fixed values via `#define`,
|
||||
/// but aren't actually enums (e.g. AERON_COMMAND_.*, AERON_ERROR_CODE_.*).
|
||||
/// Behavior is ultimately very similar to `num::FromPrimitive`.
|
||||
macro_rules! define_enum {
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
$name:ident, [$(
|
||||
$(#[$inner:meta]),*
|
||||
$left:ident = $right:ident
|
||||
),+]
|
||||
) => {
|
||||
#[repr(u32)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
$(#[$outer])*
|
||||
pub enum $name {$(
|
||||
$(#[$inner])*
|
||||
$left = $right,
|
||||
)*}
|
||||
|
||||
impl ::std::convert::TryFrom<u32> for $name {
|
||||
type Error = ();
|
||||
fn try_from(val: u32) -> Result<$name, ()> {
|
||||
match val {
|
||||
$(v if v == $name::$left as u32 => Ok($name::$left)),*,
|
||||
_ => Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
define_enum!(
|
||||
#[doc = "Commands sent from clients to the Media Driver"]
|
||||
ClientCommand,
|
||||
[
|
||||
#[doc = "Client declaring a new publication"]
|
||||
AddPublication = AERON_COMMAND_ADD_PUBLICATION,
|
||||
#[doc = "Client removing a publication"]
|
||||
RemovePublication = AERON_COMMAND_REMOVE_PUBLICATION
|
||||
]
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::control_protocol::ClientCommand;
|
||||
use std::convert::TryInto;
|
||||
|
||||
#[test]
|
||||
fn client_command_convert() {
|
||||
assert_eq!(
|
||||
Ok(ClientCommand::AddPublication),
|
||||
::aeron_driver_sys::AERON_COMMAND_ADD_PUBLICATION.try_into()
|
||||
)
|
||||
}
|
||||
}
|
@ -2,4 +2,5 @@
|
||||
#![deny(missing_docs)]
|
||||
|
||||
pub mod client;
|
||||
pub mod control_protocol;
|
||||
pub mod driver;
|
||||
|
Loading…
Reference in New Issue
Block a user