multiple methods in one macro invocation

This commit is contained in:
Vinzenz Schroeter 2025-06-18 17:39:30 +02:00
parent 5e1a466bbf
commit b92dcc565a
6 changed files with 48 additions and 111 deletions

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_method},
macros::{wrap_clone, wrap_free, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
@ -83,8 +83,9 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec(
wrap_clone!(sp_bitmap::Bitmap);
wrap_free!(sp_bitmap::Bitmap);
wrap_method!(
wrap_methods!(
sp_bitmap::Bitmap;
/// Gets the current value at the specified position.
///
/// # Arguments
@ -95,10 +96,7 @@ wrap_method!(
///
/// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> bool;
);
wrap_method!(
sp_bitmap::Bitmap;
/// Sets the value of the specified position.
///
/// # Arguments
@ -110,32 +108,20 @@ wrap_method!(
///
/// - when accessing `x` or `y` out of bounds
mut fn set(x: usize, y: usize, value: bool);
);
wrap_method!(
sp_bitmap::Bitmap;
/// Sets the state of all pixels in the [Bitmap].
///
/// # Arguments
///
/// - `value`: the value to set all pixels to
mut fn fill(value: bool);
);
wrap_method!(
sp_bitmap::Bitmap;
/// Gets the width in pixels.
ref fn width() -> usize;
);
wrap_method!(
sp_bitmap::Bitmap;
/// Gets the height in pixels.
ref fn height() -> usize;
);
wrap_method!(
sp_bitmap::Bitmap;
/// Gets an unsafe reference to the data of the [Bitmap] instance.
///
/// The returned memory is valid for the lifetime of the bitmap.

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_method},
macros::{wrap_clone, wrap_free, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{
@ -38,8 +38,9 @@ pub unsafe extern "C" fn sp_bitvec_load(
wrap_clone!(sp_bitvec::DisplayBitVec);
wrap_free!(sp_bitvec::DisplayBitVec);
wrap_method!(
wrap_methods!(
sp_bitvec::DisplayBitVec;
/// Gets the value of a bit.
///
/// # Arguments
@ -54,10 +55,7 @@ wrap_method!(
/// - when accessing `index` out of bounds
ref fn get(index: usize) -> bool;
|result| result.map(|x| *x).unwrap_or(false);
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Sets the value of a bit.
///
/// # Arguments
@ -69,32 +67,20 @@ wrap_method!(
///
/// - when accessing `index` out of bounds
mut fn set(index: usize, value: bool);
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Sets the value of all bits.
///
/// # Arguments
///
/// - `value`: the value to set all bits to
mut fn fill(value: bool);
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Gets the length in bits.
ref fn len() -> usize;
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Returns true if length is 0.
ref fn is_empty() -> bool;
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
///
/// The returned memory is valid for the lifetime of the bitvec.

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_method},
macros::{wrap_clone, wrap_free, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
@ -55,8 +55,9 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
wrap_clone!(sp_brightness_grid::BrightnessGrid);
wrap_free!(sp_brightness_grid::BrightnessGrid);
wrap_method!(
wrap_methods!(
sp_brightness_grid::BrightnessGrid;
/// Gets the current value at the specified position.
///
/// # Arguments
@ -68,10 +69,7 @@ wrap_method!(
/// # Panics
/// - When accessing `x` or `y` out of bounds.
ref fn get(x: usize, y: usize) -> Brightness;
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Sets the value of the specified position.
///
/// # Arguments
@ -85,32 +83,20 @@ wrap_method!(
///
/// - When accessing `x` or `y` out of bounds.
mut fn set(x: usize, y: usize, value: Brightness);
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Sets the value of all cells.
///
/// # Arguments
///
/// - `value`: the value to set all cells to
mut fn fill(value: Brightness);
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Gets the width of the grid.
ref fn width() -> usize;
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Gets the height of the grid.
ref fn height() -> usize;
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Gets an unsafe reference to the data of the instance.
///
/// The returned memory is valid for the lifetime of the grid.

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_method},
macros::{wrap_clone, wrap_free, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
@ -42,8 +42,9 @@ pub unsafe extern "C" fn sp_char_grid_load(
wrap_clone!(sp_char_grid::CharGrid);
wrap_free!(sp_char_grid::CharGrid);
wrap_method!(
wrap_methods!(
sp_char_grid::CharGrid;
/// Returns the current value at the specified position.
///
/// # Arguments
@ -54,11 +55,8 @@ wrap_method!(
///
/// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> u32;
| char | char as u32
);
| char | char as u32;
wrap_method!(
sp_char_grid::CharGrid;
/// Sets the value of the specified position in the grid.
///
/// # Arguments
@ -74,10 +72,7 @@ wrap_method!(
/// - 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();
);
wrap_method!(
sp_char_grid::CharGrid;
/// Sets the value of all cells in the grid.
///
/// # Arguments
@ -86,16 +81,10 @@ wrap_method!(
/// - when providing values that cannot be converted to Rust's `char`.
mut fn fill(value: u32);
let value = char::from_u32(value).unwrap();
);
wrap_method!(
sp_char_grid::CharGrid;
/// Gets the width of the grid.
ref fn width() -> usize;
);
wrap_method!(
sp_char_grid::CharGrid;
/// Gets the height of the grid.
ref fn height() -> usize;
);

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_method},
macros::{wrap_clone, wrap_free, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
@ -33,7 +33,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
wrap_clone!(sp_cp437_grid::Cp437Grid);
wrap_free!(sp_cp437_grid::Cp437Grid);
wrap_method!(
wrap_methods!(
sp_cp437_grid::Cp437Grid;
/// Gets the current value at the specified position.
///
@ -45,10 +45,7 @@ wrap_method!(
///
/// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> u8;
);
wrap_method!(
sp_cp437_grid::Cp437Grid;
/// Sets the value at the specified position.
///
/// # Arguments
@ -62,10 +59,7 @@ wrap_method!(
///
/// - when accessing `x` or `y` out of bounds
mut fn set(x: usize, y: usize, value: u8);
);
wrap_method!(
sp_cp437_grid::Cp437Grid;
/// Sets the value of all cells in the grid.
///
/// # Arguments
@ -73,22 +67,13 @@ wrap_method!(
/// - `cp437_grid`: instance to write to
/// - `value`: the value to set all cells to
mut fn fill(value: u8);
);
wrap_method!(
sp_cp437_grid::Cp437Grid;
/// Gets the width of the grid.
ref fn width() -> usize;
);
wrap_method!(
sp_cp437_grid::Cp437Grid;
/// Gets the height of the grid.
ref fn height() -> usize;
);
wrap_method!(
sp_cp437_grid::Cp437Grid;
/// Gets an unsafe reference to the data of the grid.
///
/// The returned memory is valid for the lifetime of the instance.

View file

@ -35,16 +35,18 @@ macro_rules! nonnull_as_mut {
}
// meta required on purpose, because otherwise the added documentation would suppress warnings
macro_rules! wrap_method {
macro_rules! wrap_methods {
(
$prefix:ident :: $object_type:ty;
$(
$(#[$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$(; |$it:ident | $return_expr:expr)?)?;
$($(let $param_let_name:ident = $param_let_expr:expr);+;)?
)*
) => {
paste::paste! {
$(
#[doc = concat!(" Calls [`servicepoint::", stringify!($object_type),
"::", stringify!($function), "`].")]
#[doc = ""]
@ -55,7 +57,9 @@ macro_rules! wrap_method {
$($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" )]
@ -68,6 +72,7 @@ macro_rules! wrap_method {
return result;
)?
}
)*
}
};
}
@ -142,5 +147,5 @@ macro_rules! wrap_fields {
pub(crate) use {
nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_fields, wrap_free,
wrap_method,
wrap_methods,
};