From bf4e35151497bb8550f15877c48c3c12136b7221 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 17:51:32 +0200 Subject: [PATCH] tweak dsl --- src/containers/bitmap.rs | 18 ++++++++++-------- src/containers/bitvec.rs | 23 +++++++++++++---------- src/containers/brightness_grid.rs | 23 ++++++++++++----------- src/containers/char_grid.rs | 25 ++++++++++++++----------- src/containers/cp437_grid.rs | 7 ++++--- src/macros.rs | 14 +++++++++----- 6 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 81af42d..ae0d2ae 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -85,7 +85,7 @@ wrap_free!(sp_bitmap::Bitmap); wrap_methods!( sp_bitmap::Bitmap; - + /// Gets the current value at the specified position. /// /// # Arguments @@ -96,7 +96,7 @@ wrap_methods!( /// /// - when accessing `x` or `y` out of bounds ref fn get(x: usize, y: usize) -> bool; - + /// Sets the value of the specified position. /// /// # Arguments @@ -108,25 +108,27 @@ wrap_methods!( /// /// - when accessing `x` or `y` out of bounds mut fn set(x: usize, y: usize, value: bool); - + /// Sets the state of all pixels in the [Bitmap]. /// /// # Arguments /// /// - `value`: the value to set all pixels to mut fn fill(value: bool); - + /// Gets the width in pixels. ref fn width() -> usize; - + /// Gets the height in pixels. ref fn height() -> usize; - + /// 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; - |slice| unsafe { ByteSlice::from_slice(slice) }; + mut fn data_ref_mut() -> ByteSlice { + + return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + }; ); /// Consumes the Bitmap and returns the contained BitVec. diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index bceae0b..76d716a 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -40,7 +40,7 @@ wrap_free!(sp_bitvec::DisplayBitVec); wrap_methods!( sp_bitvec::DisplayBitVec; - + /// Gets the value of a bit. /// /// # Arguments @@ -53,9 +53,11 @@ wrap_methods!( /// # Panics /// /// - when accessing `index` out of bounds - ref fn get(index: usize) -> bool; - |result| result.map(|x| *x).unwrap_or(false); - + ref fn get(index: usize) -> bool { + return(result) { result.map(|x| *x).unwrap_or(false) }; + }; + + /// Sets the value of a bit. /// /// # Arguments @@ -67,25 +69,26 @@ wrap_methods!( /// /// - when accessing `index` out of bounds mut fn set(index: usize, value: bool); - + /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to mut fn fill(value: bool); - + /// Gets the length in bits. ref fn len() -> usize; - + /// Returns true if length is 0. ref fn is_empty() -> 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; - |slice| unsafe { ByteSlice::from_slice(slice) }; + mut fn as_raw_mut_slice() -> ByteSlice { + return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + }; ); /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 4e15599..a3590b8 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -57,7 +57,7 @@ wrap_free!(sp_brightness_grid::BrightnessGrid); wrap_methods!( sp_brightness_grid::BrightnessGrid; - + /// Gets the current value at the specified position. /// /// # Arguments @@ -69,7 +69,7 @@ wrap_methods!( /// # Panics /// - When accessing `x` or `y` out of bounds. ref fn get(x: usize, y: usize) -> Brightness; - + /// Sets the value of the specified position. /// /// # Arguments @@ -83,29 +83,30 @@ wrap_methods!( /// /// - When accessing `x` or `y` out of bounds. mut fn set(x: usize, y: usize, value: Brightness); - + /// Sets the value of all cells. /// /// # Arguments /// /// - `value`: the value to set all cells to mut fn fill(value: Brightness); - + /// Gets the width of the grid. ref fn width() -> usize; - + /// Gets the height of the grid. ref fn height() -> usize; - + /// 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; - |br_slice| unsafe { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); + mut fn data_ref_mut() -> ByteSlice { + return(br_slice) { unsafe { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + 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 dddae0b..1b70202 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -44,7 +44,7 @@ wrap_free!(sp_char_grid::CharGrid); wrap_methods!( sp_char_grid::CharGrid; - + /// Returns the current value at the specified position. /// /// # Arguments @@ -54,9 +54,10 @@ wrap_methods!( /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> u32; - | char | char as u32; - + ref fn get(x: usize, y: usize) -> u32 { + return(char) { char as u32 }; + }; + /// Sets the value of the specified position in the grid. /// /// # Arguments @@ -70,21 +71,23 @@ wrap_methods!( /// /// - 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); - let value = char::from_u32(value).unwrap(); - + mut fn set(x: usize, y: usize, value: u32) { + prepare(value) { char::from_u32(value).unwrap() }; + }; + /// Sets the value of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - mut fn fill(value: u32); - let value = char::from_u32(value).unwrap(); - + mut fn fill(value: u32) { + prepare(value) { char::from_u32(value).unwrap() }; + }; + /// Gets the width of the grid. ref fn width() -> usize; - + /// Gets the height of the grid. ref fn height() -> usize; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 166083f..d6ae17f 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -73,12 +73,13 @@ wrap_methods!( /// Gets the height of the grid. ref fn height() -> usize; - + /// 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; - | slice | unsafe { ByteSlice::from_slice(slice) }; + mut fn data_ref_mut() -> ByteSlice { + return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + }; ); /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. diff --git a/src/macros.rs b/src/macros.rs index fc6e524..cbadc14 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -41,8 +41,12 @@ macro_rules! wrap_methods { $( $(#[$meta:meta])+ $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) - $(-> $return_type:ty$(; |$it:ident | $return_expr:expr)?)?; - $($(let $param_let_name:ident = $param_let_expr:expr);+;)? + $(-> $return_type:ty)? + $({ + $($(prepare($param_let_name:ident) $param_let_expr:block);+;)? + $(return($it:ident) $return_expr:block;)? + })? + ; )* ) => { paste::paste! { @@ -57,9 +61,9 @@ macro_rules! wrap_methods { $($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" )] @@ -69,8 +73,8 @@ macro_rules! wrap_methods { let $it = result; let result = $return_expr; )? - return result; )? + return result; } )* }