1
0
mirror of https://github.com/bspeice/aeron-rs synced 2025-07-01 13:56:07 -04:00

Move the control protocol enum to main client lib

Side benefit: forcing documentation
This commit is contained in:
2019-09-25 22:18:42 -04:00
parent 78730221d1
commit cef3a17d55
3 changed files with 60 additions and 48 deletions

View File

@ -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()
);
}
}