From a370d13f7af6b16743c487a8146e7e46fd091df1 Mon Sep 17 00:00:00 2001 From: Myrddin Dundragon Date: Tue, 29 Jul 2025 17:54:46 -0400 Subject: [PATCH] Adjusted project data. Properly pointed to the repository location. Changed the rust edition. --- .rustfmt.toml | 79 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 6 ++-- examples/enums.rs | 7 +--- examples/flags.rs | 6 +--- src/c_ptr.rs | 15 +++++--- src/c_types.rs | 87 +++-------------------------------------------- src/lib.rs | 23 ++++--------- src/macros.rs | 74 ---------------------------------------- src/raw.rs | 13 ++++--- 9 files changed, 114 insertions(+), 196 deletions(-) create mode 100644 .rustfmt.toml delete mode 100644 src/macros.rs diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..b85c2b1 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,79 @@ +max_width = 80 +hard_tabs = false +tab_spaces = 3 +newline_style = "Unix" +indent_style = "Visual" +use_small_heuristics = "Default" +fn_call_width = 60 +attr_fn_like_width = 70 +struct_lit_width = 18 +struct_variant_width = 35 +array_width = 60 +chain_width = 60 +single_line_if_else_max_width = 50 +single_line_let_else_max_width = 50 +wrap_comments = true +format_code_in_doc_comments = true +doc_comment_code_block_width = 80 +comment_width = 80 +normalize_comments = true +normalize_doc_attributes = true +format_strings = true +format_macro_matchers = true +format_macro_bodies = true +skip_macro_invocations = [] +hex_literal_case = "Preserve" +empty_item_single_line = true +struct_lit_single_line = true +fn_single_line = false +where_single_line = true +imports_indent = "Visual" +imports_layout = "Horizontal" +imports_granularity = "Module" +group_imports = "StdExternalCrate" +reorder_imports = true +reorder_modules = true +reorder_impl_items = true +type_punctuation_density = "Wide" +space_before_colon = false +space_after_colon = true +spaces_around_ranges = false +binop_separator = "Back" +remove_nested_parens = true +combine_control_expr = false +short_array_element_width_threshold = 10 +overflow_delimited_expr = false +struct_field_align_threshold = 0 +enum_discrim_align_threshold = 0 +match_arm_blocks = true +match_arm_leading_pipes = "Never" +force_multiline_blocks = true +fn_params_layout = "Compressed" +brace_style = "AlwaysNextLine" +control_brace_style = "AlwaysNextLine" +trailing_semicolon = true +trailing_comma = "Never" +match_block_trailing_comma = false +blank_lines_upper_bound = 3 +blank_lines_lower_bound = 0 +edition = "2021" +style_edition = "2021" +inline_attribute_width = 0 +format_generated_files = true +generated_marker_line_search_limit = 5 +merge_derives = true +use_try_shorthand = false +use_field_init_shorthand = false +force_explicit_abi = true +condense_wildcard_suffixes = false +color = "Always" +required_version = "1.8.0" +unstable_features = true +disable_all_formatting = false +skip_children = false +show_parse_errors = true +error_on_line_overflow = false +error_on_unformatted = false +ignore = [] +emit_mode = "Files" +make_backup = false diff --git a/Cargo.toml b/Cargo.toml index 70c324c..232dbbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "binding" -version = "0.1.0" +version = "0.2.0" description = "Defines macros and functions used when binding to C libraries." -repository = "https://workshop.cybermages.tech/CyberMages/binding.git" +repository = "https://workshop.cybermages.tech/CyberMages/binding" keywords = ["binding", "c types", "c enums", "c flags", "c strings"] -edition = "2021" +edition = "2024" documentation = "" authors = ["CyberMages LLC ", "Jason Travis Smith "] readme = "README.md" diff --git a/examples/enums.rs b/examples/enums.rs index 7e43b4f..56656d3 100644 --- a/examples/enums.rs +++ b/examples/enums.rs @@ -3,10 +3,7 @@ use binding::c_enum; - - -c_enum! -{ +c_enum! { /// Test enum is a simple enum testing the /// capabilities of creating a C enum. enum TestEnum: i32 @@ -22,8 +19,6 @@ c_enum! } } - - pub fn main() { let cenum: TestEnum; diff --git a/examples/flags.rs b/examples/flags.rs index e858e8c..847dbcf 100644 --- a/examples/flags.rs +++ b/examples/flags.rs @@ -3,10 +3,7 @@ use binding::c_flags; - - -c_flags! -{ +c_flags! { /// A sample of flags useful for the EvDev library. flags ReadFlags: u32 { @@ -25,7 +22,6 @@ c_flags! } } - pub fn main() { let flag: ReadFlags; diff --git a/src/c_ptr.rs b/src/c_ptr.rs index cc632d9..001cecf 100644 --- a/src/c_ptr.rs +++ b/src/c_ptr.rs @@ -3,9 +3,9 @@ /// 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. +/// Many C libraries return pointers to opaque or incomplete types. +/// This macro declares an equivalent Rust type that cannot be +/// instantiated directly but can safely be used as a pointer target. #[macro_export] macro_rules! c_ptr { @@ -13,6 +13,13 @@ macro_rules! c_ptr $name: ident } => { - pub enum $name {} + #[repr(C)] + pub struct $name { _private: __opaque::Private } }; } + +mod __opaque +{ + #[allow(dead_code)] + pub enum Private {} +} diff --git a/src/c_types.rs b/src/c_types.rs index f739d34..bb8a00f 100644 --- a/src/c_types.rs +++ b/src/c_types.rs @@ -3,54 +3,10 @@ //! This defines the primitive types that may be seen //! when creating 'C' bindings. -//! -//! There are several places where the target architecture and the OS -//! require special consideration. These are handled -//! with configuration statements. - -// Character types. -#[cfg(any(target_os="android", target_os="emscripten", - all(target_os="linux", - any(target_arch="aarch64", target_arch="arm", - target_arch="powerpc", target_arch="powerpc64"))))] -pub type CChar = u8; - -#[cfg(not(any(target_os="android", target_os="emscripten", - all(target_os="linux", - any(target_arch="aarch64", target_arch="arm", - target_arch="powerpc", target_arch="powerpc64")))))] -pub type CChar = i8; - - -pub type CUChar = u8; - - - -// Integer types. -#[cfg(any(target_pointer_width="32", target_os="windows"))] -pub type CLong = i32; - -#[cfg(any(target_pointer_width="32", target_os="windows"))] -pub type CULong = u32; - - -#[cfg(all(target_pointer_width="64", not(target_os="windows")))] -pub type CLong = i32; - -#[cfg(all(target_pointer_width="64", not(target_os="windows")))] -pub type CULong = u32; - - -pub type CShort = i16; -pub type CUShort = u16; -pub type CInt = i32; -pub type CUInt = u32; -pub type CLongLong = i64; -pub type CULongLong = u64; -pub type CFloat = f32; -pub type CDouble = f64; - - +/// CVoid acts as a 'C' void pointer to pass to functions. **DO NOT** +/// use this as a return type from a 'C' function. If a 'C' function +/// returns void, then use the CReturnVoid type for that. +pub use std::ffi::{c_char as CChar, c_double as CDouble, c_float as CFloat, c_int as CInt, c_long as CLong, c_longlong as CLongLong, c_short as CShort, c_uchar as CUChar, c_uint as CUInt, c_ulong as CULong, c_ulonglong as CULongLong, c_ushort as CUShort, c_void as CVoid, CStr}; // Void types. /// To return void from a 'C' function it should return an empty tuple (). @@ -58,38 +14,5 @@ pub type CDouble = f64; /// as an argument to a function. Use the CVoid type for that. pub type CReturnVoid = (); -/// This acts as a 'C' void pointer to pass to functions. **DO NOT** -/// use this as a return type from a 'C' function. If a 'C' function -/// returns void, then use the CReturnVoid type for that. -#[repr(u8)] -pub enum CVoid -{ - #[doc(hidden)] - _Variant, - - #[doc(hidden)] - _Variant2 -} - - - -// String types. -/* -#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)] -pub struct CString -{ - chars: Box<[u8]> -} - -#[derive(Hash)] -pub struct CStr -{ - inner: [CChar] -} -*/ -#[cfg(feature="use_std")] -pub type CStr = ::std::ffi::CStr; -#[cfg(feature="use_std")] +#[cfg(feature = "use_std")] pub type CString = ::std::ffi::CString; -#[cfg(feature="use_std")] -pub type NullByteError = ::std::ffi::NulError; diff --git a/src/lib.rs b/src/lib.rs index 7d62a11..e053e92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ //! for use amoungst all of CyberMages LLC's projects. // Handle using the core or the std of Rust depending on the chosen feature. -#![cfg_attr(not(feature="use_std"), no_std)] +#![cfg_attr(not(feature = "use_std"), no_std)] // This crate can only use parts of Rust that are in both the core // and the standard library. This way the logging system will work for @@ -14,14 +14,9 @@ // // This is handled by coding using the std library and referencing the core // library as the std library if the core library is desired. -#[cfg(not(feature="use_std"))] +#[cfg(not(feature = "use_std"))] extern crate core as std; - - -// Macros to help create bindings to FFI libraries. -mod macros; - // Basic C type macro modules. mod c_enum; mod c_flags; @@ -32,13 +27,7 @@ mod c_types; // Raw platform data retrieval. mod raw; - - -#[cfg(feature="use_std")] -pub use self::c_types::{CStr, CString, NullByteError}; - -pub use self::c_types::{CChar, CUChar}; -pub use self::c_types::{CShort, CUShort, CInt, CUInt, CLong, CULong}; -pub use self::c_types::{CLongLong, CULongLong, CFloat, CDouble}; -pub use self::c_types::{CVoid, CReturnVoid}; -pub use self::raw::{AsRaw, AsRawMut, AsRawPtr, AsRawMutPtr, FromRaw, IntoRaw}; +#[cfg(feature = "use_std")] +pub use self::c_types::{from_c_string, to_c_string, CString, NullByteError}; +pub use self::c_types::{CChar, CDouble, CFloat, CInt, CLong, CLongLong, CReturnVoid, CShort, CStr, CUChar, CUInt, CULong, CULongLong, CUShort, CVoid}; +pub use self::raw::{AsRaw, AsRawMut, AsRawMutPtr, AsRawPtr, FromRaw, IntoRaw}; diff --git a/src/macros.rs b/src/macros.rs deleted file mode 100644 index 0a46c01..0000000 --- a/src/macros.rs +++ /dev/null @@ -1,74 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Sealed with Magistamp. - -#[macro_export] -macro_rules! c_library -{ - { - $(statics: - { - $($(#[$satter: meta])* $sname: ident: $stype: ty);+; - } - )? - - $(functions: - { - $($(#[$fatter: meta])* $fname: ident($($farg: ident: $fargt: ty),*) -> $fret:ty);+; - } - )? - - $(varargs: - { - $($(#[$vatter: meta])* $vname: ident($($vargs: ident: $vargst: ty),+) -> $vret:ty);+; - } - )? - } => - { - extern "C" - { - $($($(#[$satter])* pub static $sname: $stype;)+)? - - $($($(#[$fatter])* pub fn $fname($($farg: $fargt),*) -> $fret;)+)? - - $($($(#[$vatter])* pub fn $vname($(vargs: $vargst),+ , ...) -> $vret;)+)? - } - }; -} - -#[macro_export] -macro_rules! external_library -{ - { - $name: ident: $link: expr => - { - $(statics: - { - $($(#[$satter: meta])* $sname: ident: $stype: ty);+; - } - )? - - $(functions: - { - $($(#[$fatter: meta])* $fname: ident($($farg: ident: $fargt: ty),*) -> $fret:ty);+; - } - )? - - $(varargs: - { - $($(#[$vatter: meta])* $vname: ident($($vargs: ident: $vargst: ty),+) -> $vret:ty);+; - } - )? - } - } => - { - #[link(name=$link)] - extern "C" - { - $($($(#[$satter])* pub static $sname: $stype;)+)? - - $($($(#[$fatter])* pub fn $fname($($farg: $fargt),*) -> $fret;)+)? - - $($($(#[$vatter])* pub fn $vname($(vargs: $vargst),+ , ...) -> $vret;)+)? - } - }; -} diff --git a/src/raw.rs b/src/raw.rs index da3aa41..af29da8 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -9,11 +9,11 @@ //! to help with understanding the code and to remind the programmer //! where values are coming from. - /// Some times there are types that need to return a platform specific /// data type that can be used by the platform specific functions. This /// type creates a path to easily get that data. -pub trait AsRaw where T: Sized +pub trait AsRaw + where T: Sized { fn as_raw(&self) -> &T; } @@ -21,7 +21,8 @@ pub trait AsRaw where T: Sized /// Some times there are types that need to return a platform specific /// data type that can be used by the platform specific functions. This /// type creates a path to easily get that data in a mutable manner. -pub trait AsRawMut where T: Sized +pub trait AsRawMut + where T: Sized { fn as_raw_mut(&mut self) -> &mut T; } @@ -29,7 +30,8 @@ pub trait AsRawMut where T: Sized /// Some times there are types that need to return a platform specific /// data type that can be used by the platform specific functions. This /// type creates a path to easily get that data as a raw const pointer. -pub trait AsRawPtr where T: Sized +pub trait AsRawPtr + where T: Sized { fn as_raw_ptr(&self) -> *const T; } @@ -37,7 +39,8 @@ pub trait AsRawPtr where T: Sized /// Some times there are types that need to return a platform specific /// data type that can be used by the platform specific functions. This /// type creates a path to easily get that data as a raw mutable pointer. -pub trait AsRawMutPtr where T: Sized +pub trait AsRawMutPtr + where T: Sized { fn as_raw_mut_ptr(&mut self) -> *mut T; }