tweak dsl

This commit is contained in:
Vinzenz Schroeter 2025-06-18 17:51:32 +02:00
parent 35e9c36ccd
commit bf4e351514
6 changed files with 62 additions and 48 deletions

View file

@ -85,7 +85,7 @@ wrap_free!(sp_bitmap::Bitmap);
wrap_methods!( wrap_methods!(
sp_bitmap::Bitmap; sp_bitmap::Bitmap;
/// Gets the current value at the specified position. /// Gets the current value at the specified position.
/// ///
/// # Arguments /// # Arguments
@ -96,7 +96,7 @@ wrap_methods!(
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> bool; ref fn get(x: usize, y: usize) -> bool;
/// Sets the value of the specified position. /// Sets the value of the specified position.
/// ///
/// # Arguments /// # Arguments
@ -108,25 +108,27 @@ wrap_methods!(
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
mut fn set(x: usize, y: usize, value: bool); mut fn set(x: usize, y: usize, value: bool);
/// Sets the state of all pixels in the [Bitmap]. /// Sets the state of all pixels in the [Bitmap].
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `value`: the value to set all pixels to /// - `value`: the value to set all pixels to
mut fn fill(value: bool); mut fn fill(value: bool);
/// Gets the width in pixels. /// Gets the width in pixels.
ref fn width() -> usize; ref fn width() -> usize;
/// Gets the height in pixels. /// Gets the height in pixels.
ref fn height() -> usize; ref fn height() -> usize;
/// Gets an unsafe reference to the data of the [Bitmap] instance. /// Gets an unsafe reference to the data of the [Bitmap] instance.
/// ///
/// The returned memory is valid for the lifetime of the bitmap. /// The returned memory is valid for the lifetime of the bitmap.
mut fn data_ref_mut() -> ByteSlice; mut fn data_ref_mut() -> ByteSlice {
|slice| unsafe { ByteSlice::from_slice(slice) };
return(slice) { unsafe { ByteSlice::from_slice(slice) } };
};
); );
/// Consumes the Bitmap and returns the contained BitVec. /// Consumes the Bitmap and returns the contained BitVec.

View file

@ -40,7 +40,7 @@ wrap_free!(sp_bitvec::DisplayBitVec);
wrap_methods!( wrap_methods!(
sp_bitvec::DisplayBitVec; sp_bitvec::DisplayBitVec;
/// Gets the value of a bit. /// Gets the value of a bit.
/// ///
/// # Arguments /// # Arguments
@ -53,9 +53,11 @@ wrap_methods!(
/// # Panics /// # Panics
/// ///
/// - when accessing `index` out of bounds /// - when accessing `index` out of bounds
ref fn get(index: usize) -> bool; ref fn get(index: usize) -> bool {
|result| result.map(|x| *x).unwrap_or(false); return(result) { result.map(|x| *x).unwrap_or(false) };
};
/// Sets the value of a bit. /// Sets the value of a bit.
/// ///
/// # Arguments /// # Arguments
@ -67,25 +69,26 @@ wrap_methods!(
/// ///
/// - when accessing `index` out of bounds /// - when accessing `index` out of bounds
mut fn set(index: usize, value: bool); mut fn set(index: usize, value: bool);
/// Sets the value of all bits. /// Sets the value of all bits.
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `value`: the value to set all bits to /// - `value`: the value to set all bits to
mut fn fill(value: bool); mut fn fill(value: bool);
/// Gets the length in bits. /// Gets the length in bits.
ref fn len() -> usize; ref fn len() -> usize;
/// Returns true if length is 0. /// Returns true if length is 0.
ref fn is_empty() -> bool; ref fn is_empty() -> bool;
/// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
/// ///
/// The returned memory is valid for the lifetime of the bitvec. /// The returned memory is valid for the lifetime of the bitvec.
mut fn as_raw_mut_slice() -> ByteSlice; mut fn as_raw_mut_slice() -> ByteSlice {
|slice| unsafe { ByteSlice::from_slice(slice) }; return(slice) { unsafe { ByteSlice::from_slice(slice) } };
};
); );
/// Creates a [BitVecCommand] and immediately turns that into a [Packet]. /// Creates a [BitVecCommand] and immediately turns that into a [Packet].

View file

@ -57,7 +57,7 @@ wrap_free!(sp_brightness_grid::BrightnessGrid);
wrap_methods!( wrap_methods!(
sp_brightness_grid::BrightnessGrid; sp_brightness_grid::BrightnessGrid;
/// Gets the current value at the specified position. /// Gets the current value at the specified position.
/// ///
/// # Arguments /// # Arguments
@ -69,7 +69,7 @@ wrap_methods!(
/// # Panics /// # Panics
/// - When accessing `x` or `y` out of bounds. /// - When accessing `x` or `y` out of bounds.
ref fn get(x: usize, y: usize) -> Brightness; ref fn get(x: usize, y: usize) -> Brightness;
/// Sets the value of the specified position. /// Sets the value of the specified position.
/// ///
/// # Arguments /// # Arguments
@ -83,29 +83,30 @@ wrap_methods!(
/// ///
/// - When accessing `x` or `y` out of bounds. /// - When accessing `x` or `y` out of bounds.
mut fn set(x: usize, y: usize, value: Brightness); mut fn set(x: usize, y: usize, value: Brightness);
/// Sets the value of all cells. /// Sets the value of all cells.
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `value`: the value to set all cells to /// - `value`: the value to set all cells to
mut fn fill(value: Brightness); mut fn fill(value: Brightness);
/// Gets the width of the grid. /// Gets the width of the grid.
ref fn width() -> usize; ref fn width() -> usize;
/// Gets the height of the grid. /// Gets the height of the grid.
ref fn height() -> usize; ref fn height() -> usize;
/// Gets an unsafe reference to the data of the instance. /// Gets an unsafe reference to the data of the instance.
/// ///
/// The returned memory is valid for the lifetime of the grid. /// The returned memory is valid for the lifetime of the grid.
mut fn data_ref_mut() -> ByteSlice; mut fn data_ref_mut() -> ByteSlice {
|br_slice| unsafe { return(br_slice) { unsafe {
//noinspection RsAssertEqual //noinspection RsAssertEqual
const _: () = assert!(size_of::<Brightness>() == 1); const _: () = assert!(size_of::<Brightness>() == 1);
ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice))
}};
}; };
); );

View file

@ -44,7 +44,7 @@ wrap_free!(sp_char_grid::CharGrid);
wrap_methods!( wrap_methods!(
sp_char_grid::CharGrid; sp_char_grid::CharGrid;
/// Returns the current value at the specified position. /// Returns the current value at the specified position.
/// ///
/// # Arguments /// # Arguments
@ -54,9 +54,10 @@ wrap_methods!(
/// # Panics /// # Panics
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> u32; ref fn get(x: usize, y: usize) -> u32 {
| char | char as u32; return(char) { char as u32 };
};
/// Sets the value of the specified position in the grid. /// Sets the value of the specified position in the grid.
/// ///
/// # Arguments /// # Arguments
@ -70,21 +71,23 @@ wrap_methods!(
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
/// - when providing values that cannot be converted to Rust's `char`. /// - when providing values that cannot be converted to Rust's `char`.
mut fn set(x: usize, y: usize, value: u32); mut fn set(x: usize, y: usize, value: u32) {
let value = char::from_u32(value).unwrap(); prepare(value) { char::from_u32(value).unwrap() };
};
/// Sets the value of all cells in the grid. /// Sets the value of all cells in the grid.
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `value`: the value to set all cells to /// - `value`: the value to set all cells to
/// - when providing values that cannot be converted to Rust's `char`. /// - when providing values that cannot be converted to Rust's `char`.
mut fn fill(value: u32); mut fn fill(value: u32) {
let value = char::from_u32(value).unwrap(); prepare(value) { char::from_u32(value).unwrap() };
};
/// Gets the width of the grid. /// Gets the width of the grid.
ref fn width() -> usize; ref fn width() -> usize;
/// Gets the height of the grid. /// Gets the height of the grid.
ref fn height() -> usize; ref fn height() -> usize;
); );

View file

@ -73,12 +73,13 @@ wrap_methods!(
/// Gets the height of the grid. /// Gets the height of the grid.
ref fn height() -> usize; ref fn height() -> usize;
/// Gets an unsafe reference to the data of the grid. /// Gets an unsafe reference to the data of the grid.
/// ///
/// The returned memory is valid for the lifetime of the instance. /// The returned memory is valid for the lifetime of the instance.
mut fn data_ref_mut() -> ByteSlice; mut fn data_ref_mut() -> ByteSlice {
| slice | unsafe { ByteSlice::from_slice(slice) }; return(slice) { unsafe { ByteSlice::from_slice(slice) } };
};
); );
/// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet].

View file

@ -41,8 +41,12 @@ macro_rules! wrap_methods {
$( $(
$(#[$meta:meta])+ $(#[$meta:meta])+
$ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*)
$(-> $return_type:ty$(; |$it:ident | $return_expr:expr)?)?; $(-> $return_type:ty)?
$($(let $param_let_name:ident = $param_let_expr:expr);+;)? $({
$($(prepare($param_let_name:ident) $param_let_expr:block);+;)?
$(return($it:ident) $return_expr:block;)?
})?
;
)* )*
) => { ) => {
paste::paste! { paste::paste! {
@ -57,9 +61,9 @@ macro_rules! wrap_methods {
$($param_name: $param_type),* $($param_name: $param_type),*
) $(-> $return_type)? { ) $(-> $return_type)? {
let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) };
$( $($(
$(let $param_let_name = $param_let_expr;)* $(let $param_let_name = $param_let_expr;)*
)? )?)?
#[allow( #[allow(
unused_variables, unused_variables,
reason = "This variable may not be used depending on macro variables" )] reason = "This variable may not be used depending on macro variables" )]
@ -69,8 +73,8 @@ macro_rules! wrap_methods {
let $it = result; let $it = result;
let result = $return_expr; let result = $return_expr;
)? )?
return result;
)? )?
return result;
} }
)* )*
} }