1
0
mirror of https://github.com/bspeice/aeron-rs synced 2024-12-22 05:48:10 -05:00

Add in Rust-y bindings for some of the #define constants

This commit is contained in:
Bradlee Speice 2019-09-25 00:19:04 -04:00
parent 8371b25638
commit 3cd0aa4f55
3 changed files with 53 additions and 1 deletions

View File

@ -1,3 +1,4 @@
#include <stddef.h> #include <stddef.h>
#include <aeron_driver_context.h>
#include <aeronmd.h> #include <aeronmd.h>
#include <aeron_driver_context.h>
#include <command/aeron_control_protocol.h>

View File

@ -97,6 +97,7 @@ pub fn main() {
.header("bindings.h") .header("bindings.h")
.whitelist_function("aeron_.*") .whitelist_function("aeron_.*")
.whitelist_type("aeron_.*") .whitelist_type("aeron_.*")
.whitelist_var("AERON_.*")
.constified_enum_module("aeron_.*_enum") .constified_enum_module("aeron_.*_enum")
.generate() .generate()
.expect("Unable to generate aeron_driver bindings"); .expect("Unable to generate aeron_driver bindings");

View File

@ -4,8 +4,50 @@
#![allow(clippy::all)] #![allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 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 = "Command codes used when interacting with the Media Driver"],
AeronErrorCode, [
(GenericError, AERON_ERROR_CODE_GENERIC_ERROR)
]
);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::*;
use std::convert::TryInto;
#[test] #[test]
fn version_check() { fn version_check() {
@ -16,4 +58,12 @@ mod tests {
assert_eq!(minor, 21); assert_eq!(minor, 21);
assert_eq!(patch, 2); assert_eq!(patch, 2);
} }
#[test]
fn define_enum_try() {
assert_eq!(
Ok(AeronCommand::AddPublication),
AERON_COMMAND_ADD_PUBLICATION.try_into()
);
}
} }