container macro code dedup

This commit is contained in:
Vinzenz Schroeter 2025-06-22 12:52:53 +02:00
parent 5ecb84ed16
commit b4730ffdf3
11 changed files with 148 additions and 222 deletions

View file

@ -656,11 +656,11 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance);
/**
* Calls method [`servicepoint::Bitmap::fill`].
*
* Sets the state of all pixels in the [Bitmap].
* Sets the state of all cells in the grid.
*
* # Arguments
*
* - `value`: the value to set all pixels to
* - `value`: the value to set all cells to
*
* This function is part of the `bitmap` module.
*/
@ -704,7 +704,7 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y);
/**
* Calls method [`servicepoint::Bitmap::height`].
*
* Gets the height in pixels.
* Gets the height.
*
* This function is part of the `bitmap` module.
*/
@ -809,7 +809,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance,
/**
* Calls method [`servicepoint::Bitmap::width`].
*
* Gets the width in pixels.
* Gets the width.
*
* This function is part of the `bitmap` module.
*/
@ -914,46 +914,6 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command,
*/
struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ command);
/**
* Creates a [BitVecCommand] and immediately turns that into a [Packet].
*
* The provided [DisplayBitVec] gets consumed.
*
* Returns NULL in case of an error.
*
* This function is part of the `bitvec` module.
*/
struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec,
size_t offset,
BinaryOperation operation,
CompressionCode compression);
/**
* Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
*
* returns: [DisplayBitVec] instance containing data.
*
* This function is part of the `bitvec` module.
*/
BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data);
/**
* Creates a new [DisplayBitVec] instance.
*
* # Arguments
*
* - `size`: size in bits.
*
* returns: [DisplayBitVec] with all bits set to false.
*
* # Panics
*
* - when `size` is not divisible by 8.
*
* This function is part of the `bitvec` module.
*/
BitVec */*notnull*/ sp_bitvec_new(size_t size);
/**
*Clones a [`BitVecCommand`] instance.
*
@ -1083,7 +1043,7 @@ struct ByteSlice sp_brightnessgrid_data_ref_mut(BrightnessGrid */*notnull*/ inst
/**
* Calls method [`servicepoint::BrightnessGrid::fill`].
*
* Sets the value of all cells.
* Sets the state of all cells in the grid.
*
* # Arguments
*
@ -1110,10 +1070,9 @@ void sp_brightnessgrid_free(BrightnessGrid */*notnull*/ instance);
*
* - `x` and `y`: position of the cell to read
*
* returns: value at position
*
* # Panics
* - When accessing `x` or `y` out of bounds.
*
* - when accessing `x` or `y` out of bounds
*
* This function is part of the `brightnessgrid` module.
*/
@ -1124,7 +1083,7 @@ Brightness sp_brightnessgrid_get(BrightnessGrid */*notnull*/ instance,
/**
* Calls method [`servicepoint::BrightnessGrid::height`].
*
* Gets the height of the grid.
* Gets the height.
*
* This function is part of the `brightnessgrid` module.
*/
@ -1189,11 +1148,9 @@ BrightnessGrid */*notnull*/ sp_brightnessgrid_new(size_t width, size_t height);
* - `x` and `y`: position of the cell
* - `value`: the value to write to the cell
*
* returns: old value of the cell
*
* # Panics
*
* - When accessing `x` or `y` out of bounds.
* - when accessing `x` or `y` out of bounds
*
* This function is part of the `brightnessgrid` module.
*/
@ -1205,7 +1162,7 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance,
/**
* Calls method [`servicepoint::BrightnessGrid::width`].
*
* Gets the width of the grid.
* Gets the width.
*
* This function is part of the `brightnessgrid` module.
*/
@ -1340,7 +1297,7 @@ uint32_t sp_chargrid_get(CharGrid */*notnull*/ instance, size_t x, size_t y);
/**
* Calls method [`servicepoint::CharGrid::height`].
*
* Gets the height of the grid.
* Gets the height.
*
* This function is part of the `chargrid` module.
*/
@ -1413,7 +1370,7 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance,
/**
* Calls method [`servicepoint::CharGrid::width`].
*
* Gets the width of the grid.
* Gets the width.
*
* This function is part of the `chargrid` module.
*/
@ -1622,11 +1579,10 @@ struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance);
/**
* Calls method [`servicepoint::Cp437Grid::fill`].
*
* Sets the value of all cells in the grid.
* Sets the state of all cells in the grid.
*
* # Arguments
*
* - `cp437_grid`: instance to write to
* - `value`: the value to set all cells to
*
* This function is part of the `cp437grid` module.
@ -1660,7 +1616,7 @@ uint8_t sp_cp437grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y);
/**
* Calls method [`servicepoint::Cp437Grid::height`].
*
* Gets the height of the grid.
* Gets the height.
*
* This function is part of the `cp437grid` module.
*/
@ -1700,15 +1656,13 @@ Cp437Grid */*notnull*/ sp_cp437grid_new(size_t width, size_t height);
/**
* Calls method [`servicepoint::Cp437Grid::set`].
*
* Sets the value at the specified position.
* Sets the value of the specified position.
*
* # Arguments
*
* - `x` and `y`: position of the cell
* - `value`: the value to write to the cell
*
* returns: old value of the cell
*
* # Panics
*
* - when accessing `x` or `y` out of bounds
@ -1723,7 +1677,7 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance,
/**
* Calls method [`servicepoint::Cp437Grid::width`].
*
* Gets the width of the grid.
* Gets the width.
*
* This function is part of the `cp437grid` module.
*/
@ -1847,6 +1801,20 @@ void sp_displaybitvec_free(BitVec */*notnull*/ instance);
*/
bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index);
/**
* Creates a [BitVecCommand] and immediately turns that into a [Packet].
*
* The provided [DisplayBitVec] gets consumed.
*
* Returns NULL in case of an error.
*
* This function is part of the `displaybitvec` module.
*/
struct Packet *sp_displaybitvec_into_packet(BitVec */*notnull*/ bitvec,
size_t offset,
BinaryOperation operation,
CompressionCode compression);
/**
* Calls method [`servicepoint::DisplayBitVec::is_empty`].
*
@ -1865,6 +1833,32 @@ bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance);
*/
size_t sp_displaybitvec_len(BitVec */*notnull*/ instance);
/**
* Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
*
* returns: [DisplayBitVec] instance containing data.
*
* This function is part of the `displaybitvec` module.
*/
BitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data);
/**
* Creates a new [DisplayBitVec] instance.
*
* # Arguments
*
* - `size`: size in bits.
*
* returns: [DisplayBitVec] with all bits set to false.
*
* # Panics
*
* - when `size` is not divisible by 8.
*
* This function is part of the `displaybitvec` module.
*/
BitVec */*notnull*/ sp_displaybitvec_new(size_t size);
/**
* Calls method [`servicepoint::DisplayBitVec::set`].
*

View file

@ -83,8 +83,8 @@ macro_rules! derive_command_into_packet {
macro_rules! wrap_command {
($command:ident, $object_type:ident) => {
$crate::macros::wrap_clone!($object_type);
$crate::macros::wrap_free!($object_type);
$crate::macros::derive_clone!($object_type);
$crate::macros::derive_free!($object_type);
$crate::commands::derive_command_from!($command);
$crate::commands::derive_command_into_packet!($object_type);
};

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
containers::{wrap_grid, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
@ -9,8 +9,7 @@ use servicepoint::{
};
use std::ptr::NonNull;
wrap_clone!(Bitmap);
wrap_free!(Bitmap);
wrap_grid!(Bitmap, bool);
wrap_functions!(bitmap;
@ -107,42 +106,6 @@ wrap_functions!(bitmap;
wrap_methods!(Bitmap;
/// Gets the current value at the specified position.
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> bool;
/// Sets the value of the specified position.
///
/// # Arguments
///
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// # Panics
///
/// - 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.

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
containers::{wrap_container, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{
@ -8,7 +8,9 @@ use servicepoint::{
};
use std::ptr::NonNull;
wrap_functions!(bitvec;
wrap_container!(DisplayBitVec);
wrap_functions!(associate DisplayBitVec;
/// Creates a new [DisplayBitVec] instance.
///
@ -55,9 +57,6 @@ wrap_functions!(bitvec;
);
wrap_clone!(DisplayBitVec);
wrap_free!(DisplayBitVec);
wrap_methods!(DisplayBitVec;
/// Gets the value of a bit.
@ -76,7 +75,6 @@ wrap_methods!(DisplayBitVec;
return(result) { result.map(|x| *x).unwrap_or(false) };
};
/// Sets the value of a bit.
///
/// # Arguments

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
containers::{wrap_grid, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
@ -9,7 +9,9 @@ use servicepoint::{
};
use std::{mem::transmute, ptr::NonNull};
wrap_functions!(brightnessgrid;
wrap_grid!(BrightnessGrid, Brightness);
wrap_functions!(associate BrightnessGrid;
/// Creates a new [BrightnessGrid] with the specified dimensions.
///
@ -71,50 +73,7 @@ wrap_functions!(brightnessgrid;
);
wrap_clone!(BrightnessGrid);
wrap_free!(BrightnessGrid);
wrap_methods!(BrightnessGrid;
/// Gets the current value at the specified position.
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// returns: value at position
///
/// # 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
///
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// returns: old value of the cell
///
/// # Panics
///
/// - 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.

View file

@ -1,12 +1,15 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
containers::{derive_get_width_height, wrap_container, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
use std::ptr::NonNull;
wrap_functions!(chargrid;
wrap_container!(CharGrid);
derive_get_width_height!(CharGrid);
wrap_functions!(associate CharGrid;
/// Creates a new [CharGrid] with the specified dimensions.
///
@ -58,9 +61,6 @@ wrap_functions!(chargrid;
);
wrap_clone!(CharGrid);
wrap_free!(CharGrid);
wrap_methods!(CharGrid;
/// Returns the current value at the specified position.
@ -102,10 +102,4 @@ wrap_methods!(CharGrid;
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

@ -1,7 +1,6 @@
use crate::macros::wrap_functions;
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_free, wrap_methods},
containers::{wrap_grid, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
@ -9,6 +8,8 @@ use servicepoint::{
};
use std::ptr::NonNull;
wrap_grid!(Cp437Grid, u8);
wrap_functions!(cp437grid;
/// Creates a new [Cp437Grid] with the specified dimensions.
@ -50,49 +51,7 @@ wrap_functions!(cp437grid;
);
wrap_clone!(Cp437Grid);
wrap_free!(Cp437Grid);
wrap_methods!(Cp437Grid;
/// Gets the current value at the specified position.
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> u8;
/// Sets the value at the specified position.
///
/// # Arguments
///
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// returns: old value of the cell
///
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
mut fn set(x: usize, y: usize, value: u8);
/// Sets the value of all cells in the grid.
///
/// # Arguments
///
/// - `cp437_grid`: instance to write to
/// - `value`: the value to set all cells to
mut fn fill(value: u8);
/// 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 grid.
///
/// The returned memory is valid for the lifetime of the instance.

View file

@ -12,6 +12,65 @@ pub use byte_slice::*;
pub use char_grid::*;
pub use cp437_grid::*;
macro_rules! wrap_container {
($object_type:ident) => {
derive_clone!($object_type);
derive_free!($object_type);
};
}
macro_rules! derive_get_width_height {
($object_type:ident) => {
$crate::macros::wrap_methods! {$object_type;
/// Gets the width.
ref fn width() -> usize;
/// Gets the height.
ref fn height() -> usize;
}
};
}
macro_rules! wrap_grid {
($object_type:ident, $value_type:ident) => {
$crate::containers::wrap_container!($object_type);
$crate::containers::derive_get_width_height!($object_type);
$crate::macros::wrap_methods! {$object_type;
/// Gets the current value at the specified position.
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
ref fn get(x: usize, y: usize) -> $value_type;
/// Sets the value of the specified position.
///
/// # Arguments
///
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
mut fn set(x: usize, y: usize, value: $value_type);
/// Sets the state of all cells in the grid.
///
/// # Arguments
///
/// - `value`: the value to set all cells to
mut fn fill(value: $value_type);
}
};
}
pub(crate) use {derive_get_width_height, wrap_container, wrap_grid};
mod _hidden {
/// This is a type only used by cbindgen to have a type for pointers.
#[allow(unused)]

View file

@ -1,4 +1,4 @@
macro_rules! wrap_free {
macro_rules! derive_free {
($typ:ident) => {
::paste::paste! {
$crate::macros::wrap_functions!([< $typ:lower >];
@ -11,7 +11,7 @@ macro_rules! wrap_free {
};
}
macro_rules! wrap_clone {
macro_rules! derive_clone {
($typ:ident) => {
::paste::paste! {
$crate::macros::wrap_functions!([< $typ:lower >];
@ -254,6 +254,6 @@ macro_rules! wrap_functions {
}
pub(crate) use {
nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_fields, wrap_free,
derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields,
wrap_functions, wrap_methods,
};

View file

@ -1,6 +1,6 @@
use crate::{
containers::ByteSlice,
macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions},
macros::{derive_clone, derive_free, wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_move_ok},
};
use servicepoint::{CommandCode, Header, Packet};
@ -68,8 +68,8 @@ wrap_functions!(packet;
}
);
wrap_clone!(Packet);
wrap_free!(Packet);
derive_clone!(Packet);
derive_free!(Packet);
wrap_fields!(Packet;
prop header: Header {

View file

@ -1,6 +1,6 @@
use crate::{
commands::{CommandTag, SPCommand},
macros::{wrap_free, wrap_functions},
macros::{derive_free, wrap_functions},
mem::{heap_move_ok, heap_remove},
};
use servicepoint::{Header, Packet, UdpSocketExt};
@ -10,7 +10,7 @@ use std::{
ptr::NonNull,
};
wrap_free!(UdpSocket);
derive_free!(UdpSocket);
wrap_functions!(associate UdpSocket;