From 3bc41bae4facd5e2ca6f6fbef68767dd61316c38 Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Tue, 25 Jun 2019 02:31:18 -0400 Subject: [PATCH] Adding macro definitions for declaring C structures and pointers. --- src/c_ptr.rs | 15 +++++++++++++++ src/c_struct.rs | 18 ++++++++++++++++++ src/lib.rs | 2 ++ 3 files changed, 35 insertions(+) create mode 100644 src/c_ptr.rs create mode 100644 src/c_struct.rs diff --git a/src/c_ptr.rs b/src/c_ptr.rs new file mode 100644 index 0000000..50e1c5b --- /dev/null +++ b/src/c_ptr.rs @@ -0,0 +1,15 @@ +/// A C Pointer to an undefined structure. +/// +/// A lot of C libraries return a pointer to an undefined structure. +/// This is easily wrapped as an empty enum. This keeps users from being +/// able to create an instance of the value. +#[macro_export] +macro_rules! c_ptr +{ + { + $name: ident + } => + { + pub enum $name {} + }; +} diff --git a/src/c_struct.rs b/src/c_struct.rs new file mode 100644 index 0000000..c4496b6 --- /dev/null +++ b/src/c_struct.rs @@ -0,0 +1,18 @@ +/// Defines a C style structure. +#[macro_export] +macro_rules! c_struct +{ + { + $cname: ident + { + $($name: ident: $type: ty);*; + } + } => + { + #[repr(C)] + pub struct $cname + { + $(pub $name: $type),+, + } + }; +} diff --git a/src/lib.rs b/src/lib.rs index 098e477..8f19083 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,8 @@ mod macros; // Basic C type macro modules. mod c_enum; mod c_flags; +mod c_ptr; +mod c_struct; mod c_types; // Raw platform data retrieval.