WIP: type per command #4

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

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
containers::ByteSlice, 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}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
}; };
use servicepoint::{ use servicepoint::{
@ -83,8 +83,9 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec(
wrap_clone!(sp_bitmap::Bitmap); wrap_clone!(sp_bitmap::Bitmap);
wrap_free!(sp_bitmap::Bitmap); wrap_free!(sp_bitmap::Bitmap);
wrap_method!( 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
@ -95,10 +96,7 @@ wrap_method!(
/// ///
/// - 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;
);
wrap_method!(
sp_bitmap::Bitmap;
/// Sets the value of the specified position. /// Sets the value of the specified position.
/// ///
/// # Arguments /// # Arguments
@ -110,32 +108,20 @@ wrap_method!(
/// ///
/// - 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);
);
wrap_method!(
sp_bitmap::Bitmap;
/// 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);
);
wrap_method!(
sp_bitmap::Bitmap;
/// Gets the width in pixels. /// Gets the width in pixels.
ref fn width() -> usize; ref fn width() -> usize;
);
wrap_method!(
sp_bitmap::Bitmap;
/// Gets the height in pixels. /// Gets the height in pixels.
ref fn height() -> usize; ref fn height() -> usize;
);
wrap_method!(
sp_bitmap::Bitmap;
/// 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.

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
containers::ByteSlice, 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}, mem::{heap_move_nonnull, heap_move_ok, heap_remove},
}; };
use servicepoint::{ use servicepoint::{
@ -38,8 +38,9 @@ pub unsafe extern "C" fn sp_bitvec_load(
wrap_clone!(sp_bitvec::DisplayBitVec); wrap_clone!(sp_bitvec::DisplayBitVec);
wrap_free!(sp_bitvec::DisplayBitVec); wrap_free!(sp_bitvec::DisplayBitVec);
wrap_method!( wrap_methods!(
sp_bitvec::DisplayBitVec; sp_bitvec::DisplayBitVec;
/// Gets the value of a bit. /// Gets the value of a bit.
/// ///
/// # Arguments /// # Arguments
@ -54,10 +55,7 @@ wrap_method!(
/// - 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); |result| result.map(|x| *x).unwrap_or(false);
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Sets the value of a bit. /// Sets the value of a bit.
/// ///
/// # Arguments /// # Arguments
@ -69,32 +67,20 @@ wrap_method!(
/// ///
/// - 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);
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// 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);
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Gets the length in bits. /// Gets the length in bits.
ref fn len() -> usize; ref fn len() -> usize;
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// Returns true if length is 0. /// Returns true if length is 0.
ref fn is_empty() -> bool; ref fn is_empty() -> bool;
);
wrap_method!(
sp_bitvec::DisplayBitVec;
/// 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.

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
containers::ByteSlice, 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}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
}; };
use servicepoint::{ use servicepoint::{
@ -55,8 +55,9 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
wrap_clone!(sp_brightness_grid::BrightnessGrid); wrap_clone!(sp_brightness_grid::BrightnessGrid);
wrap_free!(sp_brightness_grid::BrightnessGrid); wrap_free!(sp_brightness_grid::BrightnessGrid);
wrap_method!( 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
@ -68,10 +69,7 @@ wrap_method!(
/// # 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;
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Sets the value of the specified position. /// Sets the value of the specified position.
/// ///
/// # Arguments /// # Arguments
@ -85,32 +83,20 @@ wrap_method!(
/// ///
/// - 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);
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// 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);
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Gets the width of the grid. /// Gets the width of the grid.
ref fn width() -> usize; ref fn width() -> usize;
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// Gets the height of the grid. /// Gets the height of the grid.
ref fn height() -> usize; ref fn height() -> usize;
);
wrap_method!(
sp_brightness_grid::BrightnessGrid;
/// 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.

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
containers::ByteSlice, 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}, mem::{heap_move_nonnull, heap_move_ok, heap_remove},
}; };
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; 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_clone!(sp_char_grid::CharGrid);
wrap_free!(sp_char_grid::CharGrid); wrap_free!(sp_char_grid::CharGrid);
wrap_method!( 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,11 +55,8 @@ wrap_method!(
/// ///
/// - 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 | char | char as u32;
);
wrap_method!(
sp_char_grid::CharGrid;
/// Sets the value of the specified position in the grid. /// Sets the value of the specified position in the grid.
/// ///
/// # Arguments /// # Arguments
@ -74,10 +72,7 @@ wrap_method!(
/// - 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(); let value = char::from_u32(value).unwrap();
);
wrap_method!(
sp_char_grid::CharGrid;
/// Sets the value of all cells in the grid. /// Sets the value of all cells in the grid.
/// ///
/// # Arguments /// # Arguments
@ -86,16 +81,10 @@ wrap_method!(
/// - 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(); let value = char::from_u32(value).unwrap();
);
wrap_method!(
sp_char_grid::CharGrid;
/// Gets the width of the grid. /// Gets the width of the grid.
ref fn width() -> usize; ref fn width() -> usize;
);
wrap_method!(
sp_char_grid::CharGrid;
/// Gets the height of the grid. /// Gets the height of the grid.
ref fn height() -> usize; ref fn height() -> usize;
); );

View file

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

View file

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