From e4341307844754658d658a4055b6b42853d4be25 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 19:43:52 +0200 Subject: [PATCH] unifiy special handling of params and return --- src/commands/mod.rs | 4 +- src/containers/bitmap.rs | 4 +- src/containers/bitvec.rs | 16 +++--- src/containers/brightness_grid.rs | 15 +++--- src/containers/char_grid.rs | 12 ++--- src/containers/cp437_grid.rs | 4 +- src/containers/mod.rs | 10 ++-- src/macros.rs | 83 +++++++++++++++++++------------ 8 files changed, 82 insertions(+), 66 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9da0a22..58e5957 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -67,7 +67,7 @@ macro_rules! derive_command_from { macro_rules! derive_command_into_packet { ($command_type:ident) => { ::paste::paste! { - wrap_functions!(associate $command_type; + $crate::macros::wrap_functions!(associate $command_type; #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. @@ -90,7 +90,7 @@ macro_rules! wrap_command { }; ($command:ident) => { ::paste::paste! { - wrap_command!($command, [< $command Command >]); + $crate::commands::wrap_command!($command, [< $command Command >]); } }; } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index ffda27e..9bae852 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -109,7 +109,7 @@ wrap_methods!(Bitmap; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - mut fn data_ref_mut() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn data_ref_mut(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 1549d60..2a76cf7 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -71,8 +71,8 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - ref fn get(index: usize) -> bool { - return(result) { result.map(|x| *x).unwrap_or(false) }; + ref fn get(instance, index: usize) -> bool { + instance.get(index).map(|x| *x).unwrap_or(false) }; /// Sets the value of a bit. @@ -85,25 +85,25 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - mut fn set(index: usize, value: bool); + mut fn set(instance, index: usize, value: bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - mut fn fill(value: bool); + mut fn fill(instance, value: bool); /// Gets the length in bits. - ref fn len() -> usize; + ref fn len(instance) -> usize; /// Returns true if length is 0. - ref fn is_empty() -> bool; + ref fn is_empty(instance) -> bool; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - mut fn as_raw_mut_slice() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn as_raw_mut_slice(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } }; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 033bbdd..a5d01f2 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -77,14 +77,13 @@ wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - mut fn data_ref_mut() -> ByteSlice { - return(br_slice) { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); + mut fn data_ref_mut(instance) -> ByteSlice { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - unsafe { - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) - } - }; + let br_slice = instance.data_ref_mut(); + unsafe { + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + } }; ); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 365f1ad..1a59fdd 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -72,8 +72,8 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> u32 { - return(char) { char as u32 }; + ref fn get(instance, x: usize, y: usize) -> u32 { + instance.get(x, y) as u32 }; /// Sets the value of the specified position in the grid. @@ -89,8 +89,8 @@ wrap_methods!(CharGrid; /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - mut fn set(x: usize, y: usize, value: u32) { - prepare(value) { char::from_u32(value).unwrap() }; + mut fn set(instance, x: usize, y: usize, value: u32) { + instance.set(x, y, char::from_u32(value).unwrap()) }; /// Sets the value of all cells in the grid. @@ -99,7 +99,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - mut fn fill(value: u32) { - prepare(value) { char::from_u32(value).unwrap() }; + mut fn fill(instance, value: u32) { + instance.fill(char::from_u32(value).unwrap()) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 001ebb8..fc31d13 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -55,7 +55,7 @@ wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - mut fn data_ref_mut() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn data_ref_mut(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 8726038..fcd76a1 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -23,10 +23,10 @@ macro_rules! derive_get_width_height { ($object_type:ident) => { $crate::macros::wrap_methods! {$object_type; /// Gets the width. - ref fn width() -> usize; + ref fn width(instance) -> usize; /// Gets the height. - ref fn height() -> usize; + ref fn height(instance) -> usize; } }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> $value_type; + ref fn get(instance, x: usize, y: usize) -> $value_type; /// Sets the value of the specified position. /// @@ -57,14 +57,14 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - mut fn set(x: usize, y: usize, value: $value_type); + mut fn set(instance, x: usize, y: usize, value: $value_type); /// Sets the state of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to - mut fn fill(value: $value_type); + mut fn fill(instance, value: $value_type); } }; } diff --git a/src/macros.rs b/src/macros.rs index 8dcaf55..e0560f5 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -36,49 +36,66 @@ macro_rules! nonnull_as_mut { }; } -// meta required on purpose, because otherwise the added documentation would suppress warnings +macro_rules! wrap_method { + ( + $object_type:ident; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + $(-> $return_type:ty)? + ) => { + ::paste::paste!{ + $crate::macros::wrap_method!( + $object_type; + $(#[$meta])+ + $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + $(-> $return_type)? { + $instance.$function($($($param_name),*)?) + } + ); + } + }; + ($object_type:ident; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + $(-> $return_type:ty)? + $impl:block + ) => { + paste::paste! { + $crate::macros::wrap_functions!([< $object_type:lower >]; + #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] + /// + $(#[$meta])* + fn $function( + $instance: ::core::ptr::NonNull<$object_type> + $(,$($param_name: $param_type),*)? + ) $(-> $return_type)? { + let $instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !($instance) }; + $impl + } + ); + } + }; +} + macro_rules! wrap_methods { ( $object_type:ident; $( $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? - $({ - $($(prepare($param_let_name:ident) $param_let_expr:block);+;)? - $(return($it:ident) $return_expr:block;)? - })? - ; + $($impl:block)?; )+ ) => { paste::paste! { - $crate::macros::wrap_functions!([< $object_type:lower >]; - $( - #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] - /// + $( + $crate::macros::wrap_method!($object_type; $(#[$meta])* - fn $function( - instance: ::core::ptr::NonNull<$object_type>, - $($param_name: $param_type),* - ) $(-> $return_type)? { - let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; - $($( - $(let $param_let_name = $param_let_expr;)* - )?)? - #[allow( - unused_variables, - reason = "This variable may not be used depending on macro variables" )] - let result = instance.$function($($param_name),*); - $( - $( - let $it = result; - let result = $return_expr; - )? - )? - return result; - } - )+ + $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + $(-> $return_type)? + $($impl)? ); + )+ } }; } @@ -213,5 +230,5 @@ macro_rules! wrap_functions { pub(crate) use { derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, - wrap_functions, wrap_methods, wrap_fields_accessor + wrap_functions, wrap_methods, wrap_fields_accessor, wrap_method };