From 9e1fee89addc952b277ab00cd9252431baa107c2 Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Mon, 24 Jun 2019 21:04:52 -0400 Subject: [PATCH] Adding a macro to give structure to the linking of external libraries. --- src/lib.rs | 3 +++ src/macros.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/macros.rs diff --git a/src/lib.rs b/src/lib.rs index cd4d6df..5d30f58 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,9 @@ mod c_types; // Raw platform data retrieval. mod raw; +// Macros to help create bindings to FFI libraries. +mod macros; + pub use self::c_types::{CChar, CUChar}; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..b830171 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,31 @@ +#[macro_export] +macro_rules! external_library +{ + { + $name: ident : $link: ident + { + $(statics: + $($sname: ident: $stype: ty),+; + )|* + + $(functions: + $($fname: ident($($farg: ty),*) -> $fret:ty),+; + )|* + + $(varargs: + $($vname: ident($($vargs: ty),+) -> $vret: ty),+; + )|* + } + } => + { + #[link(name=$link)] + extern "C" + { + $($(pub static $sname: $stype;)+)* + + $($(pub fn $fname($(_: $farg),*) -> $fret;)+)* + + $($(pub fn $vname($(_: $vargs),+ , ...) -> $vret;)+)* + } + } +}