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

@ -125,8 +125,10 @@ wrap_methods!(
/// 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

@ -53,8 +53,10 @@ 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.
/// ///
@ -84,8 +86,9 @@ wrap_methods!(
/// 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

@ -100,12 +100,13 @@ wrap_methods!(
/// 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

@ -54,8 +54,9 @@ 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.
/// ///
@ -70,8 +71,9 @@ 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.
/// ///
@ -79,8 +81,9 @@ wrap_methods!(
/// ///
/// - `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;

View file

@ -77,8 +77,9 @@ wrap_methods!(
/// 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;
} }
)* )*
} }