WIP: type per command #4

Draft
vinzenz wants to merge 49 commits from next into main
6 changed files with 62 additions and 48 deletions
Showing only changes of commit bf4e351514 - Show all commits

View file

@ -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.

View file

@ -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].

View file

@ -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::<Brightness>() == 1);
mut fn data_ref_mut() -> ByteSlice {
return(br_slice) { unsafe {
//noinspection RsAssertEqual
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!(
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;
);

View file

@ -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].

View file

@ -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;
}
)*
}