From 18f0be072a92d774ac02a170e32910ab0fb12ae6 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 19 Jun 2025 18:32:07 +0200 Subject: [PATCH 01/17] fix docs, misc tweaks --- include/servicepoint.h | 60 ++++++++++++++++--------------- src/containers/brightness_grid.rs | 11 +++--- src/containers/byte_slice.rs | 3 +- src/containers/mod.rs | 5 +++ src/lib.rs | 12 ++----- src/macros.rs | 2 +- src/udp.rs | 8 +++++ 7 files changed, 56 insertions(+), 45 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 68d4c57..8fa2ab5 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -412,6 +412,8 @@ typedef struct Packet Packet; /** * This is a type only used by cbindgen to have a type for pointers. + * + * See [servicepoint::UdpSocketExt]. */ typedef struct UdpSocket UdpSocket; @@ -641,7 +643,7 @@ extern "C" { struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); /** - * Calls [`servicepoint::Bitmap::data_ref_mut`]. + * Calls method [`servicepoint::Bitmap::data_ref_mut`]. * * Gets an unsafe reference to the data of the [Bitmap] instance. * @@ -652,7 +654,7 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); /** - * Calls [`servicepoint::Bitmap::fill`]. + * Calls method [`servicepoint::Bitmap::fill`]. * * Sets the state of all pixels in the [Bitmap]. * @@ -683,7 +685,7 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); /** - * Calls [`servicepoint::Bitmap::get`]. + * Calls method [`servicepoint::Bitmap::get`]. * * Gets the current value at the specified position. * @@ -700,7 +702,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); /** - * Calls [`servicepoint::Bitmap::height`]. + * Calls method [`servicepoint::Bitmap::height`]. * * Gets the height in pixels. * @@ -784,7 +786,7 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); /** - * Calls [`servicepoint::Bitmap::set`]. + * Calls method [`servicepoint::Bitmap::set`]. * * Sets the value of the specified position. * @@ -805,7 +807,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, bool value); /** - * Calls [`servicepoint::Bitmap::width`]. + * Calls method [`servicepoint::Bitmap::width`]. * * Gets the width in pixels. * @@ -814,7 +816,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. + * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. * * Gets an unsafe reference to the data of the [DisplayBitVec] instance. * @@ -832,7 +834,7 @@ struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::fill`]. + * Calls method [`servicepoint::DisplayBitVec::fill`]. * * Sets the value of all bits. * @@ -852,7 +854,7 @@ void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); void sp_bitvec_free(BitVec */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::get`]. + * Calls method [`servicepoint::DisplayBitVec::get`]. * * Gets the value of a bit. * @@ -886,7 +888,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, CompressionCode compression); /** - * Calls [`servicepoint::DisplayBitVec::is_empty`]. + * Calls method [`servicepoint::DisplayBitVec::is_empty`]. * * Returns true if length is 0. * @@ -895,7 +897,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::len`]. + * Calls method [`servicepoint::DisplayBitVec::len`]. * * Gets the length in bits. * @@ -930,7 +932,7 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); BitVec */*notnull*/ sp_bitvec_new(size_t size); /** - * Calls [`servicepoint::DisplayBitVec::set`]. + * Calls method [`servicepoint::DisplayBitVec::set`]. * * Sets the value of a bit. * @@ -955,7 +957,7 @@ void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); /** - * Calls [`servicepoint::BrightnessGrid::data_ref_mut`]. + * Calls method [`servicepoint::BrightnessGrid::data_ref_mut`]. * * Gets an unsafe reference to the data of the instance. * @@ -966,7 +968,7 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); /** - * Calls [`servicepoint::BrightnessGrid::fill`]. + * Calls method [`servicepoint::BrightnessGrid::fill`]. * * Sets the value of all cells. * @@ -987,7 +989,7 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); /** - * Calls [`servicepoint::BrightnessGrid::get`]. + * Calls method [`servicepoint::BrightnessGrid::get`]. * * Gets the current value at the specified position. * @@ -1007,7 +1009,7 @@ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, size_t y); /** - * Calls [`servicepoint::BrightnessGrid::height`]. + * Calls method [`servicepoint::BrightnessGrid::height`]. * * Gets the height of the grid. * @@ -1065,7 +1067,7 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); /** - * Calls [`servicepoint::BrightnessGrid::set`]. + * Calls method [`servicepoint::BrightnessGrid::set`]. * * Sets the value of the specified position. * @@ -1088,7 +1090,7 @@ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, Brightness value); /** - * Calls [`servicepoint::BrightnessGrid::width`]. + * Calls method [`servicepoint::BrightnessGrid::width`]. * * Gets the width of the grid. * @@ -1104,7 +1106,7 @@ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); /** - * Calls [`servicepoint::CharGrid::fill`]. + * Calls method [`servicepoint::CharGrid::fill`]. * * Sets the value of all cells in the grid. * @@ -1125,7 +1127,7 @@ void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); void sp_char_grid_free(CharGrid */*notnull*/ instance); /** - * Calls [`servicepoint::CharGrid::get`]. + * Calls method [`servicepoint::CharGrid::get`]. * * Returns the current value at the specified position. * @@ -1142,7 +1144,7 @@ void sp_char_grid_free(CharGrid */*notnull*/ instance); uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); /** - * Calls [`servicepoint::CharGrid::height`]. + * Calls method [`servicepoint::CharGrid::height`]. * * Gets the height of the grid. * @@ -1191,7 +1193,7 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); /** - * Calls [`servicepoint::CharGrid::set`]. + * Calls method [`servicepoint::CharGrid::set`]. * * Sets the value of the specified position in the grid. * @@ -1215,7 +1217,7 @@ void sp_char_grid_set(CharGrid */*notnull*/ instance, uint32_t value); /** - * Calls [`servicepoint::CharGrid::width`]. + * Calls method [`servicepoint::CharGrid::width`]. * * Gets the width of the grid. * @@ -1824,7 +1826,7 @@ struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); /** - * Calls [`servicepoint::Cp437Grid::data_ref_mut`]. + * Calls method [`servicepoint::Cp437Grid::data_ref_mut`]. * * Gets an unsafe reference to the data of the grid. * @@ -1835,7 +1837,7 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); /** - * Calls [`servicepoint::Cp437Grid::fill`]. + * Calls method [`servicepoint::Cp437Grid::fill`]. * * Sets the value of all cells in the grid. * @@ -1856,7 +1858,7 @@ void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); /** - * Calls [`servicepoint::Cp437Grid::get`]. + * Calls method [`servicepoint::Cp437Grid::get`]. * * Gets the current value at the specified position. * @@ -1873,7 +1875,7 @@ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** - * Calls [`servicepoint::Cp437Grid::height`]. + * Calls method [`servicepoint::Cp437Grid::height`]. * * Gets the height of the grid. * @@ -1913,7 +1915,7 @@ Cp437Grid *sp_cp437_grid_load(size_t width, Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); /** - * Calls [`servicepoint::Cp437Grid::set`]. + * Calls method [`servicepoint::Cp437Grid::set`]. * * Sets the value at the specified position. * @@ -1936,7 +1938,7 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, uint8_t value); /** - * Calls [`servicepoint::Cp437Grid::width`]. + * Calls method [`servicepoint::Cp437Grid::width`]. * * Gets the width of the grid. * diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 2374b47..e266bef 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,7 +1,6 @@ -use crate::macros::wrap_functions; use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_methods}, + macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -121,11 +120,13 @@ wrap_methods!( /// /// The returned memory is valid for the lifetime of the grid. mut fn data_ref_mut() -> ByteSlice { - return(br_slice) { unsafe { + return(br_slice) { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) - }}; + unsafe { + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + } + }; }; ); diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index 10ee22f..66bdce2 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -26,7 +26,8 @@ pub struct ByteSlice { } impl ByteSlice { - pub(crate) const INVALID: ByteSlice = ByteSlice { + /// Represents an invalid [ByteSlice] instance. + pub const INVALID: ByteSlice = ByteSlice { start: std::ptr::null_mut(), length: 0, }; diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 62d51d0..b9460e9 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -11,3 +11,8 @@ pub use brightness_grid::*; pub use byte_slice::*; pub use char_grid::*; pub use cp437_grid::*; + +mod _hidden { + /// This is a type only used by cbindgen to have a type for pointers. + pub struct DisplayBitVec; +} diff --git a/src/lib.rs b/src/lib.rs index 344a1a2..4df864c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,22 +29,16 @@ pub mod commands; /// Functions related to [servicepoint::Bitmap], [servicepoint::CharGrid] and friends. pub mod containers; -mod macros; +pub(crate) mod macros; pub(crate) mod mem; -/// Functions related to [Packet]. +/// Functions related to [servicepoint::Packet]. pub mod packet; -/// Functions related to [UdpSocket]. +/// Functions related to [servicepoint::UdpSocketExt]. pub mod udp; /// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. pub const SP_FRAME_PACING_MS: u128 = 30; -/// This is a type only used by cbindgen to have a type for pointers. -pub struct UdpSocket; - -/// This is a type only used by cbindgen to have a type for pointers. -pub struct DisplayBitVec; - #[cfg(feature = "env_logger")] mod feature_env_logger { use crate::macros::wrap_functions; diff --git a/src/macros.rs b/src/macros.rs index 4157f28..c66808d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -50,7 +50,7 @@ macro_rules! wrap_methods { paste::paste! { $crate::macros::wrap_functions!($prefix; $( - #[doc = concat!(" Calls [`servicepoint::", stringify!($object_type), + #[doc = concat!(" Calls method [`servicepoint::", stringify!($object_type), "::", stringify!($function), "`].")] #[doc = ""] $(#[$meta])* diff --git a/src/udp.rs b/src/udp.rs index cb96547..a969512 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -129,3 +129,11 @@ wrap_functions!(sp_udp; } ); + +mod _hidden { + /// This is a type only used by cbindgen to have a type for pointers. + /// + /// See [servicepoint::UdpSocketExt]. + #[allow(unused)] + pub struct UdpSocket; +} From 1eb59d986a2a09dfe58e1b708adf6bbc6cc82523 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 21 Jun 2025 23:25:52 +0200 Subject: [PATCH 02/17] wrap_command macro --- example/src/header_logger.c | 12 +- include/servicepoint.h | 137 +++++++++++++--------- src/commands/bitmap_command.rs | 7 +- src/commands/bitvec_command.rs | 6 +- src/commands/brightness_grid_command.rs | 7 +- src/commands/cc_only_commands.rs | 28 +++-- src/commands/char_grid_command.rs | 7 +- src/commands/cp437_grid_command.rs | 7 +- src/commands/generic_command.rs | 10 +- src/commands/global_brightness_command.rs | 6 +- src/commands/mod.rs | 33 +++++- src/macros.rs | 7 +- src/udp.rs | 2 +- 13 files changed, 162 insertions(+), 107 deletions(-) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 9cb3cd1..5f04ac4 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -33,7 +33,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); - Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); + Bitmap *bitmap = sp_cmd_bitmap_get_bitmap_mut(bitmapCommand); size_t w = sp_bitmap_width(bitmap); size_t h = sp_bitmap_height(bitmap); @@ -47,7 +47,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); - BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); + BrightnessGrid *grid = sp_cmd_brightness_grid_get_grid_mut(gridCommand); size_t w = sp_brightness_grid_width(grid); size_t h = sp_brightness_grid_height(grid); @@ -61,7 +61,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_char_grid_get_origin(gridCommand, &x, &y); - CharGrid *grid = sp_cmd_char_grid_get(gridCommand); + CharGrid *grid = sp_cmd_char_grid_get_grid_mut(gridCommand); size_t w = sp_char_grid_width(grid); size_t h = sp_char_grid_height(grid); @@ -75,7 +75,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); - Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); + Cp437Grid *grid = sp_cmd_cp437_grid_get_grid_mut(gridCommand); size_t w = sp_cp437_grid_width(grid); size_t h = sp_cp437_grid_height(grid); @@ -84,7 +84,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_BIT_VEC: { - BitVecCommand *bitvecCommand = command.data.bitvec; + BitVecCommand *bitvecCommand = command.data.bit_vec; size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); @@ -109,7 +109,7 @@ bool log_command(struct Command command) { break; } - BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); + BitVec *bitvec = sp_cmd_bitvec_get_bitvec_mut(bitvecCommand); size_t len = sp_bitvec_len(bitvec); printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", diff --git a/include/servicepoint.h b/include/servicepoint.h index 8fa2ab5..50adba6 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -99,7 +99,7 @@ typedef uint8_t BinaryOperation; #endif // __cplusplus /** - * The u16 command codes used for the [Command]s. + * The u16 command codes used for the [`crate::Command`]s. */ enum CommandCode #ifdef __cplusplus @@ -560,7 +560,7 @@ typedef struct ValueGrid_u8 Cp437Grid; typedef union CommandUnion { uint8_t *null; struct BitmapCommand */*notnull*/ bitmap; - struct BitVecCommand */*notnull*/ bitvec; + struct BitVecCommand */*notnull*/ bit_vec; struct BrightnessGridCommand */*notnull*/ brightness_grid; struct CharGridCommand */*notnull*/ char_grid; struct Cp437GridCommand */*notnull*/ cp437_grid; @@ -1432,20 +1432,6 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, */ struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); -/** - *Clones a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the sp_cmd_brightness_global module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - *Deallocates a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the sp_cmd_brightness_global module. - */ -void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance); - /** * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * @@ -1477,20 +1463,6 @@ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightn void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, Brightness value); -/** - *Clones a [`BrightnessGridCommand`] instance. - * - * This function is part of the sp_cmd_brightness_grid module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ instance); - -/** - *Deallocates a [`BrightnessGridCommand`] instance. - * - * This function is part of the sp_cmd_brightness_grid module. - */ -void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ instance); - /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. @@ -1559,18 +1531,18 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ size_t origin_y); /** - *Clones a [`CharGridCommand`] instance. + *Clones a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the sp_cmd_brightnessgrid module. */ -struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ instance); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** - *Deallocates a [`CharGridCommand`] instance. + *Deallocates a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the sp_cmd_brightnessgrid module. */ -void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance); +void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], @@ -1639,6 +1611,27 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, */ struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); +/** + *Clones a [`CharGridCommand`] instance. + * + * This function is part of the sp_cmd_chargrid module. + */ +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); + +/** + *Deallocates a [`CharGridCommand`] instance. + * + * This function is part of the sp_cmd_chargrid module. + */ +void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); + +/** + *Clones a [`ClearCommand`] instance. + * + * This function is part of the sp_cmd_clear module. + */ +struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); + /** *Deallocates a [`ClearCommand`] instance. * @@ -1657,20 +1650,6 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); */ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); -/** - *Clones a [`Cp437GridCommand`] instance. - * - * This function is part of the sp_cmd_cp437_grid module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ instance); - -/** - *Deallocates a [`Cp437GridCommand`] instance. - * - * This function is part of the sp_cmd_cp437_grid module. - */ -void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance); - /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. @@ -1738,21 +1717,42 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, */ struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); +/** + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the sp_cmd_cp437grid module. + */ +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the sp_cmd_cp437grid module. + */ +void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Clones a [`FadeOutCommand`] instance. + * + * This function is part of the sp_cmd_fadeout module. + */ +struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); + /** *Deallocates a [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fade_out module. + * This function is part of the sp_cmd_fadeout module. */ -void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance); +void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); /** * A yet-to-be-tested command. * * Returns: a new [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fade_out module. + * This function is part of the sp_cmd_fadeout module. */ -struct FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); +struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); /** * Clones an [SPCommand] instance. @@ -1800,12 +1800,33 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); +/** + *Clones a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the sp_cmd_globalbrightness module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + *Deallocates a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the sp_cmd_globalbrightness module. + */ +void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + *Clones a [`HardResetCommand`] instance. + * + * This function is part of the sp_cmd_hardreset module. + */ +struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); + /** *Deallocates a [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hard_reset module. + * This function is part of the sp_cmd_hardreset module. */ -void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); +void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); /** * Kills the udp daemon on the display, which usually results in a restart. @@ -1814,9 +1835,9 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); * * Returns: a new [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hard_reset module. + * This function is part of the sp_cmd_hardreset module. */ -struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); +struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); /** *Clones a [`Cp437Grid`] instance. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 68f6408..14f7626 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_bitmap::BitmapCommand); -wrap_free!(sp_cmd_bitmap::BitmapCommand); +wrap_command!(Bitmap); wrap_fields!(sp_cmd_bitmap::BitmapCommand; prop bitmap: Bitmap { mut get(); move set(value); }; diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 6cec313..38af213 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,5 +1,6 @@ use crate::{ - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::wrap_command, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -8,8 +9,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_clone!(sp_cmd_bitvec::BitVecCommand); -wrap_free!(sp_cmd_bitvec::BitVecCommand); +wrap_command!(BitVec); wrap_fields!(sp_cmd_bitvec::BitVecCommand; prop bitvec: DisplayBitVec { mut get(); move set(value); }; diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index cc26b77..361ae8b 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_brightness_grid::BrightnessGridCommand); -wrap_free!(sp_cmd_brightness_grid::BrightnessGridCommand); +wrap_command!(BrightnessGrid); wrap_fields!(sp_cmd_brightness_grid::BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index f4162a0..f90a045 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,37 +1,45 @@ use crate::{ - macros::{wrap_free, wrap_functions}, - mem::heap_move_nonnull, + commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull, }; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use std::ptr::NonNull; macro_rules! wrap_cc_only { - ($prefix:ident :: $typ:ident ; $(#[$meta:meta])*) => { + ($(#[$meta:meta])*; $command:ident, $prefix:ident, $object_type:ident) => { + wrap_command!($command); + wrap_functions!($prefix; $(#[$meta])* /// - #[doc = concat!(" Returns: a new [`",stringify!($typ),"`] instance.")] - fn new() -> NonNull<$typ> { - heap_move_nonnull($typ) + #[doc = concat!(" Returns: a new [`",stringify!($object_type),"`] instance.")] + fn new() -> NonNull<$object_type> { + heap_move_nonnull($object_type) } ); + }; - wrap_free!($prefix :: $typ); + ($(#[$meta:meta])* $command:ident) => { + ::paste::paste!{ + wrap_cc_only!($(#[$meta])*; $command, [< sp_cmd_ $command:lower >], [< $command Command >]); + } }; } -wrap_cc_only!(sp_cmd_clear::ClearCommand; +wrap_cc_only!( /// Set all pixels to the off state. /// /// Does not affect brightness. + Clear ); -wrap_cc_only!(sp_cmd_hard_reset::HardResetCommand; +wrap_cc_only!( /// Kills the udp daemon on the display, which usually results in a restart. /// /// Please do not send this in your normal program flow. + HardReset ); -wrap_cc_only!(sp_cmd_fade_out::FadeOutCommand; +wrap_cc_only!( /// A yet-to-be-tested command. + FadeOut ); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index fe97dc0..dfab435 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_char_grid::CharGridCommand); -wrap_free!(sp_cmd_char_grid::CharGridCommand); +wrap_command!(CharGrid); wrap_fields!(sp_cmd_char_grid::CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 25294bb..cc43813 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand); -wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand); +wrap_command!(Cp437Grid); wrap_fields!(sp_cmd_cp437_grid::Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 9734d30..13dcd51 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -18,7 +18,7 @@ use std::ptr::{null_mut, NonNull}; pub union CommandUnion { pub null: *mut u8, pub bitmap: NonNull, - pub bitvec: NonNull, + pub bit_vec: NonNull, pub brightness_grid: NonNull, pub char_grid: NonNull, pub cp437_grid: NonNull, @@ -121,7 +121,7 @@ wrap_functions!(sp_cmd_generic; TypedCommand::BitVec(bitvec) => SPCommand { tag: CommandTag::BitVec, data: CommandUnion { - bitvec: heap_move_nonnull(bitvec), + bit_vec: heap_move_nonnull(bitvec), }, }, TypedCommand::HardReset(hard_reset) => SPCommand { @@ -197,7 +197,7 @@ wrap_functions!(sp_cmd_generic; CommandTag::BitVec => SPCommand { tag: CommandTag::BitVec, data: CommandUnion { - bitvec: heap_clone(command.data.bitvec), + bit_vec: heap_clone(command.data.bit_vec), }, }, CommandTag::HardReset => SPCommand { @@ -239,7 +239,7 @@ wrap_functions!(sp_cmd_generic; match command.tag { CommandTag::Invalid => (), CommandTag::Bitmap => heap_drop(command.data.bitmap), - CommandTag::BitVec => heap_drop(command.data.bitvec), + CommandTag::BitVec => heap_drop(command.data.bit_vec), CommandTag::BrightnessGrid => { heap_drop(command.data.brightness_grid) } @@ -269,7 +269,7 @@ wrap_functions!(sp_cmd_generic; heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() }) } CommandTag::BitVec => { - heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() }) + heap_move_ok(unsafe { heap_remove(command.data.bit_vec).try_into() }) } CommandTag::BrightnessGrid => heap_move_ok(unsafe { heap_remove(command.data.brightness_grid).try_into() diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 35bcbe0..7f8f47c 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,5 +1,6 @@ use crate::{ - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::wrap_command, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_remove}, }; use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; @@ -21,8 +22,7 @@ wrap_functions!(sp_cmd_brightness_global; ); -wrap_clone!(sp_cmd_brightness_global::GlobalBrightnessCommand); -wrap_free!(sp_cmd_brightness_global::GlobalBrightnessCommand); +wrap_command!(GlobalBrightness); wrap_fields!( sp_cmd_brightness_global::GlobalBrightnessCommand; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a6fa74d..6ec78cb 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -44,4 +44,35 @@ macro_rules! wrap_origin_accessors { }; } -pub(crate) use wrap_origin_accessors; +macro_rules! derive_command_from { + ($command:ident) => { + ::paste::paste! { + impl From<::servicepoint::[< $command Command >]> for $crate::commands::SPCommand { + fn from(command: ::servicepoint::[< $command Command >]) -> Self { + Self { + tag: $crate::commands::CommandTag::$command, + data: $crate::commands::CommandUnion { + [< $command:snake >]: $crate::mem::heap_move_nonnull(command) + }, + } + } + } + } + }; +} + +macro_rules! wrap_command { + ($command:ident, $prefix:ident, $object_type:ident) => { + $crate::macros::wrap_clone!($prefix::$object_type); + $crate::macros::wrap_free!($prefix::$object_type); + + $crate::commands::derive_command_from!($command); + }; + ($command:ident) => { + ::paste::paste!{ + wrap_command!($command, [< sp_cmd_ $command:lower >], [< $command Command >]); + } + }; +} + +pub(crate) use {derive_command_from, wrap_command, wrap_origin_accessors}; diff --git a/src/macros.rs b/src/macros.rs index c66808d..3b35e1d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -209,7 +209,7 @@ macro_rules! wrap_fields { macro_rules! wrap_functions { ( - $prefix:ident; + $module:ident; $( $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) @@ -221,13 +221,12 @@ macro_rules! wrap_functions { $( $(#[$meta])* #[doc = ""] - #[doc = concat!(" This function is part of the ", stringify!($prefix), " module.")] + #[doc = concat!(" This function is part of the ", stringify!($module), " module.")] #[no_mangle] - pub unsafe extern "C" fn [<$prefix _ $function>]( + pub unsafe extern "C" fn [< $module _ $function >]( $($param_name: $param_type),* ) $(-> $return_type)? $block - )+ } }; diff --git a/src/udp.rs b/src/udp.rs index a969512..7eebe27 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -79,7 +79,7 @@ wrap_functions!(sp_udp; .send_command(heap_remove(command.data.bitmap)), CommandTag::BitVec => connection .as_ref() - .send_command(heap_remove(command.data.bitvec)), + .send_command(heap_remove(command.data.bit_vec)), CommandTag::BrightnessGrid => connection .as_ref() .send_command(heap_remove(command.data.brightness_grid)), From 500cbbc872362848225f7ade10bc7deb1693e7ea Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 21 Jun 2025 23:51:10 +0200 Subject: [PATCH 03/17] add prefix centrally --- include/servicepoint.h | 290 +++++++++++----------- src/commands/bitmap_command.rs | 6 +- src/commands/bitvec_command.rs | 4 +- src/commands/brightness_grid_command.rs | 6 +- src/commands/cc_only_commands.rs | 2 +- src/commands/char_grid_command.rs | 6 +- src/commands/cp437_grid_command.rs | 6 +- src/commands/generic_command.rs | 2 +- src/commands/global_brightness_command.rs | 4 +- src/commands/mod.rs | 2 +- src/containers/bitmap.rs | 9 +- src/containers/bitvec.rs | 8 +- src/containers/brightness_grid.rs | 8 +- src/containers/char_grid.rs | 8 +- src/containers/cp437_grid.rs | 8 +- src/containers/mod.rs | 1 + src/lib.rs | 6 +- src/macros.rs | 2 +- src/packet.rs | 8 +- src/udp.rs | 4 +- 20 files changed, 195 insertions(+), 195 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 50adba6..63b2226 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -638,7 +638,7 @@ extern "C" { /** *Clones a [`Bitmap`] instance. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); @@ -649,7 +649,7 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitmap. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); @@ -662,14 +662,14 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); * * - `value`: the value to set all pixels to * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** *Deallocates a [`Bitmap`] instance. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); @@ -680,7 +680,7 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); * * Returns NULL in case of error. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); @@ -697,7 +697,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); @@ -706,14 +706,14 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); * * Gets the height in pixels. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** * Consumes the Bitmap and returns the contained BitVec. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); @@ -724,7 +724,7 @@ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); * * Returns NULL in case of an error. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, size_t x, @@ -741,7 +741,7 @@ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, * * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap *sp_bitmap_load(size_t width, size_t height, @@ -772,7 +772,7 @@ struct Bitmap *sp_bitmap_load(size_t width, * sp_bitmap_free(grid); * ``` * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap *sp_bitmap_new(size_t width, size_t height); @@ -781,7 +781,7 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); * * returns: [Bitmap] initialized to all pixels off. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); @@ -799,7 +799,7 @@ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t x, @@ -811,7 +811,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, * * Gets the width in pixels. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); @@ -822,14 +822,14 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitvec. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); /** *Clones a [`DisplayBitVec`] instance. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); @@ -842,14 +842,14 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); * * - `value`: the value to set all bits to * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); /** *Deallocates a [`DisplayBitVec`] instance. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ void sp_bitvec_free(BitVec */*notnull*/ instance); @@ -869,7 +869,7 @@ void sp_bitvec_free(BitVec */*notnull*/ instance); * * - when accessing `index` out of bounds * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); @@ -880,7 +880,7 @@ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); * * Returns NULL in case of an error. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, size_t offset, @@ -892,7 +892,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, * * Returns true if length is 0. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); @@ -901,7 +901,7 @@ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); * * Gets the length in bits. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ size_t sp_bitvec_len(BitVec */*notnull*/ instance); @@ -910,7 +910,7 @@ size_t sp_bitvec_len(BitVec */*notnull*/ instance); * * returns: [DisplayBitVec] instance containing data. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); @@ -927,7 +927,7 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); * * - when `size` is not divisible by 8. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ BitVec */*notnull*/ sp_bitvec_new(size_t size); @@ -945,14 +945,14 @@ BitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); /** *Clones a [`BrightnessGrid`] instance. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); @@ -963,7 +963,7 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ * * The returned memory is valid for the lifetime of the grid. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); @@ -976,7 +976,7 @@ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ ins * * - `value`: the value to set all cells to * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); @@ -984,7 +984,7 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, /** *Deallocates a [`BrightnessGrid`] instance. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); @@ -1002,7 +1002,7 @@ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); * # Panics * - When accessing `x` or `y` out of bounds. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, size_t x, @@ -1013,7 +1013,7 @@ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, * * Gets the height of the grid. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); @@ -1024,7 +1024,7 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, size_t x, @@ -1037,7 +1037,7 @@ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, * * returns: new [BrightnessGrid] instance, or NULL in case of an error. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ BrightnessGrid *sp_brightness_grid_load(size_t width, size_t height, @@ -1062,7 +1062,7 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, * sp_udp_free(connection); * ``` * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); @@ -1082,7 +1082,7 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); * * - When accessing `x` or `y` out of bounds. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, size_t x, @@ -1094,14 +1094,14 @@ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, * * Gets the width of the grid. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); /** *Clones a [`CharGrid`] instance. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); @@ -1115,14 +1115,14 @@ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); * - `value`: the value to set all cells to * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** *Deallocates a [`CharGrid`] instance. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ void sp_char_grid_free(CharGrid */*notnull*/ instance); @@ -1139,7 +1139,7 @@ void sp_char_grid_free(CharGrid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); @@ -1148,7 +1148,7 @@ uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); * * Gets the height of the grid. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); @@ -1159,7 +1159,7 @@ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, size_t x, @@ -1170,7 +1170,7 @@ struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, * * returns: new CharGrid or NULL in case of an error * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); @@ -1188,7 +1188,7 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); * sp_char_grid_free(grid); * ``` * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); @@ -1209,7 +1209,7 @@ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); * - when accessing `x` or `y` out of bounds * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ void sp_char_grid_set(CharGrid */*notnull*/ instance, size_t x, @@ -1221,21 +1221,21 @@ void sp_char_grid_set(CharGrid */*notnull*/ instance, * * Gets the width of the grid. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ size_t sp_char_grid_width(CharGrid */*notnull*/ instance); /** *Clones a [`BitmapCommand`] instance. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); /** *Deallocates a [`BitmapCommand`] instance. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); @@ -1245,7 +1245,7 @@ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); * * Rust equivalent: `BitmapCommand::from(bitmap)` * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); @@ -1255,21 +1255,21 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*not * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct Bitmap */*notnull*/ sp_cmd_bitmap_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); /** * Reads the origin field of the [`BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1282,7 +1282,7 @@ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, * * Returns: a new [BitmapCommand] instance. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, size_t origin_x, @@ -1293,7 +1293,7 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ b * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, struct Bitmap */*notnull*/ value); @@ -1301,7 +1301,7 @@ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, CompressionCode value); @@ -1309,7 +1309,7 @@ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_x, @@ -1320,21 +1320,21 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** *Clones a [`BitVecCommand`] instance. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); /** *Deallocates a [`BitVecCommand`] instance. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); @@ -1344,28 +1344,28 @@ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ BitVec */*notnull*/ sp_cmd_bitvec_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); @@ -1383,7 +1383,7 @@ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ in * * The contained [`DisplayBitVec`] is always uncompressed. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, size_t offset, @@ -1394,7 +1394,7 @@ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, BitVec */*notnull*/ value); @@ -1402,7 +1402,7 @@ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, CompressionCode value); @@ -1410,7 +1410,7 @@ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); @@ -1418,7 +1418,7 @@ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); @@ -1428,21 +1428,21 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); /** * Turns the command into a packet * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); @@ -1451,14 +1451,14 @@ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBri * * Returns: a new [GlobalBrightnessCommand] instance. * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, Brightness value); @@ -1467,7 +1467,7 @@ void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*n * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); @@ -1477,14 +1477,14 @@ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(Brigh * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ BrightnessGrid */*notnull*/ sp_cmd_brightness_grid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1495,7 +1495,7 @@ void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand */*notnull*/ command); @@ -1506,7 +1506,7 @@ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand * * * Returns: a new [BrightnessGridCommand] instance. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, size_t origin_x, @@ -1516,7 +1516,7 @@ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessG * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ void sp_cmd_brightness_grid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, BrightnessGrid */*notnull*/ value); @@ -1524,7 +1524,7 @@ void sp_cmd_brightness_grid_set_grid(struct BrightnessGridCommand */*notnull*/ i /** * Overwrites the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ command, size_t origin_x, @@ -1533,14 +1533,14 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ /** *Clones a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_brightnessgrid module. + * This function is part of the cmd_brightnessgrid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** *Deallocates a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_brightnessgrid module. + * This function is part of the cmd_brightnessgrid module. */ void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); @@ -1548,7 +1548,7 @@ void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instan * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); @@ -1558,14 +1558,14 @@ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnu * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ CharGrid */*notnull*/ sp_cmd_char_grid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`CharGridCommand`]. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1578,7 +1578,7 @@ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, * * Returns: a new [CharGridCommand] instance. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, size_t origin_x, @@ -1588,7 +1588,7 @@ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ g * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ void sp_cmd_char_grid_set_grid(struct CharGridCommand */*notnull*/ instance, CharGrid */*notnull*/ value); @@ -1596,7 +1596,7 @@ void sp_cmd_char_grid_set_grid(struct CharGridCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`CharGridCommand`]. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_x, @@ -1607,35 +1607,35 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** *Clones a [`CharGridCommand`] instance. * - * This function is part of the sp_cmd_chargrid module. + * This function is part of the cmd_chargrid module. */ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); /** *Deallocates a [`CharGridCommand`] instance. * - * This function is part of the sp_cmd_chargrid module. + * This function is part of the cmd_chargrid module. */ void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); /** *Clones a [`ClearCommand`] instance. * - * This function is part of the sp_cmd_clear module. + * This function is part of the cmd_clear module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); /** *Deallocates a [`ClearCommand`] instance. * - * This function is part of the sp_cmd_clear module. + * This function is part of the cmd_clear module. */ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); @@ -1646,7 +1646,7 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); * * Returns: a new [`ClearCommand`] instance. * - * This function is part of the sp_cmd_clear module. + * This function is part of the cmd_clear module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); @@ -1654,7 +1654,7 @@ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); @@ -1664,14 +1664,14 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*no * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ Cp437Grid */*notnull*/ sp_cmd_cp437_grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** * Reads the origin field of the [`Cp437GridCommand`]. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1684,7 +1684,7 @@ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, * * The origin is relative to the top-left of the display. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, size_t origin_x, @@ -1694,7 +1694,7 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull* * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ void sp_cmd_cp437_grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, Cp437Grid */*notnull*/ value); @@ -1702,7 +1702,7 @@ void sp_cmd_cp437_grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`Cp437GridCommand`]. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, size_t origin_x, @@ -1713,35 +1713,35 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** *Clones a [`Cp437GridCommand`] instance. * - * This function is part of the sp_cmd_cp437grid module. + * This function is part of the cmd_cp437grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); /** *Deallocates a [`Cp437GridCommand`] instance. * - * This function is part of the sp_cmd_cp437grid module. + * This function is part of the cmd_cp437grid module. */ void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); /** *Clones a [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fadeout module. + * This function is part of the cmd_fadeout module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); /** *Deallocates a [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fadeout module. + * This function is part of the cmd_fadeout module. */ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); @@ -1750,7 +1750,7 @@ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); * * Returns: a new [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fadeout module. + * This function is part of the cmd_fadeout module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); @@ -1759,7 +1759,7 @@ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); * * returns: a new [SPCommand] instance. * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ struct Command sp_cmd_generic_clone(struct Command command); @@ -1775,7 +1775,7 @@ struct Command sp_cmd_generic_clone(struct Command command); * sp_command_free(c); * ``` * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ void sp_cmd_generic_free(struct Command command); @@ -1785,7 +1785,7 @@ void sp_cmd_generic_free(struct Command command); * * Returns tag [CommandTag::Invalid] in case of an error. * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ struct Packet *sp_cmd_generic_into_packet(struct Command command); @@ -1796,35 +1796,35 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** *Clones a [`GlobalBrightnessCommand`] instance. * - * This function is part of the sp_cmd_globalbrightness module. + * This function is part of the cmd_globalbrightness module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Deallocates a [`GlobalBrightnessCommand`] instance. * - * This function is part of the sp_cmd_globalbrightness module. + * This function is part of the cmd_globalbrightness module. */ void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Clones a [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hardreset module. + * This function is part of the cmd_hardreset module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); /** *Deallocates a [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hardreset module. + * This function is part of the cmd_hardreset module. */ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); @@ -1835,14 +1835,14 @@ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); * * Returns: a new [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hardreset module. + * This function is part of the cmd_hardreset module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); /** *Clones a [`Cp437Grid`] instance. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); @@ -1853,7 +1853,7 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); * * The returned memory is valid for the lifetime of the instance. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); @@ -1867,14 +1867,14 @@ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** *Deallocates a [`Cp437Grid`] instance. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); @@ -1891,7 +1891,7 @@ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); @@ -1900,7 +1900,7 @@ uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); * * Gets the height of the grid. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); @@ -1911,7 +1911,7 @@ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, size_t x, @@ -1920,7 +1920,7 @@ struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ Cp437Grid *sp_cp437_grid_load(size_t width, size_t height, @@ -1931,7 +1931,7 @@ Cp437Grid *sp_cp437_grid_load(size_t width, * * returns: [Cp437Grid] initialized to 0. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); @@ -1951,7 +1951,7 @@ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, size_t x, @@ -1963,7 +1963,7 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, * * Gets the width of the grid. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); @@ -1971,21 +1971,21 @@ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). * - * This function is part of the sp_envlogger module. + * This function is part of the envlogger module. */ void sp_envlogger_init(void); /** *Clones a [`Packet`] instance. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** *Deallocates a [`Packet`] instance. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ void sp_packet_free(struct Packet */*notnull*/ instance); @@ -1994,7 +1994,7 @@ void sp_packet_free(struct Packet */*notnull*/ instance); * * returns: new instance. Will never return null. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); @@ -2002,7 +2002,7 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, /** * Gets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); @@ -2012,7 +2012,7 @@ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); @@ -2023,7 +2023,7 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ i * * The returned memory can be changed and will be valid until a new payload is set. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); @@ -2034,7 +2034,7 @@ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); * * - if the buffer is not big enough to hold header+payload. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); @@ -2042,7 +2042,7 @@ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, /** * Sets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); @@ -2052,7 +2052,7 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, * * This makes previous payload pointers invalid. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ void sp_packet_set_payload(struct Packet */*notnull*/ packet, struct ByteSlice data); @@ -2062,7 +2062,7 @@ void sp_packet_set_payload(struct Packet */*notnull*/ packet, * * returns: NULL in case of an error, pointer to the allocated packet otherwise * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Packet *sp_packet_try_load(struct ByteSlice data); @@ -2073,13 +2073,13 @@ struct Packet *sp_packet_try_load(struct ByteSlice data); * * This function is part of the sp module. */ -bool sp_u16_to_command_code(uint16_t code, - CommandCode *result); +bool sp_sp_u16_to_command_code(uint16_t code, + CommandCode *result); /** *Deallocates a [`UdpSocket`] instance. * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ void sp_udp_free(struct UdpSocket */*notnull*/ instance); @@ -2096,7 +2096,7 @@ void sp_udp_free(struct UdpSocket */*notnull*/ instance); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ struct UdpSocket *sp_udp_open(char */*notnull*/ host); @@ -2113,7 +2113,7 @@ struct UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint8_t ip2, @@ -2134,7 +2134,7 @@ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, struct Command command); @@ -2150,7 +2150,7 @@ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); @@ -2162,7 +2162,7 @@ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, * * returns: true in case of success * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, struct Packet */*notnull*/ packet); diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 14f7626..06cc53f 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -8,14 +8,14 @@ use std::ptr::NonNull; wrap_command!(Bitmap); -wrap_fields!(sp_cmd_bitmap::BitmapCommand; +wrap_fields!(cmd_bitmap::BitmapCommand; prop bitmap: Bitmap { mut get(); move set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_origin_accessors!(sp_cmd_bitmap::BitmapCommand); +wrap_origin_accessors!(cmd_bitmap::BitmapCommand); -wrap_functions!(sp_cmd_bitmap; +wrap_functions!(cmd_bitmap; /// Sets a window of pixels to the specified values. /// /// The passed [Bitmap] gets consumed. diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 38af213..100fc93 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -11,14 +11,14 @@ use std::ptr::NonNull; wrap_command!(BitVec); -wrap_fields!(sp_cmd_bitvec::BitVecCommand; +wrap_fields!(cmd_bitvec::BitVecCommand; prop bitvec: DisplayBitVec { mut get(); move set(value); }; prop offset: Offset { get(); set(value); }; prop operation: BinaryOperation { get(); set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_functions!(sp_cmd_bitvec; +wrap_functions!(cmd_bitvec; /// Set pixel data starting at the pixel offset on screen. /// diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 361ae8b..7a10389 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); -wrap_fields!(sp_cmd_brightness_grid::BrightnessGridCommand; +wrap_fields!(cmd_brightness_grid::BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(sp_cmd_brightness_grid::BrightnessGridCommand); +wrap_origin_accessors!(cmd_brightness_grid::BrightnessGridCommand); -wrap_functions!(sp_cmd_brightness_grid; +wrap_functions!(cmd_brightness_grid; /// Set the brightness of individual tiles in a rectangular area of the display. /// diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index f90a045..0d72078 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -20,7 +20,7 @@ macro_rules! wrap_cc_only { ($(#[$meta:meta])* $command:ident) => { ::paste::paste!{ - wrap_cc_only!($(#[$meta])*; $command, [< sp_cmd_ $command:lower >], [< $command Command >]); + wrap_cc_only!($(#[$meta])*; $command, [< cmd_ $command:lower >], [< $command Command >]); } }; } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index dfab435..07765b8 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(CharGrid); -wrap_fields!(sp_cmd_char_grid::CharGridCommand; +wrap_fields!(cmd_char_grid::CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(sp_cmd_char_grid::CharGridCommand); +wrap_origin_accessors!(cmd_char_grid::CharGridCommand); -wrap_functions!(sp_cmd_char_grid; +wrap_functions!(cmd_char_grid; /// Show UTF-8 encoded text on the screen. /// diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index cc43813..2e76e3c 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); -wrap_fields!(sp_cmd_cp437_grid::Cp437GridCommand; +wrap_fields!(cmd_cp437_grid::Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(sp_cmd_cp437_grid::Cp437GridCommand); +wrap_origin_accessors!(cmd_cp437_grid::Cp437GridCommand); -wrap_functions!(sp_cmd_cp437_grid; +wrap_functions!(cmd_cp437_grid; /// Show text on the screen. /// diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 13dcd51..a4775d9 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -69,7 +69,7 @@ impl SPCommand { }; } -wrap_functions!(sp_cmd_generic; +wrap_functions!(cmd_generic; /// Tries to turn a [Packet] into a [SPCommand]. /// diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 7f8f47c..6d00d7c 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; use std::ptr::NonNull; -wrap_functions!(sp_cmd_brightness_global; +wrap_functions!(cmd_brightness_global; /// Set the brightness of all tiles to the same value. /// @@ -25,7 +25,7 @@ wrap_functions!(sp_cmd_brightness_global; wrap_command!(GlobalBrightness); wrap_fields!( - sp_cmd_brightness_global::GlobalBrightnessCommand; + cmd_brightness_global::GlobalBrightnessCommand; prop brightness: Brightness { get(); set(value); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6ec78cb..0fce600 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -70,7 +70,7 @@ macro_rules! wrap_command { }; ($command:ident) => { ::paste::paste!{ - wrap_command!($command, [< sp_cmd_ $command:lower >], [< $command Command >]); + wrap_command!($command, [< cmd_ $command:lower >], [< $command Command >]); } }; } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 6d502d9..9ebf13a 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -9,10 +9,10 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_clone!(sp_bitmap::Bitmap); -wrap_free!(sp_bitmap::Bitmap); +wrap_clone!(bitmap::Bitmap); +wrap_free!(bitmap::Bitmap); -wrap_functions!(sp_bitmap; +wrap_functions!(bitmap; /// Creates a new [Bitmap] with the specified dimensions. /// @@ -106,7 +106,7 @@ wrap_functions!(sp_bitmap; ); wrap_methods!( - sp_bitmap::Bitmap; + bitmap::Bitmap; /// Gets the current value at the specified position. /// @@ -148,7 +148,6 @@ wrap_methods!( /// /// The returned memory is valid for the lifetime of the bitmap. mut fn data_ref_mut() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index e232ab5..4512ac1 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -8,7 +8,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_functions!(sp_bitvec; +wrap_functions!(bitvec; /// Creates a new [DisplayBitVec] instance. /// @@ -55,11 +55,11 @@ wrap_functions!(sp_bitvec; ); -wrap_clone!(sp_bitvec::DisplayBitVec); -wrap_free!(sp_bitvec::DisplayBitVec); +wrap_clone!(bitvec::DisplayBitVec); +wrap_free!(bitvec::DisplayBitVec); wrap_methods!( - sp_bitvec::DisplayBitVec; + bitvec::DisplayBitVec; /// Gets the value of a bit. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index e266bef..4212ec5 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::{mem::transmute, ptr::NonNull}; -wrap_functions!(sp_brightness_grid; +wrap_functions!(brightness_grid; /// Creates a new [BrightnessGrid] with the specified dimensions. /// @@ -71,11 +71,11 @@ wrap_functions!(sp_brightness_grid; ); -wrap_clone!(sp_brightness_grid::BrightnessGrid); -wrap_free!(sp_brightness_grid::BrightnessGrid); +wrap_clone!(brightness_grid::BrightnessGrid); +wrap_free!(brightness_grid::BrightnessGrid); wrap_methods!( - sp_brightness_grid::BrightnessGrid; + brightness_grid::BrightnessGrid; /// Gets the current value at the specified position. /// diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 782e2aa..2a3667b 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; -wrap_functions!(sp_char_grid; +wrap_functions!(char_grid; /// Creates a new [CharGrid] with the specified dimensions. /// @@ -58,11 +58,11 @@ wrap_functions!(sp_char_grid; ); -wrap_clone!(sp_char_grid::CharGrid); -wrap_free!(sp_char_grid::CharGrid); +wrap_clone!(char_grid::CharGrid); +wrap_free!(char_grid::CharGrid); wrap_methods!( - sp_char_grid::CharGrid; + char_grid::CharGrid; /// Returns the current value at the specified position. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 2ed9aba..a344b1a 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_functions!(sp_cp437_grid; +wrap_functions!(cp437_grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// @@ -50,11 +50,11 @@ wrap_functions!(sp_cp437_grid; ); -wrap_clone!(sp_cp437_grid::Cp437Grid); -wrap_free!(sp_cp437_grid::Cp437Grid); +wrap_clone!(cp437_grid::Cp437Grid); +wrap_free!(cp437_grid::Cp437Grid); wrap_methods!( - sp_cp437_grid::Cp437Grid; + cp437_grid::Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments diff --git a/src/containers/mod.rs b/src/containers/mod.rs index b9460e9..7576a7b 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -14,5 +14,6 @@ pub use cp437_grid::*; mod _hidden { /// This is a type only used by cbindgen to have a type for pointers. + #[allow(unused)] pub struct DisplayBitVec; } diff --git a/src/lib.rs b/src/lib.rs index 4df864c..5a1a5de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,6 @@ //! //! # Examples //! -//! Make sure to check out [this GitHub repo](https://github.com/arfst23/ServicePoint) as well! -//! //! ```C //! #include //! #include "servicepoint.h" @@ -24,6 +22,8 @@ //! return 0; //! } //! ``` +//! +//! There are more examples in the source repository. /// Functions related to commands. pub mod commands; @@ -43,7 +43,7 @@ pub const SP_FRAME_PACING_MS: u128 = 30; mod feature_env_logger { use crate::macros::wrap_functions; - wrap_functions!(sp_envlogger; + wrap_functions!(envlogger; /// Call this function at the beginning of main to enable rust logging controlled by the /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). fn init() { diff --git a/src/macros.rs b/src/macros.rs index 3b35e1d..8f96746 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -223,7 +223,7 @@ macro_rules! wrap_functions { #[doc = ""] #[doc = concat!(" This function is part of the ", stringify!($module), " module.")] #[no_mangle] - pub unsafe extern "C" fn [< $module _ $function >]( + pub unsafe extern "C" fn [< sp_ $module _ $function >]( $($param_name: $param_type),* ) $(-> $return_type)? $block diff --git a/src/packet.rs b/src/packet.rs index 4278fe0..18d63bd 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; -wrap_functions!(sp_packet; +wrap_functions!(packet; /// Tries to load a [Packet] from the passed array with the specified length. /// @@ -68,11 +68,11 @@ wrap_functions!(sp_packet; } ); -wrap_clone!(sp_packet::Packet); -wrap_free!(sp_packet::Packet); +wrap_clone!(packet::Packet); +wrap_free!(packet::Packet); wrap_fields!( - sp_packet::Packet; + packet::Packet; prop header: Header { get(); mut get(); diff --git a/src/udp.rs b/src/udp.rs index 7eebe27..fd907be 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -10,9 +10,9 @@ use std::{ ptr::NonNull, }; -wrap_free!(sp_udp::UdpSocket); +wrap_free!(udp::UdpSocket); -wrap_functions!(sp_udp; +wrap_functions!(udp; /// Creates a new instance of [UdpSocket]. /// From 8f13ba61f0d8966b164bb981f27668de12e0e22a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 00:14:06 +0200 Subject: [PATCH 04/17] remove _ from type names in function names --- include/servicepoint.h | 574 +++++++++++----------- src/commands/brightness_grid_command.rs | 6 +- src/commands/char_grid_command.rs | 6 +- src/commands/cp437_grid_command.rs | 6 +- src/commands/global_brightness_command.rs | 4 +- src/containers/brightness_grid.rs | 8 +- src/containers/char_grid.rs | 8 +- src/containers/cp437_grid.rs | 8 +- src/macros.rs | 2 +- 9 files changed, 311 insertions(+), 311 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 63b2226..0c6490f 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -638,7 +638,7 @@ extern "C" { /** *Clones a [`Bitmap`] instance. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); @@ -649,7 +649,7 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitmap. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); @@ -662,14 +662,14 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); * * - `value`: the value to set all pixels to * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** *Deallocates a [`Bitmap`] instance. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); @@ -680,7 +680,7 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); * * Returns NULL in case of error. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); @@ -697,7 +697,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); * * - when accessing `x` or `y` out of bounds * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); @@ -706,14 +706,14 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); * * Gets the height in pixels. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** * Consumes the Bitmap and returns the contained BitVec. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); @@ -724,7 +724,7 @@ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); * * Returns NULL in case of an error. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, size_t x, @@ -741,7 +741,7 @@ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, * * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap *sp_bitmap_load(size_t width, size_t height, @@ -772,7 +772,7 @@ struct Bitmap *sp_bitmap_load(size_t width, * sp_bitmap_free(grid); * ``` * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap *sp_bitmap_new(size_t width, size_t height); @@ -781,7 +781,7 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); * * returns: [Bitmap] initialized to all pixels off. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); @@ -799,7 +799,7 @@ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t x, @@ -811,7 +811,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, * * Gets the width in pixels. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); @@ -822,14 +822,14 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitvec. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); /** *Clones a [`DisplayBitVec`] instance. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); @@ -842,14 +842,14 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); * * - `value`: the value to set all bits to * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); /** *Deallocates a [`DisplayBitVec`] instance. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ void sp_bitvec_free(BitVec */*notnull*/ instance); @@ -869,7 +869,7 @@ void sp_bitvec_free(BitVec */*notnull*/ instance); * * - when accessing `index` out of bounds * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); @@ -880,7 +880,7 @@ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); * * Returns NULL in case of an error. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, size_t offset, @@ -892,7 +892,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, * * Returns true if length is 0. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); @@ -901,7 +901,7 @@ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); * * Gets the length in bits. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ size_t sp_bitvec_len(BitVec */*notnull*/ instance); @@ -910,7 +910,7 @@ size_t sp_bitvec_len(BitVec */*notnull*/ instance); * * returns: [DisplayBitVec] instance containing data. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); @@ -927,7 +927,7 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); * * - when `size` is not divisible by 8. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ BitVec */*notnull*/ sp_bitvec_new(size_t size); @@ -945,16 +945,16 @@ BitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); /** *Clones a [`BrightnessGrid`] instance. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); +BrightnessGrid */*notnull*/ sp_brightnessgrid_clone(BrightnessGrid */*notnull*/ instance); /** * Calls method [`servicepoint::BrightnessGrid::data_ref_mut`]. @@ -963,9 +963,9 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ * * The returned memory is valid for the lifetime of the grid. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); +struct ByteSlice sp_brightnessgrid_data_ref_mut(BrightnessGrid */*notnull*/ instance); /** * Calls method [`servicepoint::BrightnessGrid::fill`]. @@ -976,17 +976,17 @@ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ ins * * - `value`: the value to set all cells to * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, - Brightness value); +void sp_brightnessgrid_fill(BrightnessGrid */*notnull*/ instance, + Brightness value); /** *Deallocates a [`BrightnessGrid`] instance. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); +void sp_brightnessgrid_free(BrightnessGrid */*notnull*/ instance); /** * Calls method [`servicepoint::BrightnessGrid::get`]. @@ -1002,20 +1002,20 @@ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); * # Panics * - When accessing `x` or `y` out of bounds. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, - size_t x, - size_t y); +Brightness sp_brightnessgrid_get(BrightnessGrid */*notnull*/ instance, + size_t x, + size_t y); /** * Calls method [`servicepoint::BrightnessGrid::height`]. * * Gets the height of the grid. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); +size_t sp_brightnessgrid_height(BrightnessGrid */*notnull*/ instance); /** * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. @@ -1024,11 +1024,11 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_brightnessgrid_into_packet(BrightnessGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [BrightnessGrid] with the specified dimensions from the provided data. @@ -1037,11 +1037,11 @@ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, * * returns: new [BrightnessGrid] instance, or NULL in case of an error. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -BrightnessGrid *sp_brightness_grid_load(size_t width, - size_t height, - struct ByteSlice data); +BrightnessGrid *sp_brightnessgrid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [BrightnessGrid] with the specified dimensions. @@ -1062,9 +1062,9 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, * sp_udp_free(connection); * ``` * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); +BrightnessGrid */*notnull*/ sp_brightnessgrid_new(size_t width, size_t height); /** * Calls method [`servicepoint::BrightnessGrid::set`]. @@ -1082,28 +1082,28 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); * * - When accessing `x` or `y` out of bounds. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, - size_t x, - size_t y, - Brightness value); +void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, + size_t x, + size_t y, + Brightness value); /** * Calls method [`servicepoint::BrightnessGrid::width`]. * * Gets the width of the grid. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); +size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); /** *Clones a [`CharGrid`] instance. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); +CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); /** * Calls method [`servicepoint::CharGrid::fill`]. @@ -1115,16 +1115,16 @@ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); * - `value`: the value to set all cells to * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); +void sp_chargrid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** *Deallocates a [`CharGrid`] instance. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -void sp_char_grid_free(CharGrid */*notnull*/ instance); +void sp_chargrid_free(CharGrid */*notnull*/ instance); /** * Calls method [`servicepoint::CharGrid::get`]. @@ -1139,18 +1139,18 @@ void sp_char_grid_free(CharGrid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); +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. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -size_t sp_char_grid_height(CharGrid */*notnull*/ instance); +size_t sp_chargrid_height(CharGrid */*notnull*/ instance); /** * Creates a [CharGridCommand] and immediately turns that into a [Packet]. @@ -1159,20 +1159,20 @@ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_chargrid_into_packet(CharGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [CharGrid] with the specified dimensions from the provided data. * * returns: new CharGrid or NULL in case of an error * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); +CharGrid *sp_chargrid_load(size_t width, size_t height, struct ByteSlice data); /** * Creates a new [CharGrid] with the specified dimensions. @@ -1188,9 +1188,9 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); * sp_char_grid_free(grid); * ``` * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); +CharGrid */*notnull*/ sp_chargrid_new(size_t width, size_t height); /** * Calls method [`servicepoint::CharGrid::set`]. @@ -1209,33 +1209,33 @@ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); * - when accessing `x` or `y` out of bounds * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -void sp_char_grid_set(CharGrid */*notnull*/ instance, - size_t x, - size_t y, - uint32_t value); +void sp_chargrid_set(CharGrid */*notnull*/ instance, + size_t x, + size_t y, + uint32_t value); /** * Calls method [`servicepoint::CharGrid::width`]. * * Gets the width of the grid. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -size_t sp_char_grid_width(CharGrid */*notnull*/ instance); +size_t sp_chargrid_width(CharGrid */*notnull*/ instance); /** *Clones a [`BitmapCommand`] instance. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); /** *Deallocates a [`BitmapCommand`] instance. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); @@ -1245,7 +1245,7 @@ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); * * Rust equivalent: `BitmapCommand::from(bitmap)` * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); @@ -1255,21 +1255,21 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*not * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct Bitmap */*notnull*/ sp_cmd_bitmap_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); /** * Reads the origin field of the [`BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1282,7 +1282,7 @@ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, * * Returns: a new [BitmapCommand] instance. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, size_t origin_x, @@ -1293,7 +1293,7 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ b * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, struct Bitmap */*notnull*/ value); @@ -1301,7 +1301,7 @@ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, CompressionCode value); @@ -1309,7 +1309,7 @@ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_x, @@ -1320,21 +1320,21 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** *Clones a [`BitVecCommand`] instance. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); /** *Deallocates a [`BitVecCommand`] instance. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); @@ -1344,28 +1344,28 @@ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ BitVec */*notnull*/ sp_cmd_bitvec_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); @@ -1383,7 +1383,7 @@ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ in * * The contained [`DisplayBitVec`] is always uncompressed. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, size_t offset, @@ -1394,7 +1394,7 @@ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, BitVec */*notnull*/ value); @@ -1402,7 +1402,7 @@ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, CompressionCode value); @@ -1410,7 +1410,7 @@ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); @@ -1418,7 +1418,7 @@ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); @@ -1428,48 +1428,31 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** - * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + *Clones a [`BrightnessGridCommand`] instance. * - * This function is part of the cmd_brightness_global module. + * This function is part of the `cmd_brightnessgrid` module. */ -Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** - * Turns the command into a packet + *Deallocates a [`BrightnessGridCommand`] instance. * - * This function is part of the cmd_brightness_global module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); - -/** - * Set the brightness of all tiles to the same value. - * - * Returns: a new [GlobalBrightnessCommand] instance. - * - * This function is part of the cmd_brightness_global module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); - -/** - * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. - * - * This function is part of the cmd_brightness_global module. - */ -void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, - Brightness value); +void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_from_grid(BrightnessGrid */*notnull*/ grid); /** * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. @@ -1477,27 +1460,27 @@ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(Brigh * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -BrightnessGrid */*notnull*/ sp_cmd_brightness_grid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); +BrightnessGrid */*notnull*/ sp_cmd_brightnessgrid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); +void sp_cmd_brightnessgrid_get_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); /** * Tries to turn a [BrightnessGridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand */*notnull*/ command); +struct Packet *sp_cmd_brightnessgrid_into_packet(struct BrightnessGridCommand */*notnull*/ command); /** * Set the brightness of individual tiles in a rectangular area of the display. @@ -1506,51 +1489,51 @@ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand * * * Returns: a new [BrightnessGridCommand] instance. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -void sp_cmd_brightness_grid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, - BrightnessGrid */*notnull*/ value); +void sp_cmd_brightnessgrid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, + BrightnessGrid */*notnull*/ value); /** * Overwrites the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); +void sp_cmd_brightnessgrid_set_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); /** - *Clones a [`BrightnessGridCommand`] instance. + *Clones a [`CharGridCommand`] instance. * - * This function is part of the cmd_brightnessgrid module. + * This function is part of the `cmd_chargrid` module. */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); /** - *Deallocates a [`BrightnessGridCommand`] instance. + *Deallocates a [`CharGridCommand`] instance. * - * This function is part of the cmd_brightnessgrid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); +void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_from_grid(CharGrid */*notnull*/ grid); /** * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. @@ -1558,18 +1541,18 @@ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnu * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -CharGrid */*notnull*/ sp_cmd_char_grid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); +CharGrid */*notnull*/ sp_cmd_chargrid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`CharGridCommand`]. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); +void sp_cmd_chargrid_get_origin(struct CharGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); /** * Show UTF-8 encoded text on the screen. @@ -1578,64 +1561,50 @@ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, * * Returns: a new [CharGridCommand] instance. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_char_grid_set_grid(struct CharGridCommand */*notnull*/ instance, - CharGrid */*notnull*/ value); +void sp_cmd_chargrid_set_grid(struct CharGridCommand */*notnull*/ instance, + CharGrid */*notnull*/ value); /** * Overwrites the origin field of the [`CharGridCommand`]. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); +void sp_cmd_chargrid_set_origin(struct CharGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); /** * Tries to turn a [CharGridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); - -/** - *Clones a [`CharGridCommand`] instance. - * - * This function is part of the cmd_chargrid module. - */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); - -/** - *Deallocates a [`CharGridCommand`] instance. - * - * This function is part of the cmd_chargrid module. - */ -void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); +struct Packet *sp_cmd_chargrid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** *Clones a [`ClearCommand`] instance. * - * This function is part of the cmd_clear module. + * This function is part of the `cmd_clear` module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); /** *Deallocates a [`ClearCommand`] instance. * - * This function is part of the cmd_clear module. + * This function is part of the `cmd_clear` module. */ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); @@ -1646,17 +1615,31 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); * * Returns: a new [`ClearCommand`] instance. * - * This function is part of the cmd_clear module. + * This function is part of the `cmd_clear` module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); +/** + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the `cmd_cp437grid` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the `cmd_cp437grid` module. + */ +void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); + /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_from_grid(Cp437Grid */*notnull*/ grid); /** * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. @@ -1664,18 +1647,18 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*no * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -Cp437Grid */*notnull*/ sp_cmd_cp437_grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); +Cp437Grid */*notnull*/ sp_cmd_cp437grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** * Reads the origin field of the [`Cp437GridCommand`]. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); +void sp_cmd_cp437grid_get_origin(struct Cp437GridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); /** * Show text on the screen. @@ -1684,64 +1667,50 @@ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, * * The origin is relative to the top-left of the display. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -void sp_cmd_cp437_grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, - Cp437Grid */*notnull*/ value); +void sp_cmd_cp437grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, + Cp437Grid */*notnull*/ value); /** * Overwrites the origin field of the [`Cp437GridCommand`]. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); +void sp_cmd_cp437grid_set_origin(struct Cp437GridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); /** * Tries to turn a [Cp437GridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); - -/** - *Clones a [`Cp437GridCommand`] instance. - * - * This function is part of the cmd_cp437grid module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); - -/** - *Deallocates a [`Cp437GridCommand`] instance. - * - * This function is part of the cmd_cp437grid module. - */ -void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); +struct Packet *sp_cmd_cp437grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** *Clones a [`FadeOutCommand`] instance. * - * This function is part of the cmd_fadeout module. + * This function is part of the `cmd_fadeout` module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); /** *Deallocates a [`FadeOutCommand`] instance. * - * This function is part of the cmd_fadeout module. + * This function is part of the `cmd_fadeout` module. */ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); @@ -1750,7 +1719,7 @@ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); * * Returns: a new [`FadeOutCommand`] instance. * - * This function is part of the cmd_fadeout module. + * This function is part of the `cmd_fadeout` module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); @@ -1759,7 +1728,7 @@ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); * * returns: a new [SPCommand] instance. * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ struct Command sp_cmd_generic_clone(struct Command command); @@ -1775,7 +1744,7 @@ struct Command sp_cmd_generic_clone(struct Command command); * sp_command_free(c); * ``` * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ void sp_cmd_generic_free(struct Command command); @@ -1785,7 +1754,7 @@ void sp_cmd_generic_free(struct Command command); * * Returns tag [CommandTag::Invalid] in case of an error. * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ struct Packet *sp_cmd_generic_into_packet(struct Command command); @@ -1796,35 +1765,66 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** *Clones a [`GlobalBrightnessCommand`] instance. * - * This function is part of the cmd_globalbrightness module. + * This function is part of the `cmd_globalbrightness` module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Deallocates a [`GlobalBrightnessCommand`] instance. * - * This function is part of the cmd_globalbrightness module. + * This function is part of the `cmd_globalbrightness` module. */ void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); +/** + * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `cmd_globalbrightness` module. + */ +Brightness sp_cmd_globalbrightness_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + * Turns the command into a packet + * + * This function is part of the `cmd_globalbrightness` module. + */ +struct Packet */*notnull*/ sp_cmd_globalbrightness_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); + +/** + * Set the brightness of all tiles to the same value. + * + * Returns: a new [GlobalBrightnessCommand] instance. + * + * This function is part of the `cmd_globalbrightness` module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_new(Brightness brightness); + +/** + * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `cmd_globalbrightness` module. + */ +void sp_cmd_globalbrightness_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, + Brightness value); + /** *Clones a [`HardResetCommand`] instance. * - * This function is part of the cmd_hardreset module. + * This function is part of the `cmd_hardreset` module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); /** *Deallocates a [`HardResetCommand`] instance. * - * This function is part of the cmd_hardreset module. + * This function is part of the `cmd_hardreset` module. */ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); @@ -1835,16 +1835,16 @@ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); * * Returns: a new [`HardResetCommand`] instance. * - * This function is part of the cmd_hardreset module. + * This function is part of the `cmd_hardreset` module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); /** *Clones a [`Cp437Grid`] instance. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); +Cp437Grid */*notnull*/ sp_cp437grid_clone(Cp437Grid */*notnull*/ instance); /** * Calls method [`servicepoint::Cp437Grid::data_ref_mut`]. @@ -1853,9 +1853,9 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); * * The returned memory is valid for the lifetime of the instance. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); +struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); /** * Calls method [`servicepoint::Cp437Grid::fill`]. @@ -1867,16 +1867,16 @@ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); +void sp_cp437grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** *Deallocates a [`Cp437Grid`] instance. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); +void sp_cp437grid_free(Cp437Grid */*notnull*/ instance); /** * Calls method [`servicepoint::Cp437Grid::get`]. @@ -1891,18 +1891,18 @@ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); +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. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); +size_t sp_cp437grid_height(Cp437Grid */*notnull*/ instance); /** * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. @@ -1911,29 +1911,29 @@ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_cp437grid_into_packet(Cp437Grid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -Cp437Grid *sp_cp437_grid_load(size_t width, - size_t height, - struct ByteSlice data); +Cp437Grid *sp_cp437grid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [Cp437Grid] with the specified dimensions. * * returns: [Cp437Grid] initialized to 0. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); +Cp437Grid */*notnull*/ sp_cp437grid_new(size_t width, size_t height); /** * Calls method [`servicepoint::Cp437Grid::set`]. @@ -1951,41 +1951,41 @@ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, - size_t x, - size_t y, - uint8_t value); +void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, + size_t x, + size_t y, + uint8_t value); /** * Calls method [`servicepoint::Cp437Grid::width`]. * * Gets the width of the grid. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); +size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); /** * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). * - * This function is part of the envlogger module. + * This function is part of the `envlogger` module. */ void sp_envlogger_init(void); /** *Clones a [`Packet`] instance. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** *Deallocates a [`Packet`] instance. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ void sp_packet_free(struct Packet */*notnull*/ instance); @@ -1994,7 +1994,7 @@ void sp_packet_free(struct Packet */*notnull*/ instance); * * returns: new instance. Will never return null. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); @@ -2002,7 +2002,7 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, /** * Gets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); @@ -2012,7 +2012,7 @@ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); @@ -2023,7 +2023,7 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ i * * The returned memory can be changed and will be valid until a new payload is set. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); @@ -2034,7 +2034,7 @@ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); * * - if the buffer is not big enough to hold header+payload. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); @@ -2042,7 +2042,7 @@ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, /** * Sets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); @@ -2052,7 +2052,7 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, * * This makes previous payload pointers invalid. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ void sp_packet_set_payload(struct Packet */*notnull*/ packet, struct ByteSlice data); @@ -2062,7 +2062,7 @@ void sp_packet_set_payload(struct Packet */*notnull*/ packet, * * returns: NULL in case of an error, pointer to the allocated packet otherwise * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Packet *sp_packet_try_load(struct ByteSlice data); @@ -2071,7 +2071,7 @@ struct Packet *sp_packet_try_load(struct ByteSlice data); * * If the provided value is not valid, false is returned and result is not changed. * - * This function is part of the sp module. + * This function is part of the `sp` module. */ bool sp_sp_u16_to_command_code(uint16_t code, CommandCode *result); @@ -2079,7 +2079,7 @@ bool sp_sp_u16_to_command_code(uint16_t code, /** *Deallocates a [`UdpSocket`] instance. * - * This function is part of the udp module. + * This function is part of the `udp` module. */ void sp_udp_free(struct UdpSocket */*notnull*/ instance); @@ -2096,7 +2096,7 @@ void sp_udp_free(struct UdpSocket */*notnull*/ instance); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ struct UdpSocket *sp_udp_open(char */*notnull*/ host); @@ -2113,7 +2113,7 @@ struct UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint8_t ip2, @@ -2134,7 +2134,7 @@ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, struct Command command); @@ -2150,7 +2150,7 @@ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); @@ -2162,7 +2162,7 @@ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, * * returns: true in case of success * - * This function is part of the udp module. + * This function is part of the `udp` module. */ bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, struct Packet */*notnull*/ packet); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7a10389..b66952d 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); -wrap_fields!(cmd_brightness_grid::BrightnessGridCommand; +wrap_fields!(cmd_brightnessgrid::BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_brightness_grid::BrightnessGridCommand); +wrap_origin_accessors!(cmd_brightnessgrid::BrightnessGridCommand); -wrap_functions!(cmd_brightness_grid; +wrap_functions!(cmd_brightnessgrid; /// Set the brightness of individual tiles in a rectangular area of the display. /// diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 07765b8..f614808 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(CharGrid); -wrap_fields!(cmd_char_grid::CharGridCommand; +wrap_fields!(cmd_chargrid::CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_char_grid::CharGridCommand); +wrap_origin_accessors!(cmd_chargrid::CharGridCommand); -wrap_functions!(cmd_char_grid; +wrap_functions!(cmd_chargrid; /// Show UTF-8 encoded text on the screen. /// diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 2e76e3c..4ecd058 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); -wrap_fields!(cmd_cp437_grid::Cp437GridCommand; +wrap_fields!(cmd_cp437grid::Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_cp437_grid::Cp437GridCommand); +wrap_origin_accessors!(cmd_cp437grid::Cp437GridCommand); -wrap_functions!(cmd_cp437_grid; +wrap_functions!(cmd_cp437grid; /// Show text on the screen. /// diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 6d00d7c..1b3fb34 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; use std::ptr::NonNull; -wrap_functions!(cmd_brightness_global; +wrap_functions!(cmd_globalbrightness; /// Set the brightness of all tiles to the same value. /// @@ -25,7 +25,7 @@ wrap_functions!(cmd_brightness_global; wrap_command!(GlobalBrightness); wrap_fields!( - cmd_brightness_global::GlobalBrightnessCommand; + cmd_globalbrightness::GlobalBrightnessCommand; prop brightness: Brightness { get(); set(value); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 4212ec5..01fc010 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::{mem::transmute, ptr::NonNull}; -wrap_functions!(brightness_grid; +wrap_functions!(brightnessgrid; /// Creates a new [BrightnessGrid] with the specified dimensions. /// @@ -71,11 +71,11 @@ wrap_functions!(brightness_grid; ); -wrap_clone!(brightness_grid::BrightnessGrid); -wrap_free!(brightness_grid::BrightnessGrid); +wrap_clone!(brightnessgrid::BrightnessGrid); +wrap_free!(brightnessgrid::BrightnessGrid); wrap_methods!( - brightness_grid::BrightnessGrid; + brightnessgrid::BrightnessGrid; /// Gets the current value at the specified position. /// diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 2a3667b..a79af3a 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; -wrap_functions!(char_grid; +wrap_functions!(chargrid; /// Creates a new [CharGrid] with the specified dimensions. /// @@ -58,11 +58,11 @@ wrap_functions!(char_grid; ); -wrap_clone!(char_grid::CharGrid); -wrap_free!(char_grid::CharGrid); +wrap_clone!(chargrid::CharGrid); +wrap_free!(chargrid::CharGrid); wrap_methods!( - char_grid::CharGrid; + chargrid::CharGrid; /// Returns the current value at the specified position. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index a344b1a..a78cecf 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_functions!(cp437_grid; +wrap_functions!(cp437grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// @@ -50,11 +50,11 @@ wrap_functions!(cp437_grid; ); -wrap_clone!(cp437_grid::Cp437Grid); -wrap_free!(cp437_grid::Cp437Grid); +wrap_clone!(cp437grid::Cp437Grid); +wrap_free!(cp437grid::Cp437Grid); wrap_methods!( - cp437_grid::Cp437Grid; + cp437grid::Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments diff --git a/src/macros.rs b/src/macros.rs index 8f96746..93e06b8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -221,7 +221,7 @@ macro_rules! wrap_functions { $( $(#[$meta])* #[doc = ""] - #[doc = concat!(" This function is part of the ", stringify!($module), " module.")] + #[doc = " This function is part of the `" $module "` module."] #[no_mangle] pub unsafe extern "C" fn [< sp_ $module _ $function >]( $($param_name: $param_type),* From 8116375fd023deabf7e313eaed2d83557eb2f8ae Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:16:45 +0200 Subject: [PATCH 05/17] more type name based naming --- example/Makefile | 2 +- example/src/announce.c | 28 +- example/src/brightness_tester.c | 14 +- example/src/header_logger.c | 42 +- example/src/helpers.h | 6 +- example/src/moving_line.c | 6 +- example/src/random_stuff.c | 2 +- example/src/undefined.c | 22 + example/src/wiping_clear.c | 6 +- include/servicepoint.h | 1184 +++++++++++---------- src/commands/bitmap_command.rs | 6 +- src/commands/bitvec_command.rs | 4 +- src/commands/brightness_grid_command.rs | 6 +- src/commands/cc_only_commands.rs | 24 +- src/commands/char_grid_command.rs | 4 +- src/commands/cp437_grid_command.rs | 4 +- src/commands/global_brightness_command.rs | 5 +- src/commands/mod.rs | 60 +- src/containers/bitmap.rs | 7 +- src/containers/bitvec.rs | 7 +- src/containers/brightness_grid.rs | 7 +- src/containers/byte_slice.rs | 1 + src/containers/char_grid.rs | 7 +- src/containers/cp437_grid.rs | 7 +- src/macros.rs | 65 +- src/packet.rs | 7 +- src/udp.rs | 4 +- 27 files changed, 787 insertions(+), 750 deletions(-) create mode 100644 example/src/undefined.c diff --git a/example/Makefile b/example/Makefile index 0a56e1a..15e53d1 100644 --- a/example/Makefile +++ b/example/Makefile @@ -96,7 +96,7 @@ endif # ADD NEW EXAMPLES HERE _c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ - src/moving_line.c src/random_stuff.c src/wiping_clear.c + src/moving_line.c src/random_stuff.c src/wiping_clear.c src/undefined.c _programs := $(basename $(notdir $(_c_src))) _bins := $(_programs) _unstripped_bins := $(addsuffix _unstripped, $(_bins)) diff --git a/example/src/announce.c b/example/src/announce.c index bfdc48f..33da334 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -3,27 +3,27 @@ int main(void) { sock_init(); - sp_udp_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); + sp_udpsocket_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); - CharGrid *grid = sp_char_grid_new(5, 2); + CharGrid *grid = sp_chargrid_new(5, 2); if (grid == NULL) return 1; - sp_char_grid_set(grid, 0, 0, 'H'); - sp_char_grid_set(grid, 1, 0, 'e'); - sp_char_grid_set(grid, 2, 0, 'l'); - sp_char_grid_set(grid, 3, 0, 'l'); - sp_char_grid_set(grid, 4, 0, 'o'); - sp_char_grid_set(grid, 0, 1, 'W'); - sp_char_grid_set(grid, 1, 1, 'o'); - sp_char_grid_set(grid, 2, 1, 'r'); - sp_char_grid_set(grid, 3, 1, 'l'); - sp_char_grid_set(grid, 4, 1, 'd'); + sp_chargrid_set(grid, 0, 0, 'H'); + sp_chargrid_set(grid, 1, 0, 'e'); + sp_chargrid_set(grid, 2, 0, 'l'); + sp_chargrid_set(grid, 3, 0, 'l'); + sp_chargrid_set(grid, 4, 0, 'o'); + sp_chargrid_set(grid, 0, 1, 'W'); + sp_chargrid_set(grid, 1, 1, 'o'); + sp_chargrid_set(grid, 2, 1, 'r'); + sp_chargrid_set(grid, 3, 1, 'l'); + sp_chargrid_set(grid, 4, 1, 'd'); - Packet *packet = sp_char_grid_into_packet(grid, 0, 0); + Packet *packet = sp_chargrid_into_packet(grid, 0, 0); if (packet == NULL) return 1; - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); return 0; } diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 2257162..9d17dc7 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -5,14 +5,14 @@ void enable_all_pixels(void) { Bitmap *all_on = sp_bitmap_new_max_sized(); sp_bitmap_fill(all_on, true); - BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on); - Packet *packet = sp_cmd_bitmap_try_into_packet(bitmapCommand); + BitmapCommand *bitmapCommand = sp_bitmapcommand_from_bitmap(all_on); + Packet *packet = sp_bitmapcommand_try_into_packet(bitmapCommand); if (packet != NULL) - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); } void make_brightness_pattern(BrightnessGrid *grid) { - ByteSlice slice = sp_brightness_grid_data_ref_mut(grid); + ByteSlice slice = sp_brightnessgrid_data_ref_mut(grid); for (size_t index = 0; index < slice.length; index++) { slice.start[index] = (uint8_t)(index % ((size_t) Brightness_MAX)); } @@ -23,13 +23,13 @@ int main(void) { enable_all_pixels(); - BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT); + BrightnessGrid *grid = sp_brightnessgrid_new(TILE_WIDTH, TILE_HEIGHT); make_brightness_pattern(grid); - Packet *packet = sp_cmd_brightness_grid_into_packet(sp_cmd_brightness_grid_from_grid(grid)); + Packet *packet = sp_brightnessgridcommand_into_packet(sp_brightnessgridcommand_from_grid(grid)); if (packet == NULL) return -2; - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); return 0; } diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 5f04ac4..ec421dd 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -28,12 +28,12 @@ bool log_command(struct Command command) { case COMMAND_TAG_BITMAP: { BitmapCommand *bitmapCommand = command.data.bitmap; - CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); + CompressionCode compression = sp_bitmapcommand_get_compression(bitmapCommand); size_t x, y; - sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); + sp_bitmapcommand_get_origin(bitmapCommand, &x, &y); - Bitmap *bitmap = sp_cmd_bitmap_get_bitmap_mut(bitmapCommand); + Bitmap *bitmap = sp_bitmapcommand_get_bitmap_mut(bitmapCommand); size_t w = sp_bitmap_width(bitmap); size_t h = sp_bitmap_height(bitmap); @@ -45,11 +45,11 @@ bool log_command(struct Command command) { BrightnessGridCommand *gridCommand = command.data.brightness_grid; size_t x, y; - sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); + sp_brightnessgridcommand_get_origin(gridCommand, &x, &y); - BrightnessGrid *grid = sp_cmd_brightness_grid_get_grid_mut(gridCommand); - size_t w = sp_brightness_grid_width(grid); - size_t h = sp_brightness_grid_height(grid); + BrightnessGrid *grid = sp_brightnessgridcommand_get_grid_mut(gridCommand); + size_t w = sp_brightnessgrid_width(grid); + size_t h = sp_brightnessgrid_height(grid); printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -59,11 +59,11 @@ bool log_command(struct Command command) { CharGridCommand *gridCommand = command.data.char_grid; size_t x, y; - sp_cmd_char_grid_get_origin(gridCommand, &x, &y); + sp_chargridcommand_get_origin(gridCommand, &x, &y); - CharGrid *grid = sp_cmd_char_grid_get_grid_mut(gridCommand); - size_t w = sp_char_grid_width(grid); - size_t h = sp_char_grid_height(grid); + CharGrid *grid = sp_chargridcommand_get_grid_mut(gridCommand); + size_t w = sp_chargrid_width(grid); + size_t h = sp_chargrid_height(grid); printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -73,11 +73,11 @@ bool log_command(struct Command command) { Cp437GridCommand *gridCommand = command.data.cp437_grid; size_t x, y; - sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); + sp_cp437gridcommand_get_origin(gridCommand, &x, &y); - Cp437Grid *grid = sp_cmd_cp437_grid_get_grid_mut(gridCommand); - size_t w = sp_cp437_grid_width(grid); - size_t h = sp_cp437_grid_height(grid); + Cp437Grid *grid = sp_cp437gridcommand_get_grid_mut(gridCommand); + size_t w = sp_cp437grid_width(grid); + size_t h = sp_cp437grid_height(grid); printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -86,10 +86,10 @@ bool log_command(struct Command command) { case COMMAND_TAG_BIT_VEC: { BitVecCommand *bitvecCommand = command.data.bit_vec; - size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); - CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); + size_t offset = sp_bitveccommand_get_offset(bitvecCommand); + CompressionCode compression = sp_bitveccommand_get_compression(bitvecCommand); - BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); + BinaryOperation operation = sp_bitveccommand_get_operation(bitvecCommand); char *operationText; switch (operation) { case BINARY_OPERATION_AND: @@ -109,8 +109,8 @@ bool log_command(struct Command command) { break; } - BitVec *bitvec = sp_cmd_bitvec_get_bitvec_mut(bitvecCommand); - size_t len = sp_bitvec_len(bitvec); + BitVec *bitvec = sp_bitveccommand_get_bitvec_mut(bitvecCommand); + size_t len = sp_displaybitvec_len(bitvec); printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", offset, len, compression, operationText); @@ -129,7 +129,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_cmd_brightness_global_get_brightness(command.data.global_brightness); + Brightness brightness = sp_globalbrightnesscommand_get_brightness(command.data.global_brightness); printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); break; } diff --git a/example/src/helpers.h b/example/src/helpers.h index bc6c119..ee415f2 100644 --- a/example/src/helpers.h +++ b/example/src/helpers.h @@ -10,12 +10,12 @@ static UdpSocket *sock = NULL; void sock_free() { - sp_udp_free(sock); + sp_udpsocket_free(sock); } void sock_init() { - //sock = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + sock = sp_udpsocket_open_ipv4(127, 0, 0, 1, 2342); + //sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342); if (sock == NULL) exit(-1); atexit(sock_free); diff --git a/example/src/moving_line.c b/example/src/moving_line.c index 35ba20d..12a1a9b 100644 --- a/example/src/moving_line.c +++ b/example/src/moving_line.c @@ -13,14 +13,14 @@ int main(void) { sp_bitmap_set(bitmap, (y + x) % PIXEL_WIDTH, y, true); } - BitmapCommand *command = sp_cmd_bitmap_from_bitmap(sp_bitmap_clone(bitmap)); - Packet *packet = sp_cmd_bitmap_try_into_packet(command); + BitmapCommand *command = sp_bitmapcommand_from_bitmap(sp_bitmap_clone(bitmap)); + Packet *packet = sp_bitmapcommand_try_into_packet(command); if (packet == NULL) { result = -2; goto exit; } - if (!sp_udp_send_packet(sock, packet)) { + if (!sp_udpsocket_send_packet(sock, packet)) { result = -3; goto exit; } diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index b55e42c..8838123 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -18,6 +18,6 @@ int main(void) { Header *header = sp_packet_get_header_mut(packet); printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d); - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); return 0; } diff --git a/example/src/undefined.c b/example/src/undefined.c new file mode 100644 index 0000000..44aaf6d --- /dev/null +++ b/example/src/undefined.c @@ -0,0 +1,22 @@ +#include +#include "servicepoint.h" +#include "helpers.h" + +/// DO NOT DO ANY OF THIS! +int main(void) { + BitmapCommand *bmcmd = sp_bitmapcommand_new(sp_bitmap_new_max_sized(), 0, 0, COMPRESSION_CODE_UNCOMPRESSED); + BitVecCommand *bvcmd = (BitVecCommand *) bmcmd; + sp_bitveccommand_free(bvcmd); + + uint8_t *data = calloc(1024, 1); + struct Command generic = { + .tag = COMMAND_TAG_BRIGHTNESS_GRID, + .data = {.null = data}, + }; + + sock_init(); + + sp_udpsocket_send_command(sock, generic); + + return 0; +} diff --git a/example/src/wiping_clear.c b/example/src/wiping_clear.c index a3f7017..3984169 100644 --- a/example/src/wiping_clear.c +++ b/example/src/wiping_clear.c @@ -14,13 +14,13 @@ int main() { } BitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); - BitVecCommand *command = sp_cmd_bitvec_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); - Packet *packet = sp_cmd_bitvec_try_into_packet(command); + BitVecCommand *command = sp_bitveccommand_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); + Packet *packet = sp_bitveccommand_try_into_packet(command); if (packet == NULL) { result = -2; goto exit; } - if (!sp_udp_send_packet(sock, packet)) { + if (!sp_udpsocket_send_packet(sock, packet)) { result = -3; goto exit; } diff --git a/include/servicepoint.h b/include/servicepoint.h index 0c6490f..e0e6cc4 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -476,6 +476,11 @@ typedef struct ByteSlice { size_t length; } ByteSlice; +/** + * Type alias for documenting the meaning of the u16 in enum values + */ +typedef size_t Offset; + /** * A grid containing brightness values. * @@ -542,11 +547,6 @@ typedef uint8_t Brightness; */ typedef struct ValueGrid_char CharGrid; -/** - * Type alias for documenting the meaning of the u16 in enum values - */ -typedef size_t Offset; - /** * A grid containing codepage 437 characters. * @@ -816,62 +816,103 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. + *Clones a [`BitmapCommand`] instance. * - * Gets an unsafe reference to the data of the [DisplayBitVec] instance. - * - * The returned memory is valid for the lifetime of the bitvec. - * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); +struct BitmapCommand */*notnull*/ sp_bitmapcommand_clone(struct BitmapCommand */*notnull*/ instance); /** - *Clones a [`DisplayBitVec`] instance. + *Deallocates a [`BitmapCommand`] instance. * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); +void sp_bitmapcommand_free(struct BitmapCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::fill`]. + * Move the provided [Bitmap] into a new [BitmapCommand], + * leaving other fields as their default values. * - * Sets the value of all bits. + * Rust equivalent: `BitmapCommand::from(bitmap)` * - * # Arguments - * - * - `value`: the value to set all bits to - * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); +struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** - *Deallocates a [`DisplayBitVec`] instance. + * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the `bitvec` module. + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `bitmapcommand` module. */ -void sp_bitvec_free(BitVec */*notnull*/ instance); +struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::get`]. + * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * Gets the value of a bit. - * - * # Arguments - * - * - `bit_vec`: instance to read from - * - `index`: the bit index to read - * - * returns: value of the bit - * - * # Panics - * - * - when accessing `index` out of bounds - * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); +CompressionCode sp_bitmapcommand_get_compression(struct BitmapCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`BitmapCommand`]. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_get_origin(struct BitmapCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Sets a window of pixels to the specified values. + * + * The passed [Bitmap] gets consumed. + * + * Returns: a new [BitmapCommand] instance. + * + * This function is part of the `bitmapcommand` module. + */ +struct BitmapCommand */*notnull*/ sp_bitmapcommand_new(struct Bitmap */*notnull*/ bitmap, + size_t origin_x, + size_t origin_y, + CompressionCode compression); + +/** + * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_set_bitmap(struct BitmapCommand */*notnull*/ instance, + struct Bitmap */*notnull*/ value); + +/** + * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_set_compression(struct BitmapCommand */*notnull*/ instance, + CompressionCode value); + +/** + * Overwrites the origin field of the [`BitmapCommand`]. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + * Tries to turn a [BitmapCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `bitmapcommand` module. + */ +struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ command); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -887,24 +928,6 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, BinaryOperation operation, CompressionCode compression); -/** - * Calls method [`servicepoint::DisplayBitVec::is_empty`]. - * - * Returns true if length is 0. - * - * This function is part of the `bitvec` module. - */ -bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); - -/** - * Calls method [`servicepoint::DisplayBitVec::len`]. - * - * Gets the length in bits. - * - * This function is part of the `bitvec` module. - */ -size_t sp_bitvec_len(BitVec */*notnull*/ instance); - /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. * @@ -932,22 +955,112 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); BitVec */*notnull*/ sp_bitvec_new(size_t size); /** - * Calls method [`servicepoint::DisplayBitVec::set`]. + *Clones a [`BitVecCommand`] instance. * - * Sets the value of a bit. - * - * # Arguments - * - * - `index`: the bit index to edit - * - `value`: the value to set the bit to - * - * # Panics - * - * - when accessing `index` out of bounds - * - * This function is part of the `bitvec` module. + * This function is part of the `bitveccommand` module. */ -void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); +struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */*notnull*/ instance); + +/** + *Deallocates a [`BitVecCommand`] instance. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `bitveccommand` module. + */ +BitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +Offset sp_bitveccommand_get_offset(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +BinaryOperation sp_bitveccommand_get_operation(struct BitVecCommand */*notnull*/ instance); + +/** + * Set pixel data starting at the pixel offset on screen. + * + * The screen will continuously overwrite more pixel data without regarding the offset, meaning + * once the starting row is full, overwriting will continue on column 0. + * + * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. + * + * `new_bit = old_bit op sent_bit` + * + * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. + * + * The contained [`DisplayBitVec`] is always uncompressed. + * + * This function is part of the `bitveccommand` module. + */ +struct BitVecCommand */*notnull*/ sp_bitveccommand_new(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); + +/** + * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, + BitVec */*notnull*/ value); + +/** + * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_compression(struct BitVecCommand */*notnull*/ instance, + CompressionCode value); + +/** + * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_offset(struct BitVecCommand */*notnull*/ instance, + Offset value); + +/** + * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, + BinaryOperation value); + +/** + * Tries to turn a [BitVecCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `bitveccommand` module. + */ +struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ command); /** *Clones a [`BrightnessGrid`] instance. @@ -1098,6 +1211,87 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, */ size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); +/** + *Clones a [`BrightnessGridCommand`] instance. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_clone(struct BrightnessGridCommand */*notnull*/ instance); + +/** + *Deallocates a [`BrightnessGridCommand`] instance. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_free(struct BrightnessGridCommand */*notnull*/ instance); + +/** + * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], + * leaving other fields as their default values. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(BrightnessGrid */*notnull*/ grid); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `brightnessgridcommand` module. + */ +BrightnessGrid */*notnull*/ sp_brightnessgridcommand_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_get_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Tries to turn a [BrightnessGridCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct Packet *sp_brightnessgridcommand_into_packet(struct BrightnessGridCommand */*notnull*/ command); + +/** + * Set the brightness of individual tiles in a rectangular area of the display. + * + * The passed [BrightnessGrid] gets consumed. + * + * Returns: a new [BrightnessGridCommand] instance. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_set_grid(struct BrightnessGridCommand */*notnull*/ instance, + BrightnessGrid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + /** *Clones a [`CharGrid`] instance. * @@ -1225,307 +1419,81 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, */ size_t sp_chargrid_width(CharGrid */*notnull*/ instance); -/** - *Clones a [`BitmapCommand`] instance. - * - * This function is part of the `cmd_bitmap` module. - */ -struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); - -/** - *Deallocates a [`BitmapCommand`] instance. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); - -/** - * Move the provided [Bitmap] into a new [BitmapCommand], - * leaving other fields as their default values. - * - * Rust equivalent: `BitmapCommand::from(bitmap)` - * - * This function is part of the `cmd_bitmap` module. - */ -struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); - -/** - * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_bitmap` module. - */ -struct Bitmap */*notnull*/ sp_cmd_bitmap_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); - -/** - * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -/** - * Sets a window of pixels to the specified values. - * - * The passed [Bitmap] gets consumed. - * - * Returns: a new [BitmapCommand] instance. - * - * This function is part of the `cmd_bitmap` module. - */ -struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, - size_t origin_x, - size_t origin_y, - CompressionCode compression); - -/** - * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, - struct Bitmap */*notnull*/ value); - -/** - * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, - CompressionCode value); - -/** - * Overwrites the origin field of the [`BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - -/** - * Tries to turn a [BitmapCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_bitmap` module. - */ -struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); - -/** - *Clones a [`BitVecCommand`] instance. - * - * This function is part of the `cmd_bitvec` module. - */ -struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); - -/** - *Deallocates a [`BitVecCommand`] instance. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_bitvec` module. - */ -BitVec */*notnull*/ sp_cmd_bitvec_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); - -/** - * Set pixel data starting at the pixel offset on screen. - * - * The screen will continuously overwrite more pixel data without regarding the offset, meaning - * once the starting row is full, overwriting will continue on column 0. - * - * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. - * - * `new_bit = old_bit op sent_bit` - * - * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. - * - * The contained [`DisplayBitVec`] is always uncompressed. - * - * This function is part of the `cmd_bitvec` module. - */ -struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); - -/** - * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, - BitVec */*notnull*/ value); - -/** - * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, - CompressionCode value); - -/** - * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, - Offset value); - -/** - * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, - BinaryOperation value); - -/** - * Tries to turn a [BitVecCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_bitvec` module. - */ -struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); - -/** - *Clones a [`BrightnessGridCommand`] instance. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); - -/** - *Deallocates a [`BrightnessGridCommand`] instance. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); - -/** - * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], - * leaving other fields as their default values. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_from_grid(BrightnessGrid */*notnull*/ grid); - -/** - * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -BrightnessGrid */*notnull*/ sp_cmd_brightnessgrid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`BrightnessGridCommand`]. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_get_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -/** - * Tries to turn a [BrightnessGridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct Packet *sp_cmd_brightnessgrid_into_packet(struct BrightnessGridCommand */*notnull*/ command); - -/** - * Set the brightness of individual tiles in a rectangular area of the display. - * - * The passed [BrightnessGrid] gets consumed. - * - * Returns: a new [BrightnessGridCommand] instance. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_new(BrightnessGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - -/** - * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, - BrightnessGrid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`BrightnessGridCommand`]. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_set_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - /** *Clones a [`CharGridCommand`] instance. * - * This function is part of the `cmd_chargrid` module. + * This function is part of the `chargridcommand` module. */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); +struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridCommand */*notnull*/ instance); /** *Deallocates a [`CharGridCommand`] instance. * - * This function is part of the `cmd_chargrid` module. + * This function is part of the `chargridcommand` module. */ -void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); +void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `chargridcommand` module. + */ +CharGrid */*notnull*/ sp_chargridcommand_get_grid_mut(struct CharGridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`CharGridCommand`]. + * + * This function is part of the `chargridcommand` module. + */ +void sp_chargridcommand_get_origin(struct CharGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `chargridcommand` module. + */ +void sp_chargridcommand_set_grid(struct CharGridCommand */*notnull*/ instance, + CharGrid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`CharGridCommand`]. + * + * This function is part of the `chargridcommand` module. + */ +void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + *Clones a [`ClearCommand`] instance. + * + * This function is part of the `clearcommand` module. + */ +struct ClearCommand */*notnull*/ sp_clearcommand_clone(struct ClearCommand */*notnull*/ instance); + +/** + *Deallocates a [`ClearCommand`] instance. + * + * This function is part of the `clearcommand` module. + */ +void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); + +/** + * Set all pixels to the off state. + * + * Does not affect brightness. + * + * Returns: a new [`ClearCommand`] instance. + * + * This function is part of the `clearcommand` module. + */ +struct ClearCommand */*notnull*/ sp_clearcommand_new(void); /** * Moves the provided [CharGrid] into a new [CharGridCommand], @@ -1535,25 +1503,6 @@ void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); */ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_from_grid(CharGrid */*notnull*/ grid); -/** - * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_chargrid` module. - */ -CharGrid */*notnull*/ sp_cmd_chargrid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`CharGridCommand`]. - * - * This function is part of the `cmd_chargrid` module. - */ -void sp_cmd_chargrid_get_origin(struct CharGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - /** * Show UTF-8 encoded text on the screen. * @@ -1567,24 +1516,6 @@ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ gr size_t origin_x, size_t origin_y); -/** - * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_chargrid` module. - */ -void sp_cmd_chargrid_set_grid(struct CharGridCommand */*notnull*/ instance, - CharGrid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`CharGridCommand`]. - * - * This function is part of the `cmd_chargrid` module. - */ -void sp_cmd_chargrid_set_origin(struct CharGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - /** * Tries to turn a [CharGridCommand] into a [Packet]. * @@ -1594,45 +1525,6 @@ void sp_cmd_chargrid_set_origin(struct CharGridCommand */*notnull*/ command, */ struct Packet *sp_cmd_chargrid_try_into_packet(struct CharGridCommand */*notnull*/ command); -/** - *Clones a [`ClearCommand`] instance. - * - * This function is part of the `cmd_clear` module. - */ -struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); - -/** - *Deallocates a [`ClearCommand`] instance. - * - * This function is part of the `cmd_clear` module. - */ -void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); - -/** - * Set all pixels to the off state. - * - * Does not affect brightness. - * - * Returns: a new [`ClearCommand`] instance. - * - * This function is part of the `cmd_clear` module. - */ -struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); - -/** - *Clones a [`Cp437GridCommand`] instance. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); - -/** - *Deallocates a [`Cp437GridCommand`] instance. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); - /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. @@ -1641,25 +1533,6 @@ void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_from_grid(Cp437Grid */*notnull*/ grid); -/** - * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_cp437grid` module. - */ -Cp437Grid */*notnull*/ sp_cmd_cp437grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`Cp437GridCommand`]. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_get_origin(struct Cp437GridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - /** * Show text on the screen. * @@ -1673,24 +1546,6 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ size_t origin_x, size_t origin_y); -/** - * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, - Cp437Grid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`Cp437GridCommand`]. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_set_origin(struct Cp437GridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - /** * Tries to turn a [Cp437GridCommand] into a [Packet]. * @@ -1700,29 +1555,6 @@ void sp_cmd_cp437grid_set_origin(struct Cp437GridCommand */*notnull*/ command, */ struct Packet *sp_cmd_cp437grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); -/** - *Clones a [`FadeOutCommand`] instance. - * - * This function is part of the `cmd_fadeout` module. - */ -struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); - -/** - *Deallocates a [`FadeOutCommand`] instance. - * - * This function is part of the `cmd_fadeout` module. - */ -void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); - -/** - * A yet-to-be-tested command. - * - * Returns: a new [`FadeOutCommand`] instance. - * - * This function is part of the `cmd_fadeout` module. - */ -struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); - /** * Clones an [SPCommand] instance. * @@ -1769,76 +1601,6 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); -/** - *Clones a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the `cmd_globalbrightness` module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - *Deallocates a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the `cmd_globalbrightness` module. - */ -void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. - * - * This function is part of the `cmd_globalbrightness` module. - */ -Brightness sp_cmd_globalbrightness_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - * Turns the command into a packet - * - * This function is part of the `cmd_globalbrightness` module. - */ -struct Packet */*notnull*/ sp_cmd_globalbrightness_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); - -/** - * Set the brightness of all tiles to the same value. - * - * Returns: a new [GlobalBrightnessCommand] instance. - * - * This function is part of the `cmd_globalbrightness` module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_new(Brightness brightness); - -/** - * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. - * - * This function is part of the `cmd_globalbrightness` module. - */ -void sp_cmd_globalbrightness_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, - Brightness value); - -/** - *Clones a [`HardResetCommand`] instance. - * - * This function is part of the `cmd_hardreset` module. - */ -struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); - -/** - *Deallocates a [`HardResetCommand`] instance. - * - * This function is part of the `cmd_hardreset` module. - */ -void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); - -/** - * Kills the udp daemon on the display, which usually results in a restart. - * - * Please do not send this in your normal program flow. - * - * Returns: a new [`HardResetCommand`] instance. - * - * This function is part of the `cmd_hardreset` module. - */ -struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); - /** *Clones a [`Cp437Grid`] instance. * @@ -1967,6 +1729,153 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, */ size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); +/** + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `cp437gridcommand` module. + */ +Cp437Grid */*notnull*/ sp_cp437gridcommand_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`Cp437GridCommand`]. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_get_origin(struct Cp437GridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_set_grid(struct Cp437GridCommand */*notnull*/ instance, + Cp437Grid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`Cp437GridCommand`]. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. + * + * Gets an unsafe reference to the data of the [DisplayBitVec] instance. + * + * The returned memory is valid for the lifetime of the bitvec. + * + * This function is part of the `displaybitvec` module. + */ +struct ByteSlice sp_displaybitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); + +/** + *Clones a [`DisplayBitVec`] instance. + * + * This function is part of the `displaybitvec` module. + */ +BitVec */*notnull*/ sp_displaybitvec_clone(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::fill`]. + * + * Sets the value of all bits. + * + * # Arguments + * + * - `value`: the value to set all bits to + * + * This function is part of the `displaybitvec` module. + */ +void sp_displaybitvec_fill(BitVec */*notnull*/ instance, bool value); + +/** + *Deallocates a [`DisplayBitVec`] instance. + * + * This function is part of the `displaybitvec` module. + */ +void sp_displaybitvec_free(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::get`]. + * + * Gets the value of a bit. + * + * # Arguments + * + * - `bit_vec`: instance to read from + * - `index`: the bit index to read + * + * returns: value of the bit + * + * # Panics + * + * - when accessing `index` out of bounds + * + * This function is part of the `displaybitvec` module. + */ +bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); + +/** + * Calls method [`servicepoint::DisplayBitVec::is_empty`]. + * + * Returns true if length is 0. + * + * This function is part of the `displaybitvec` module. + */ +bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::len`]. + * + * Gets the length in bits. + * + * This function is part of the `displaybitvec` module. + */ +size_t sp_displaybitvec_len(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::set`]. + * + * Sets the value of a bit. + * + * # Arguments + * + * - `index`: the bit index to edit + * - `value`: the value to set the bit to + * + * # Panics + * + * - when accessing `index` out of bounds + * + * This function is part of the `displaybitvec` module. + */ +void sp_displaybitvec_set(BitVec */*notnull*/ instance, + size_t index, + bool value); + /** * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). @@ -1975,6 +1884,99 @@ size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); */ void sp_envlogger_init(void); +/** + *Clones a [`FadeOutCommand`] instance. + * + * This function is part of the `fadeoutcommand` module. + */ +struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_clone(struct FadeOutCommand */*notnull*/ instance); + +/** + *Deallocates a [`FadeOutCommand`] instance. + * + * This function is part of the `fadeoutcommand` module. + */ +void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); + +/** + * A yet-to-be-tested command. + * + * Returns: a new [`FadeOutCommand`] instance. + * + * This function is part of the `fadeoutcommand` module. + */ +struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); + +/** + *Clones a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + *Deallocates a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the `globalbrightnesscommand` module. + */ +void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `globalbrightnesscommand` module. + */ +Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + * Turns the command into a packet + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct Packet */*notnull*/ sp_globalbrightnesscommand_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); + +/** + * Set the brightness of all tiles to the same value. + * + * Returns: a new [GlobalBrightnessCommand] instance. + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brightness brightness); + +/** + * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `globalbrightnesscommand` module. + */ +void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, + Brightness value); + +/** + *Clones a [`HardResetCommand`] instance. + * + * This function is part of the `hardresetcommand` module. + */ +struct HardResetCommand */*notnull*/ sp_hardresetcommand_clone(struct HardResetCommand */*notnull*/ instance); + +/** + *Deallocates a [`HardResetCommand`] instance. + * + * This function is part of the `hardresetcommand` module. + */ +void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); + +/** + * Kills the udp daemon on the display, which usually results in a restart. + * + * Please do not send this in your normal program flow. + * + * Returns: a new [`HardResetCommand`] instance. + * + * This function is part of the `hardresetcommand` module. + */ +struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); + /** *Clones a [`Packet`] instance. * @@ -2079,9 +2081,9 @@ bool sp_sp_u16_to_command_code(uint16_t code, /** *Deallocates a [`UdpSocket`] instance. * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -void sp_udp_free(struct UdpSocket */*notnull*/ instance); +void sp_udpsocket_free(struct UdpSocket */*notnull*/ instance); /** * Creates a new instance of [UdpSocket]. @@ -2096,9 +2098,9 @@ void sp_udp_free(struct UdpSocket */*notnull*/ instance); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -struct UdpSocket *sp_udp_open(char */*notnull*/ host); +struct UdpSocket *sp_udpsocket_open(char */*notnull*/ host); /** * Creates a new instance of [UdpSocket]. @@ -2113,13 +2115,13 @@ struct UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, - uint8_t ip2, - uint8_t ip3, - uint8_t ip4, - uint16_t port); +struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, + uint8_t ip2, + uint8_t ip3, + uint8_t ip4, + uint16_t port); /** * Sends a [SPCommand] to the display using the [UdpSocket]. @@ -2134,10 +2136,10 @@ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, - struct Command command); +bool sp_udpsocket_send_command(struct UdpSocket */*notnull*/ connection, + struct Command command); /** * Sends a [Header] to the display using the [UdpSocket]. @@ -2150,10 +2152,10 @@ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, - struct Header header); +bool sp_udpsocket_send_header(struct UdpSocket */*notnull*/ udp_connection, + struct Header header); /** * Sends a [Packet] to the display using the [UdpSocket]. @@ -2162,10 +2164,10 @@ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, * * returns: true in case of success * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, - struct Packet */*notnull*/ packet); +bool sp_udpsocket_send_packet(struct UdpSocket */*notnull*/ connection, + struct Packet */*notnull*/ packet); #ifdef __cplusplus } // extern "C" diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 06cc53f..6fa5357 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -8,14 +8,14 @@ use std::ptr::NonNull; wrap_command!(Bitmap); -wrap_fields!(cmd_bitmap::BitmapCommand; +wrap_fields!(BitmapCommand; prop bitmap: Bitmap { mut get(); move set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_origin_accessors!(cmd_bitmap::BitmapCommand); +wrap_origin_accessors!(BitmapCommand); -wrap_functions!(cmd_bitmap; +wrap_functions!(associate BitmapCommand; /// Sets a window of pixels to the specified values. /// /// The passed [Bitmap] gets consumed. diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 100fc93..fab6a2d 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -11,14 +11,14 @@ use std::ptr::NonNull; wrap_command!(BitVec); -wrap_fields!(cmd_bitvec::BitVecCommand; +wrap_fields!(BitVecCommand; prop bitvec: DisplayBitVec { mut get(); move set(value); }; prop offset: Offset { get(); set(value); }; prop operation: BinaryOperation { get(); set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_functions!(cmd_bitvec; +wrap_functions!(associate BitVecCommand; /// Set pixel data starting at the pixel offset on screen. /// diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index b66952d..8aed3d7 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); -wrap_fields!(cmd_brightnessgrid::BrightnessGridCommand; +wrap_fields!(BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_brightnessgrid::BrightnessGridCommand); +wrap_origin_accessors!(BrightnessGridCommand); -wrap_functions!(cmd_brightnessgrid; +wrap_functions!(associate BrightnessGridCommand; /// Set the brightness of individual tiles in a rectangular area of the display. /// diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 0d72078..a34e5c8 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -5,22 +5,18 @@ use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use std::ptr::NonNull; macro_rules! wrap_cc_only { - ($(#[$meta:meta])*; $command:ident, $prefix:ident, $object_type:ident) => { - wrap_command!($command); - - wrap_functions!($prefix; - $(#[$meta])* - /// - #[doc = concat!(" Returns: a new [`",stringify!($object_type),"`] instance.")] - fn new() -> NonNull<$object_type> { - heap_move_nonnull($object_type) - } - ); - }; - ($(#[$meta:meta])* $command:ident) => { ::paste::paste!{ - wrap_cc_only!($(#[$meta])*; $command, [< cmd_ $command:lower >], [< $command Command >]); + wrap_command!($command); + + wrap_functions!(associate [< $command Command >]; + $(#[$meta])* + /// + #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] + fn new() -> NonNull<[< $command Command >]> { + heap_move_nonnull([< $command Command >]) + } + ); } }; } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index f614808..551533e 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -8,11 +8,11 @@ use std::ptr::NonNull; wrap_command!(CharGrid); -wrap_fields!(cmd_chargrid::CharGridCommand; +wrap_fields!(CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_chargrid::CharGridCommand); +wrap_origin_accessors!(CharGridCommand); wrap_functions!(cmd_chargrid; diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 4ecd058..913c393 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -8,11 +8,11 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); -wrap_fields!(cmd_cp437grid::Cp437GridCommand; +wrap_fields!(Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_cp437grid::Cp437GridCommand); +wrap_origin_accessors!(Cp437GridCommand); wrap_functions!(cmd_cp437grid; diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 1b3fb34..247ef55 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; use std::ptr::NonNull; -wrap_functions!(cmd_globalbrightness; +wrap_functions!(associate GlobalBrightnessCommand; /// Set the brightness of all tiles to the same value. /// @@ -24,8 +24,7 @@ wrap_functions!(cmd_globalbrightness; wrap_command!(GlobalBrightness); -wrap_fields!( - cmd_globalbrightness::GlobalBrightnessCommand; +wrap_fields!(GlobalBrightnessCommand; prop brightness: Brightness { get(); set(value); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0fce600..a00ab89 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -15,32 +15,35 @@ pub use cp437_grid_command::*; pub use generic_command::*; macro_rules! wrap_origin_accessors { - ( $prefix:ident :: $object_type:ty ) => { - $crate::macros::wrap_functions!($prefix; - #[doc = concat!(" Reads the origin field of the [`", stringify!($object_type), "`].")] - fn get_origin( - command: NonNull<$object_type>, - origin_x: NonNull, - origin_y: NonNull, - ) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; + ( $object_type:ident ) => { + ::paste::paste! { + $crate::macros::wrap_functions!(associate $object_type; + #[doc = " Reads the origin field of the [`" $object_type "`]."] + fn get_origin( + command: NonNull<$object_type>, + origin_x: NonNull, + origin_y: NonNull, + ) { + unsafe { + let origin = &command.as_ref().origin; + *origin_x.as_ptr() = origin.x; + *origin_y.as_ptr() = origin.y; + } } - } - #[doc = concat!(" Overwrites the origin field of the [`", stringify!($object_type), "`].")] - fn set_origin( - command: NonNull<$object_type>, - origin_x: usize, - origin_y: usize, - ) { - unsafe { - $crate::macros::nonnull_as_mut!(command).origin = ::servicepoint::Origin::new(origin_x, origin_y); + #[doc = " Overwrites the origin field of the [`" $object_type "`]."] + fn set_origin( + command: NonNull<$object_type>, + origin_x: usize, + origin_y: usize, + ) { + unsafe { + $crate::macros::nonnull_as_mut!(command).origin = + ::servicepoint::Origin::new(origin_x, origin_y); + } } - } - ); + ); + } }; } @@ -62,15 +65,14 @@ macro_rules! derive_command_from { } macro_rules! wrap_command { - ($command:ident, $prefix:ident, $object_type:ident) => { - $crate::macros::wrap_clone!($prefix::$object_type); - $crate::macros::wrap_free!($prefix::$object_type); - + ($command:ident, $object_type:ident) => { + $crate::macros::wrap_clone!($object_type); + $crate::macros::wrap_free!($object_type); $crate::commands::derive_command_from!($command); }; ($command:ident) => { - ::paste::paste!{ - wrap_command!($command, [< cmd_ $command:lower >], [< $command Command >]); + ::paste::paste! { + wrap_command!($command, [< $command Command >]); } }; } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 9ebf13a..1d482b4 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -9,8 +9,8 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_clone!(bitmap::Bitmap); -wrap_free!(bitmap::Bitmap); +wrap_clone!(Bitmap); +wrap_free!(Bitmap); wrap_functions!(bitmap; @@ -105,8 +105,7 @@ wrap_functions!(bitmap; } ); -wrap_methods!( - bitmap::Bitmap; +wrap_methods!(Bitmap; /// Gets the current value at the specified position. /// diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 4512ac1..b0782c4 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -55,11 +55,10 @@ wrap_functions!(bitvec; ); -wrap_clone!(bitvec::DisplayBitVec); -wrap_free!(bitvec::DisplayBitVec); +wrap_clone!(DisplayBitVec); +wrap_free!(DisplayBitVec); -wrap_methods!( - bitvec::DisplayBitVec; +wrap_methods!(DisplayBitVec; /// Gets the value of a bit. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 01fc010..8a9d3cc 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -71,11 +71,10 @@ wrap_functions!(brightnessgrid; ); -wrap_clone!(brightnessgrid::BrightnessGrid); -wrap_free!(brightnessgrid::BrightnessGrid); +wrap_clone!(BrightnessGrid); +wrap_free!(BrightnessGrid); -wrap_methods!( - brightnessgrid::BrightnessGrid; +wrap_methods!(BrightnessGrid; /// Gets the current value at the specified position. /// diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index 66bdce2..aac6615 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -36,6 +36,7 @@ impl ByteSlice { unsafe { std::slice::from_raw_parts(self.start, self.length) } } + #[allow(clippy::mut_from_ref, reason = "This is used to get a pointer from the C side")] pub(crate) unsafe fn as_slice_mut(&self) -> &mut [u8] { unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } } diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index a79af3a..f158702 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -58,11 +58,10 @@ wrap_functions!(chargrid; ); -wrap_clone!(chargrid::CharGrid); -wrap_free!(chargrid::CharGrid); +wrap_clone!(CharGrid); +wrap_free!(CharGrid); -wrap_methods!( - chargrid::CharGrid; +wrap_methods!(CharGrid; /// Returns the current value at the specified position. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index a78cecf..5822019 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -50,11 +50,10 @@ wrap_functions!(cp437grid; ); -wrap_clone!(cp437grid::Cp437Grid); -wrap_free!(cp437grid::Cp437Grid); +wrap_clone!(Cp437Grid); +wrap_free!(Cp437Grid); -wrap_methods!( - cp437grid::Cp437Grid; +wrap_methods!(Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments diff --git a/src/macros.rs b/src/macros.rs index 93e06b8..7cd0a71 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,22 +1,26 @@ macro_rules! wrap_free { - ($prefix:ident :: $typ:ty) => { - $crate::macros::wrap_functions!($prefix; - #[doc = concat!("Deallocates a [`", stringify!($typ), "`] instance.")] - fn free(instance: NonNull<$typ>) { - unsafe { $crate::mem::heap_drop(instance) } - } - ); + ($typ:ident) => { + ::paste::paste! { + $crate::macros::wrap_functions!([< $typ:lower >]; + #[doc = "Deallocates a [`" $typ "`] instance."] + fn free(instance: NonNull<$typ>) { + unsafe { $crate::mem::heap_drop(instance) } + } + ); + } }; } macro_rules! wrap_clone { - ($prefix:ident :: $typ:ty) => { - $crate::macros::wrap_functions!($prefix; - #[doc = concat!("Clones a [`", stringify!($typ), "`] instance.")] - fn clone(instance: NonNull<$typ>) -> NonNull<$typ> { - unsafe { $crate::mem::heap_clone(instance) } - } - ); + ($typ:ident) => { + ::paste::paste! { + $crate::macros::wrap_functions!([< $typ:lower >]; + #[doc = "Clones a [`" $typ "`] instance."] + fn clone(instance: NonNull<$typ>) -> NonNull<$typ> { + unsafe { $crate::mem::heap_clone(instance) } + } + ); + } }; } @@ -35,7 +39,7 @@ macro_rules! nonnull_as_mut { // meta required on purpose, because otherwise the added documentation would suppress warnings macro_rules! wrap_methods { ( - $prefix:ident :: $object_type:ty; + $object_type:ident; $( $(#[$meta:meta])+ $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) @@ -48,10 +52,9 @@ macro_rules! wrap_methods { )+ ) => { paste::paste! { - $crate::macros::wrap_functions!($prefix; + $crate::macros::wrap_functions!([< $object_type:lower >]; $( - #[doc = concat!(" Calls method [`servicepoint::", stringify!($object_type), - "::", stringify!($function), "`].")] + #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] #[doc = ""] $(#[$meta])* fn $function( @@ -82,7 +85,7 @@ macro_rules! wrap_methods { macro_rules! wrap_fields { ( - $prefix:ident :: $object_type:ty; + $object_type:ident; $( prop $prop_name:ident : $prop_type:ty { $( @@ -118,11 +121,10 @@ macro_rules! wrap_fields { )+ ) => { paste::paste! { - $crate::macros::wrap_functions!($prefix; + $crate::macros::wrap_functions!([< $object_type:lower >]; $( $( - #[doc = concat!(" Gets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::", stringify!($object_type),"`].")] + #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] $($( #[doc = ""] #[$get_meta] @@ -230,6 +232,25 @@ macro_rules! wrap_functions { )+ } }; + ( + associate $object_type:ident; + $( + $(#[$meta:meta])+ + fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) + $(-> $return_type:ty)? + $block:block + )+ + ) => { + ::paste::paste! { + $crate::macros::wrap_functions!{[< $object_type:lower >]; + $( + $(#[$meta])+ + fn $function($($param_name: $param_type),*) $(-> $return_type)? + $block + )+ + } + } + }; } pub(crate) use { diff --git a/src/packet.rs b/src/packet.rs index 18d63bd..df532b7 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -68,11 +68,10 @@ wrap_functions!(packet; } ); -wrap_clone!(packet::Packet); -wrap_free!(packet::Packet); +wrap_clone!(Packet); +wrap_free!(Packet); -wrap_fields!( - packet::Packet; +wrap_fields!(Packet; prop header: Header { get(); mut get(); diff --git a/src/udp.rs b/src/udp.rs index fd907be..21c067e 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -10,9 +10,9 @@ use std::{ ptr::NonNull, }; -wrap_free!(udp::UdpSocket); +wrap_free!(UdpSocket); -wrap_functions!(udp; +wrap_functions!(associate UdpSocket; /// Creates a new instance of [UdpSocket]. /// From 5ecb84ed16a3cb3880255cb47d549642646dc1ba Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:27:42 +0200 Subject: [PATCH 06/17] derive try_into_packet automatically --- include/servicepoint.h | 101 ++++++++++++++-------- src/commands/bitmap_command.rs | 13 +-- src/commands/bitvec_command.rs | 12 +-- src/commands/brightness_grid_command.rs | 13 +-- src/commands/char_grid_command.rs | 13 +-- src/commands/cp437_grid_command.rs | 13 +-- src/commands/global_brightness_command.rs | 9 +- src/commands/mod.rs | 23 ++++- src/containers/byte_slice.rs | 5 +- 9 files changed, 102 insertions(+), 100 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index e0e6cc4..34a3518 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -906,7 +906,7 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_y); /** - * Tries to turn a [BitmapCommand] into a [Packet]. + *Tries to turn a [`BitmapCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * @@ -1054,7 +1054,7 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); /** - * Tries to turn a [BitVecCommand] into a [Packet]. + *Tries to turn a [`BitVecCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * @@ -1252,15 +1252,6 @@ void sp_brightnessgridcommand_get_origin(struct BrightnessGridCommand */*notnull size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); -/** - * Tries to turn a [BrightnessGridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `brightnessgridcommand` module. - */ -struct Packet *sp_brightnessgridcommand_into_packet(struct BrightnessGridCommand */*notnull*/ command); - /** * Set the brightness of individual tiles in a rectangular area of the display. * @@ -1292,6 +1283,15 @@ void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull size_t origin_x, size_t origin_y); +/** + *Tries to turn a [`BrightnessGridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ command); + /** *Clones a [`CharGrid`] instance. * @@ -1470,6 +1470,15 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + *Tries to turn a [`CharGridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `chargridcommand` module. + */ +struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ command); + /** *Clones a [`ClearCommand`] instance. * @@ -1495,6 +1504,15 @@ void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); */ struct ClearCommand */*notnull*/ sp_clearcommand_new(void); +/** + *Tries to turn a [`ClearCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `clearcommand` module. + */ +struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ command); + /** * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. @@ -1516,15 +1534,6 @@ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ gr size_t origin_x, size_t origin_y); -/** - * Tries to turn a [CharGridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_chargrid` module. - */ -struct Packet *sp_cmd_chargrid_try_into_packet(struct CharGridCommand */*notnull*/ command); - /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. @@ -1546,15 +1555,6 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ size_t origin_x, size_t origin_y); -/** - * Tries to turn a [Cp437GridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Packet *sp_cmd_cp437grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); - /** * Clones an [SPCommand] instance. * @@ -1780,6 +1780,15 @@ void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command size_t origin_x, size_t origin_y); +/** + *Tries to turn a [`Cp437GridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*notnull*/ command); + /** * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. * @@ -1907,6 +1916,15 @@ void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); */ struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); +/** + *Tries to turn a [`FadeOutCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `fadeoutcommand` module. + */ +struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ command); + /** *Clones a [`GlobalBrightnessCommand`] instance. * @@ -1928,13 +1946,6 @@ void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ */ Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); -/** - * Turns the command into a packet - * - * This function is part of the `globalbrightnesscommand` module. - */ -struct Packet */*notnull*/ sp_globalbrightnesscommand_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); - /** * Set the brightness of all tiles to the same value. * @@ -1952,6 +1963,15 @@ struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brigh void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, Brightness value); +/** + *Tries to turn a [`GlobalBrightnessCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); + /** *Clones a [`HardResetCommand`] instance. * @@ -1977,6 +1997,15 @@ void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); */ struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); +/** + *Tries to turn a [`HardResetCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `hardresetcommand` module. + */ +struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ command); + /** *Clones a [`Packet`] instance. * diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 6fa5357..102e27d 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; +use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; wrap_command!(Bitmap); @@ -43,13 +43,4 @@ wrap_functions!(associate BitmapCommand; ) -> NonNull { heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) } - - /// Tries to turn a [BitmapCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index fab6a2d..02cb8c8 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,11 +1,10 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, - Packet, }; use std::ptr::NonNull; @@ -46,13 +45,4 @@ wrap_functions!(associate BitVecCommand; }) } - /// Tries to turn a [BitVecCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 8aed3d7..7bd5aef 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet}; +use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(BrightnessGrid); @@ -40,13 +40,4 @@ wrap_functions!(associate BrightnessGridCommand; heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } - /// Tries to turn a [BrightnessGridCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 551533e..a0ad2db 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{CharGrid, CharGridCommand, Origin, Packet}; +use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(CharGrid); @@ -40,13 +40,4 @@ wrap_functions!(cmd_chargrid; heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } - /// Tries to turn a [CharGridCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 913c393..7ccb1a3 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; +use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; wrap_command!(Cp437Grid); @@ -40,13 +40,4 @@ wrap_functions!(cmd_cp437grid; heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } - /// Tries to turn a [Cp437GridCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 247ef55..ef7ce6e 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; -use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; +use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; wrap_functions!(associate GlobalBrightnessCommand; @@ -15,11 +15,6 @@ wrap_functions!(associate GlobalBrightnessCommand; heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) } - /// Turns the command into a packet - fn into_packet(command: NonNull) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) - } - ); wrap_command!(GlobalBrightness); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a00ab89..c5e751b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -64,11 +64,29 @@ macro_rules! derive_command_from { }; } +macro_rules! derive_command_into_packet { + ($command_type:ident) => { + ::paste::paste! { + wrap_functions!(associate $command_type; + #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] + /// + /// Returns: NULL or a [Packet] containing the command. + fn try_into_packet( + command: NonNull<$command_type>, + ) -> *mut ::servicepoint::Packet { + $crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into()) + } + ); + } + } +} + macro_rules! wrap_command { ($command:ident, $object_type:ident) => { $crate::macros::wrap_clone!($object_type); $crate::macros::wrap_free!($object_type); $crate::commands::derive_command_from!($command); + $crate::commands::derive_command_into_packet!($object_type); }; ($command:ident) => { ::paste::paste! { @@ -77,4 +95,7 @@ macro_rules! wrap_command { }; } -pub(crate) use {derive_command_from, wrap_command, wrap_origin_accessors}; +pub(crate) use { + derive_command_from, derive_command_into_packet, wrap_command, + wrap_origin_accessors, +}; diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index aac6615..d12cd88 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -36,7 +36,10 @@ impl ByteSlice { unsafe { std::slice::from_raw_parts(self.start, self.length) } } - #[allow(clippy::mut_from_ref, reason = "This is used to get a pointer from the C side")] + #[allow( + clippy::mut_from_ref, + reason = "This is used to get a pointer from the C side." + )] pub(crate) unsafe fn as_slice_mut(&self) -> &mut [u8] { unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } } From b4730ffdf38505fa3aeb76f36dd57cda93231137 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:52:53 +0200 Subject: [PATCH 07/17] container macro code dedup --- include/servicepoint.h | 118 ++++++++++++++---------------- src/commands/mod.rs | 4 +- src/containers/bitmap.rs | 43 +---------- src/containers/bitvec.rs | 12 ++- src/containers/brightness_grid.rs | 51 ++----------- src/containers/char_grid.rs | 18 ++--- src/containers/cp437_grid.rs | 49 +------------ src/containers/mod.rs | 59 +++++++++++++++ src/macros.rs | 6 +- src/packet.rs | 6 +- src/udp.rs | 4 +- 11 files changed, 148 insertions(+), 222 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 34a3518..85f479d 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -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`]. * diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c5e751b..e15ba1f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -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); }; diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 1d482b4..b7e3a09 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -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. diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index b0782c4..6db5e6d 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -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 diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 8a9d3cc..a7854e3 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -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. diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index f158702..6cc8a4a 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -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; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 5822019..f25a502 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -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. diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 7576a7b..8726038 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -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)] diff --git a/src/macros.rs b/src/macros.rs index 7cd0a71..f737205 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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, }; diff --git a/src/packet.rs b/src/packet.rs index df532b7..b719b8a 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -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 { diff --git a/src/udp.rs b/src/udp.rs index 21c067e..321c4d1 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -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; From 7a836783e1044a6dbfee087f8180d69eeeb51a74 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:59:13 +0200 Subject: [PATCH 08/17] rename function to ...try to indicate possible null return --- include/servicepoint.h | 134 +++++++++++++++--------------- src/commands/cc_only_commands.rs | 3 +- src/commands/mod.rs | 10 +-- src/containers/bitmap.rs | 2 +- src/containers/bitvec.rs | 2 +- src/containers/brightness_grid.rs | 2 +- src/containers/char_grid.rs | 2 +- src/containers/cp437_grid.rs | 2 +- src/macros.rs | 8 +- 9 files changed, 82 insertions(+), 83 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 85f479d..4d77ba8 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -717,20 +717,6 @@ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); -/** - * Creates a [BitmapCommand] and immediately turns that into a [Packet]. - * - * The provided [Bitmap] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `bitmap` module. - */ -struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, - size_t x, - size_t y, - CompressionCode compression); - /** * Loads a [Bitmap] with the specified dimensions from the provided data. * @@ -806,6 +792,20 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t y, bool value); +/** + * Creates a [BitmapCommand] and immediately turns that into a [Packet]. + * + * The provided [Bitmap] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `bitmap` module. + */ +struct Packet *sp_bitmap_try_into_packet(struct Bitmap */*notnull*/ bitmap, + size_t x, + size_t y, + CompressionCode compression); + /** * Calls method [`servicepoint::Bitmap::width`]. * @@ -1089,19 +1089,6 @@ Brightness sp_brightnessgrid_get(BrightnessGrid */*notnull*/ instance, */ size_t sp_brightnessgrid_height(BrightnessGrid */*notnull*/ instance); -/** - * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. - * - * The provided [BrightnessGrid] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `brightnessgrid` module. - */ -struct Packet *sp_brightnessgrid_into_packet(BrightnessGrid */*notnull*/ grid, - size_t x, - size_t y); - /** * Loads a [BrightnessGrid] with the specified dimensions from the provided data. * @@ -1159,6 +1146,19 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, size_t y, Brightness value); +/** + * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. + * + * The provided [BrightnessGrid] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `brightnessgrid` module. + */ +struct Packet *sp_brightnessgrid_try_into_packet(BrightnessGrid */*notnull*/ grid, + size_t x, + size_t y); + /** * Calls method [`servicepoint::BrightnessGrid::width`]. * @@ -1303,19 +1303,6 @@ uint32_t sp_chargrid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); */ size_t sp_chargrid_height(CharGrid */*notnull*/ instance); -/** - * Creates a [CharGridCommand] and immediately turns that into a [Packet]. - * - * The provided [CharGrid] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `chargrid` module. - */ -struct Packet *sp_chargrid_into_packet(CharGrid */*notnull*/ grid, - size_t x, - size_t y); - /** * Loads a [CharGrid] with the specified dimensions from the provided data. * @@ -1367,6 +1354,19 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, size_t y, uint32_t value); +/** + * Creates a [CharGridCommand] and immediately turns that into a [Packet]. + * + * The provided [CharGrid] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `chargrid` module. + */ +struct Packet *sp_chargrid_try_into_packet(CharGrid */*notnull*/ grid, + size_t x, + size_t y); + /** * Calls method [`servicepoint::CharGrid::width`]. * @@ -1622,19 +1622,6 @@ uint8_t sp_cp437grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); */ size_t sp_cp437grid_height(Cp437Grid */*notnull*/ instance); -/** - * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. - * - * The provided [Cp437Grid] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `cp437grid` module. - */ -struct Packet *sp_cp437grid_into_packet(Cp437Grid */*notnull*/ grid, - size_t x, - size_t y); - /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. * @@ -1674,6 +1661,19 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, size_t y, uint8_t value); +/** + * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. + * + * The provided [Cp437Grid] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `cp437grid` module. + */ +struct Packet *sp_cp437grid_try_into_packet(Cp437Grid */*notnull*/ grid, + size_t x, + size_t y); + /** * Calls method [`servicepoint::Cp437Grid::width`]. * @@ -1801,20 +1801,6 @@ 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`]. * @@ -1879,6 +1865,20 @@ void sp_displaybitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); +/** + * 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_try_into_packet(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); + /** * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index a34e5c8..6635ae9 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -2,7 +2,6 @@ use crate::{ commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull, }; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; -use std::ptr::NonNull; macro_rules! wrap_cc_only { ($(#[$meta:meta])* $command:ident) => { @@ -13,7 +12,7 @@ macro_rules! wrap_cc_only { $(#[$meta])* /// #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] - fn new() -> NonNull<[< $command Command >]> { + fn new() -> ::core::ptr::NonNull<[< $command Command >]> { heap_move_nonnull([< $command Command >]) } ); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e15ba1f..9da0a22 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -20,9 +20,9 @@ macro_rules! wrap_origin_accessors { $crate::macros::wrap_functions!(associate $object_type; #[doc = " Reads the origin field of the [`" $object_type "`]."] fn get_origin( - command: NonNull<$object_type>, - origin_x: NonNull, - origin_y: NonNull, + command: ::core::ptr::NonNull<$object_type>, + origin_x: ::core::ptr::NonNull, + origin_y: ::core::ptr::NonNull, ) { unsafe { let origin = &command.as_ref().origin; @@ -33,7 +33,7 @@ macro_rules! wrap_origin_accessors { #[doc = " Overwrites the origin field of the [`" $object_type "`]."] fn set_origin( - command: NonNull<$object_type>, + command: ::core::ptr::NonNull<$object_type>, origin_x: usize, origin_y: usize, ) { @@ -72,7 +72,7 @@ macro_rules! derive_command_into_packet { /// /// Returns: NULL or a [Packet] containing the command. fn try_into_packet( - command: NonNull<$command_type>, + command: ::core::ptr::NonNull<$command_type>, ) -> *mut ::servicepoint::Packet { $crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into()) } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index b7e3a09..ffda27e 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -89,7 +89,7 @@ wrap_functions!(bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( bitmap: NonNull, x: usize, y: usize, diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 6db5e6d..1549d60 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -40,7 +40,7 @@ wrap_functions!(associate DisplayBitVec; /// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( bitvec: NonNull, offset: usize, operation: BinaryOperation, diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a7854e3..033bbdd 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -59,7 +59,7 @@ wrap_functions!(associate BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( grid: NonNull, x: usize, y: usize, diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 6cc8a4a..365f1ad 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -47,7 +47,7 @@ wrap_functions!(associate CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( grid: NonNull, x: usize, y: usize, diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index f25a502..001ebb8 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -37,7 +37,7 @@ wrap_functions!(cp437grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( grid: NonNull, x: usize, y: usize, diff --git a/src/macros.rs b/src/macros.rs index f737205..20259fc 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,7 +3,7 @@ macro_rules! derive_free { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Deallocates a [`" $typ "`] instance."] - fn free(instance: NonNull<$typ>) { + fn free(instance: ::core::ptr::NonNull<$typ>) { unsafe { $crate::mem::heap_drop(instance) } } ); @@ -16,7 +16,7 @@ macro_rules! derive_clone { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Clones a [`" $typ "`] instance."] - fn clone(instance: NonNull<$typ>) -> NonNull<$typ> { + fn clone(instance: ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { unsafe { $crate::mem::heap_clone(instance) } } ); @@ -58,7 +58,7 @@ macro_rules! wrap_methods { #[doc = ""] $(#[$meta])* fn $function( - instance: NonNull<$object_type>, + instance: ::core::ptr::NonNull<$object_type>, $($param_name: $param_type),* ) $(-> $return_type)? { let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; @@ -192,7 +192,7 @@ macro_rules! wrap_fields { )*)? fn []( instance: ::core::ptr::NonNull<$object_type>, - value: NonNull<$prop_type>, + value: ::core::ptr::NonNull<$prop_type>, ) { let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; let value = unsafe { $crate::mem::heap_remove(value) }; From 92ce27af68db703b41cbd9e26878a3194201cf45 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 14:52:44 +0200 Subject: [PATCH 09/17] simplify property exports --- example/src/announce.c | 2 +- example/src/brightness_tester.c | 2 +- example/src/random_stuff.c | 2 +- src/commands/bitmap_command.rs | 4 +- src/commands/bitvec_command.rs | 8 +- src/commands/brightness_grid_command.rs | 2 +- src/commands/char_grid_command.rs | 2 +- src/commands/cp437_grid_command.rs | 2 +- src/commands/global_brightness_command.rs | 5 +- src/macros.rs | 154 ++++++++-------------- src/packet.rs | 6 +- 11 files changed, 70 insertions(+), 119 deletions(-) diff --git a/example/src/announce.c b/example/src/announce.c index 33da334..a0bdf9a 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -20,7 +20,7 @@ int main(void) { sp_chargrid_set(grid, 3, 1, 'l'); sp_chargrid_set(grid, 4, 1, 'd'); - Packet *packet = sp_chargrid_into_packet(grid, 0, 0); + Packet *packet = sp_chargrid_try_into_packet(grid, 0, 0); if (packet == NULL) return 1; sp_udpsocket_send_packet(sock, packet); diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 9d17dc7..921bbd0 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -26,7 +26,7 @@ int main(void) { BrightnessGrid *grid = sp_brightnessgrid_new(TILE_WIDTH, TILE_HEIGHT); make_brightness_pattern(grid); - Packet *packet = sp_brightnessgridcommand_into_packet(sp_brightnessgridcommand_from_grid(grid)); + Packet *packet = sp_brightnessgridcommand_try_into_packet(sp_brightnessgridcommand_from_grid(grid)); if (packet == NULL) return -2; diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index 8838123..7a58c3a 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -11,7 +11,7 @@ int main(void) { sp_bitmap_fill(pixels, true); - Packet *packet = sp_bitmap_into_packet(pixels, 0, 0, COMPRESSION_CODE_UNCOMPRESSED); + Packet *packet = sp_bitmap_try_into_packet(pixels, 0, 0, COMPRESSION_CODE_UNCOMPRESSED); if (packet == NULL) return 1; diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 102e27d..01e3620 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -9,8 +9,8 @@ use std::ptr::NonNull; wrap_command!(Bitmap); wrap_fields!(BitmapCommand; - prop bitmap: Bitmap { mut get(); move set(value); }; - prop compression: CompressionCode { get(); set(value); }; + prop bitmap: Bitmap { get mut; set move; }; + prop compression: CompressionCode { get; set; }; ); wrap_origin_accessors!(BitmapCommand); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 02cb8c8..e796380 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -11,10 +11,10 @@ use std::ptr::NonNull; wrap_command!(BitVec); wrap_fields!(BitVecCommand; - prop bitvec: DisplayBitVec { mut get(); move set(value); }; - prop offset: Offset { get(); set(value); }; - prop operation: BinaryOperation { get(); set(value); }; - prop compression: CompressionCode { get(); set(value); }; + prop bitvec: DisplayBitVec { get mut; set move; }; + prop offset: Offset { get; set; }; + prop operation: BinaryOperation { get; set; }; + prop compression: CompressionCode { get; set; }; ); wrap_functions!(associate BitVecCommand; diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7bd5aef..853a3fe 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -9,7 +9,7 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); wrap_fields!(BrightnessGridCommand; - prop grid: BrightnessGrid { mut get(); move set(grid); }; + prop grid: BrightnessGrid { get mut; set move; }; ); wrap_origin_accessors!(BrightnessGridCommand); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index a0ad2db..f8d5107 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -9,7 +9,7 @@ use std::ptr::NonNull; wrap_command!(CharGrid); wrap_fields!(CharGridCommand; - prop grid: CharGrid { mut get(); move set(grid); }; + prop grid: CharGrid { get mut; set move; }; ); wrap_origin_accessors!(CharGridCommand); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 7ccb1a3..4977195 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -9,7 +9,7 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); wrap_fields!(Cp437GridCommand; - prop grid: Cp437Grid { mut get(); move set(grid); }; + prop grid: Cp437Grid { get mut; set move; }; ); wrap_origin_accessors!(Cp437GridCommand); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index ef7ce6e..76cbeb5 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -20,8 +20,5 @@ wrap_functions!(associate GlobalBrightnessCommand; wrap_command!(GlobalBrightness); wrap_fields!(GlobalBrightnessCommand; - prop brightness: Brightness { - get(); - set(value); - }; + prop brightness: Brightness { get; set; }; ); diff --git a/src/macros.rs b/src/macros.rs index 20259fc..8dcaf55 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -55,7 +55,7 @@ macro_rules! wrap_methods { $crate::macros::wrap_functions!([< $object_type:lower >]; $( #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] - #[doc = ""] + /// $(#[$meta])* fn $function( instance: ::core::ptr::NonNull<$object_type>, @@ -83,132 +83,90 @@ macro_rules! wrap_methods { }; } -macro_rules! wrap_fields { - ( - $object_type:ident; - $( - prop $prop_name:ident : $prop_type:ty { - $( - get() $({ - $(#[$get_meta:meta])* - $(return $get_expr:expr;)? - })?; - )? - - $( - mut get() $({ - $(#[$get_mut_meta:meta])* - $(return $get_mut_expr:expr;)? - })?; - )? - - $( - set($value:ident) - $({ - $(#[$set_meta:meta])* - $(return $set_expr:expr;)? - })?; - )? - - $( - move set( $set_move_value:ident) - $({ - $(#[$set_move_meta:meta])* - $(return $set_move_expr:expr;)? - })?; - )? - }; - )+ - ) => { +macro_rules! wrap_fields_accessor { + (get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions!([< $object_type:lower >]; - $( - $( - #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - $($( - #[doc = ""] - #[$get_meta] - )*)? + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Gets the value of field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] fn []( instance: ::core::ptr::NonNull<$object_type> ) -> $prop_type { let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name }; - $($( - let $prop_name = $get_expr; - )?)? return $prop_name; } - )? - - $( - #[doc = concat!(" Gets a reference to the field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - $($( - #[doc = ""] - #[$get_mut_meta] - )*)? - #[doc = ""] - #[doc = " - The returned reference inherits the lifetime of object in which it is contained."] - #[doc = " - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command."] + } + } + }; + (mut get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { + paste::paste! { + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Gets a reference to the field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] + /// + /// - The returned reference inherits the lifetime of object in which it is contained. + /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. fn []( instance: ::core::ptr::NonNull<$object_type> ) -> ::core::ptr::NonNull<$prop_type> { let $prop_name = unsafe { &mut $crate::macros::nonnull_as_mut!(instance).$prop_name }; - $($( - let $prop_name = $get_mut_expr; - )?)? return ::core::ptr::NonNull::from($prop_name); } - )? - - $( - #[doc = concat!(" Sets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - $($( - #[doc = ""] - #[$set_meta] - )*)? + } + } + }; + (set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { + paste::paste! { + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Sets the value of field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] fn []( instance: ::core::ptr::NonNull<$object_type>, value: $prop_type, ) { let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; - $($( - let $value = value; - let value = $set_expr; - )?)? instance.$prop_name = value; } - )? - - $( - #[doc = concat!(" Sets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - #[doc = concat!(" The provided value is moved into the instance, ", - "potentially invalidating previously taken references.")] - $($( - #[doc = ""] - #[$set_move_meta] - )*)? + } + } + }; + (move set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { + paste::paste! { + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Sets the value of field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] + /// The provided value is moved into the instance, potentially invalidating previously taken references. fn []( instance: ::core::ptr::NonNull<$object_type>, value: ::core::ptr::NonNull<$prop_type>, ) { let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; - let value = unsafe { $crate::mem::heap_remove(value) }; - $($( - let $set_move_value = value; - let value = $set_move_expr; - )?)? - instance.$prop_name = value; + let $prop_name = unsafe { $crate::mem::heap_remove(value) }; + instance.$prop_name = $prop_name; } - )? - )+ - ); + } } }; } +macro_rules! wrap_fields { + ( + $object_type:ident; + $( + prop $prop_name:ident : $prop_type:ty { $($accessor:ident $($modifier:ident)?;)+ }; + )+ + ) => { + $($( + ::paste::paste!{ + $crate::macros::wrap_fields_accessor! { + $($modifier)? $accessor; + $object_type :: $prop_name: $prop_type + } + } + )+)+ + }; +} + macro_rules! wrap_functions { ( $module:ident; @@ -255,5 +213,5 @@ macro_rules! wrap_functions { pub(crate) use { derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, - wrap_functions, wrap_methods, + wrap_functions, wrap_methods, wrap_fields_accessor }; diff --git a/src/packet.rs b/src/packet.rs index b719b8a..28ade85 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -72,11 +72,7 @@ derive_clone!(Packet); derive_free!(Packet); wrap_fields!(Packet; - prop header: Header { - get(); - mut get(); - set(value); - }; + prop header: Header { get; get mut; set; }; ); wrap_functions!(sp; From e4341307844754658d658a4055b6b42853d4be25 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 19:43:52 +0200 Subject: [PATCH 10/17] unifiy special handling of params and return --- src/commands/mod.rs | 4 +- src/containers/bitmap.rs | 4 +- src/containers/bitvec.rs | 16 +++--- src/containers/brightness_grid.rs | 15 +++--- src/containers/char_grid.rs | 12 ++--- src/containers/cp437_grid.rs | 4 +- src/containers/mod.rs | 10 ++-- src/macros.rs | 83 +++++++++++++++++++------------ 8 files changed, 82 insertions(+), 66 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9da0a22..58e5957 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -67,7 +67,7 @@ macro_rules! derive_command_from { macro_rules! derive_command_into_packet { ($command_type:ident) => { ::paste::paste! { - wrap_functions!(associate $command_type; + $crate::macros::wrap_functions!(associate $command_type; #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. @@ -90,7 +90,7 @@ macro_rules! wrap_command { }; ($command:ident) => { ::paste::paste! { - wrap_command!($command, [< $command Command >]); + $crate::commands::wrap_command!($command, [< $command Command >]); } }; } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index ffda27e..9bae852 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -109,7 +109,7 @@ wrap_methods!(Bitmap; /// 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 { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn data_ref_mut(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 1549d60..2a76cf7 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -71,8 +71,8 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - ref fn get(index: usize) -> bool { - return(result) { result.map(|x| *x).unwrap_or(false) }; + ref fn get(instance, index: usize) -> bool { + instance.get(index).map(|x| *x).unwrap_or(false) }; /// Sets the value of a bit. @@ -85,25 +85,25 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - mut fn set(index: usize, value: bool); + mut fn set(instance, index: usize, value: bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - mut fn fill(value: bool); + mut fn fill(instance, value: bool); /// Gets the length in bits. - ref fn len() -> usize; + ref fn len(instance) -> usize; /// Returns true if length is 0. - ref fn is_empty() -> bool; + ref fn is_empty(instance) -> 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 { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn as_raw_mut_slice(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } }; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 033bbdd..a5d01f2 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -77,14 +77,13 @@ wrap_methods!(BrightnessGrid; /// 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 { - return(br_slice) { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); + mut fn data_ref_mut(instance) -> ByteSlice { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - unsafe { - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) - } - }; + let br_slice = instance.data_ref_mut(); + unsafe { + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + } }; ); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 365f1ad..1a59fdd 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -72,8 +72,8 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> u32 { - return(char) { char as u32 }; + ref fn get(instance, x: usize, y: usize) -> u32 { + instance.get(x, y) as u32 }; /// Sets the value of the specified position in the grid. @@ -89,8 +89,8 @@ wrap_methods!(CharGrid; /// /// - 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) { - prepare(value) { char::from_u32(value).unwrap() }; + mut fn set(instance, x: usize, y: usize, value: u32) { + instance.set(x, y, char::from_u32(value).unwrap()) }; /// Sets the value of all cells in the grid. @@ -99,7 +99,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - mut fn fill(value: u32) { - prepare(value) { char::from_u32(value).unwrap() }; + mut fn fill(instance, value: u32) { + instance.fill(char::from_u32(value).unwrap()) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 001ebb8..fc31d13 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -55,7 +55,7 @@ wrap_methods!(Cp437Grid; /// 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 { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn data_ref_mut(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 8726038..fcd76a1 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -23,10 +23,10 @@ macro_rules! derive_get_width_height { ($object_type:ident) => { $crate::macros::wrap_methods! {$object_type; /// Gets the width. - ref fn width() -> usize; + ref fn width(instance) -> usize; /// Gets the height. - ref fn height() -> usize; + ref fn height(instance) -> usize; } }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> $value_type; + ref fn get(instance, x: usize, y: usize) -> $value_type; /// Sets the value of the specified position. /// @@ -57,14 +57,14 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - mut fn set(x: usize, y: usize, value: $value_type); + mut fn set(instance, 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); + mut fn fill(instance, value: $value_type); } }; } diff --git a/src/macros.rs b/src/macros.rs index 8dcaf55..e0560f5 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -36,49 +36,66 @@ macro_rules! nonnull_as_mut { }; } -// meta required on purpose, because otherwise the added documentation would suppress warnings +macro_rules! wrap_method { + ( + $object_type:ident; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + $(-> $return_type:ty)? + ) => { + ::paste::paste!{ + $crate::macros::wrap_method!( + $object_type; + $(#[$meta])+ + $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + $(-> $return_type)? { + $instance.$function($($($param_name),*)?) + } + ); + } + }; + ($object_type:ident; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + $(-> $return_type:ty)? + $impl:block + ) => { + paste::paste! { + $crate::macros::wrap_functions!([< $object_type:lower >]; + #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] + /// + $(#[$meta])* + fn $function( + $instance: ::core::ptr::NonNull<$object_type> + $(,$($param_name: $param_type),*)? + ) $(-> $return_type)? { + let $instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !($instance) }; + $impl + } + ); + } + }; +} + macro_rules! wrap_methods { ( $object_type:ident; $( $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? - $({ - $($(prepare($param_let_name:ident) $param_let_expr:block);+;)? - $(return($it:ident) $return_expr:block;)? - })? - ; + $($impl:block)?; )+ ) => { paste::paste! { - $crate::macros::wrap_functions!([< $object_type:lower >]; - $( - #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] - /// + $( + $crate::macros::wrap_method!($object_type; $(#[$meta])* - fn $function( - instance: ::core::ptr::NonNull<$object_type>, - $($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" )] - let result = instance.$function($($param_name),*); - $( - $( - let $it = result; - let result = $return_expr; - )? - )? - return result; - } - )+ + $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + $(-> $return_type)? + $($impl)? ); + )+ } }; } @@ -213,5 +230,5 @@ macro_rules! wrap_functions { pub(crate) use { derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, - wrap_functions, wrap_methods, wrap_fields_accessor + wrap_functions, wrap_methods, wrap_fields_accessor, wrap_method }; From 323ba6128e0af019bc66f41bbc04bfee300149ef Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 20:26:07 +0200 Subject: [PATCH 11/17] add move fn to dsl, rename SPCommand to GenericCommand, remove DisplayBitVec command --- example/src/header_logger.c | 24 +-- example/src/undefined.c | 4 +- example/src/wiping_clear.c | 2 +- include/servicepoint.h | 180 +++++++++++--------- src/commands/generic_command.rs | 265 +++++++++++++++--------------- src/commands/mod.rs | 10 +- src/containers/bitmap.rs | 24 +-- src/containers/bitvec.rs | 18 +- src/containers/brightness_grid.rs | 17 +- src/containers/char_grid.rs | 41 ++--- src/containers/cp437_grid.rs | 17 +- src/macros.rs | 11 +- src/udp.rs | 78 ++++----- 13 files changed, 335 insertions(+), 356 deletions(-) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index ec421dd..5793da9 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -15,8 +15,8 @@ void handle_error(const char *msg) { exit(EXIT_FAILURE); } -bool log_command(struct Command command) { - switch (command.tag) { +bool log_command(struct GenericCommand *command) { + switch (command->tag) { case COMMAND_TAG_INVALID: { printf("-> this is an invalid command\n"); break; @@ -26,7 +26,7 @@ bool log_command(struct Command command) { return true; } case COMMAND_TAG_BITMAP: { - BitmapCommand *bitmapCommand = command.data.bitmap; + BitmapCommand *bitmapCommand = command->data.bitmap; CompressionCode compression = sp_bitmapcommand_get_compression(bitmapCommand); @@ -42,7 +42,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_BRIGHTNESS_GRID: { - BrightnessGridCommand *gridCommand = command.data.brightness_grid; + BrightnessGridCommand *gridCommand = command->data.brightness_grid; size_t x, y; sp_brightnessgridcommand_get_origin(gridCommand, &x, &y); @@ -56,7 +56,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_CHAR_GRID: { - CharGridCommand *gridCommand = command.data.char_grid; + CharGridCommand *gridCommand = command->data.char_grid; size_t x, y; sp_chargridcommand_get_origin(gridCommand, &x, &y); @@ -70,7 +70,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_CP437_GRID: { - Cp437GridCommand *gridCommand = command.data.cp437_grid; + Cp437GridCommand *gridCommand = command->data.cp437_grid; size_t x, y; sp_cp437gridcommand_get_origin(gridCommand, &x, &y); @@ -84,7 +84,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_BIT_VEC: { - BitVecCommand *bitvecCommand = command.data.bit_vec; + BitVecCommand *bitvecCommand = command->data.bit_vec; size_t offset = sp_bitveccommand_get_offset(bitvecCommand); CompressionCode compression = sp_bitveccommand_get_compression(bitvecCommand); @@ -109,7 +109,7 @@ bool log_command(struct Command command) { break; } - BitVec *bitvec = sp_bitveccommand_get_bitvec_mut(bitvecCommand); + DisplayBitVec *bitvec = sp_bitveccommand_get_bitvec_mut(bitvecCommand); size_t len = sp_displaybitvec_len(bitvec); printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", @@ -129,12 +129,12 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_globalbrightnesscommand_get_brightness(command.data.global_brightness); + Brightness brightness = sp_globalbrightnesscommand_get_brightness(command->data.global_brightness); printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); break; } default: { - printf("-> unknown command tag %d\n", command.tag); + printf("-> unknown command tag %d\n", command->tag); break; } } @@ -198,10 +198,10 @@ int main(int argc, char **argv) { header->command_code, header->a, header->b, header->c, header->d, payload.start, payload.length); - struct Command command = sp_cmd_generic_try_from_packet(packet); + struct GenericCommand *command = sp_genericcommand_try_from_packet(packet); done = log_command(command); - sp_cmd_generic_free(command); + sp_genericcommand_free(command); } close(udp_socket); diff --git a/example/src/undefined.c b/example/src/undefined.c index 44aaf6d..b869d71 100644 --- a/example/src/undefined.c +++ b/example/src/undefined.c @@ -9,14 +9,14 @@ int main(void) { sp_bitveccommand_free(bvcmd); uint8_t *data = calloc(1024, 1); - struct Command generic = { + struct GenericCommand generic = { .tag = COMMAND_TAG_BRIGHTNESS_GRID, .data = {.null = data}, }; sock_init(); - sp_udpsocket_send_command(sock, generic); + sp_udpsocket_send_command(sock, &generic); return 0; } diff --git a/example/src/wiping_clear.c b/example/src/wiping_clear.c index 3984169..1d0440b 100644 --- a/example/src/wiping_clear.c +++ b/example/src/wiping_clear.c @@ -13,7 +13,7 @@ int main() { sp_bitmap_set(enabled_pixels, x, y, false); } - BitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); + DisplayBitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); BitVecCommand *command = sp_bitveccommand_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); Packet *packet = sp_bitveccommand_try_into_packet(command); if (packet == NULL) { diff --git a/include/servicepoint.h b/include/servicepoint.h index 4d77ba8..095c6cc 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -355,7 +355,7 @@ typedef struct Cp437GridCommand Cp437GridCommand; /** * This is a type only used by cbindgen to have a type for pointers. */ -typedef struct BitVec BitVec; +typedef struct DisplayBitVec DisplayBitVec; /** *
Untested
@@ -578,7 +578,7 @@ typedef union CommandUnion { * * Rust equivalent: [TypedCommand]. */ -typedef struct Command { +typedef struct GenericCommand { /** * Specifies which kind of command struct is contained in `data` */ @@ -587,7 +587,7 @@ typedef struct Command { * The pointer to the command struct */ union CommandUnion data; -} Command; +} GenericCommand; /** * A raw header. @@ -682,7 +682,8 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); * * This function is part of the `bitmap` module. */ -struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); +struct Bitmap *sp_bitmap_from_bitvec(size_t width, + DisplayBitVec */*notnull*/ bitvec); /** * Calls method [`servicepoint::Bitmap::get`]. @@ -711,11 +712,13 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** + * Calls method [`servicepoint::Bitmap::into_bitvec`]. + * * Consumes the Bitmap and returns the contained BitVec. * * This function is part of the `bitmap` module. */ -BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); +DisplayBitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); /** * Loads a [Bitmap] with the specified dimensions from the provided data. @@ -793,6 +796,8 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, bool value); /** + * Calls method [`servicepoint::Bitmap::try_into_packet`]. + * * Creates a [BitmapCommand] and immediately turns that into a [Packet]. * * The provided [Bitmap] gets consumed. @@ -906,13 +911,15 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_y); /** + * Calls method [`servicepoint::BitmapCommand::try_into_packet`]. + * *Tries to turn a [`BitmapCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `bitmapcommand` module. */ -struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ command); +struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ instance); /** *Clones a [`BitVecCommand`] instance. @@ -936,7 +943,7 @@ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); * * This function is part of the `bitveccommand` module. */ -BitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); +DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. @@ -975,7 +982,7 @@ BinaryOperation sp_bitveccommand_get_operation(struct BitVecCommand */*notnull*/ * * This function is part of the `bitveccommand` module. */ -struct BitVecCommand */*notnull*/ sp_bitveccommand_new(BitVec */*notnull*/ bitvec, +struct BitVecCommand */*notnull*/ sp_bitveccommand_new(DisplayBitVec */*notnull*/ bitvec, size_t offset, BinaryOperation operation, CompressionCode compression); @@ -987,7 +994,7 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_new(BitVec */*notnull*/ bitve * This function is part of the `bitveccommand` module. */ void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, - BitVec */*notnull*/ value); + DisplayBitVec */*notnull*/ value); /** * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. @@ -1014,13 +1021,15 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); /** + * Calls method [`servicepoint::BitVecCommand::try_into_packet`]. + * *Tries to turn a [`BitVecCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `bitveccommand` module. */ -struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ command); +struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ instance); /** *Clones a [`BrightnessGrid`] instance. @@ -1147,6 +1156,8 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, Brightness value); /** + * Calls method [`servicepoint::BrightnessGrid::try_into_packet`]. + * * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. * * The provided [BrightnessGrid] gets consumed. @@ -1241,13 +1252,15 @@ void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull size_t origin_y); /** + * Calls method [`servicepoint::BrightnessGridCommand::try_into_packet`]. + * *Tries to turn a [`BrightnessGridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `brightnessgridcommand` module. */ -struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ command); +struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ instance); /** *Clones a [`CharGrid`] instance. @@ -1355,6 +1368,8 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, uint32_t value); /** + * Calls method [`servicepoint::CharGrid::try_into_packet`]. + * * Creates a [CharGridCommand] and immediately turns that into a [Packet]. * * The provided [CharGrid] gets consumed. @@ -1428,13 +1443,15 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_y); /** + * Calls method [`servicepoint::CharGridCommand::try_into_packet`]. + * *Tries to turn a [`CharGridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `chargridcommand` module. */ -struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ command); +struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ instance); /** *Clones a [`ClearCommand`] instance. @@ -1462,13 +1479,15 @@ void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); struct ClearCommand */*notnull*/ sp_clearcommand_new(void); /** + * Calls method [`servicepoint::ClearCommand::try_into_packet`]. + * *Tries to turn a [`ClearCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `clearcommand` module. */ -struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ command); +struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], @@ -1512,52 +1531,6 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ size_t origin_x, size_t origin_y); -/** - * Clones an [SPCommand] instance. - * - * returns: a new [SPCommand] instance. - * - * This function is part of the `cmd_generic` module. - */ -struct Command sp_cmd_generic_clone(struct Command command); - -/** - * Deallocates an [SPCommand]. - * - * Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. - * - * # Examples - * - * ```C - * SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); - * sp_command_free(c); - * ``` - * - * This function is part of the `cmd_generic` module. - */ -void sp_cmd_generic_free(struct Command command); - -/** - * Tries to turn a [SPCommand] into a [Packet]. - * The [SPCommand] gets consumed. - * - * Returns tag [CommandTag::Invalid] in case of an error. - * - * This function is part of the `cmd_generic` module. - */ -struct Packet *sp_cmd_generic_into_packet(struct Command command); - -/** - * Tries to turn a [Packet] into a [SPCommand]. - * - * The packet is dropped in the process. - * - * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. - * - * This function is part of the `cmd_generic` module. - */ -struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); - /** *Clones a [`Cp437Grid`] instance. * @@ -1662,6 +1635,8 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, uint8_t value); /** + * Calls method [`servicepoint::Cp437Grid::try_into_packet`]. + * * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. * * The provided [Cp437Grid] gets consumed. @@ -1735,13 +1710,15 @@ void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command size_t origin_y); /** + * Calls method [`servicepoint::Cp437GridCommand::try_into_packet`]. + * *Tries to turn a [`Cp437GridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `cp437gridcommand` module. */ -struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*notnull*/ command); +struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. @@ -1752,14 +1729,14 @@ struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*no * * This function is part of the `displaybitvec` module. */ -struct ByteSlice sp_displaybitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); +struct ByteSlice sp_displaybitvec_as_raw_mut_slice(DisplayBitVec */*notnull*/ instance); /** *Clones a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. */ -BitVec */*notnull*/ sp_displaybitvec_clone(BitVec */*notnull*/ instance); +DisplayBitVec */*notnull*/ sp_displaybitvec_clone(DisplayBitVec */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::fill`]. @@ -1772,14 +1749,14 @@ BitVec */*notnull*/ sp_displaybitvec_clone(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -void sp_displaybitvec_fill(BitVec */*notnull*/ instance, bool value); +void sp_displaybitvec_fill(DisplayBitVec */*notnull*/ instance, bool value); /** *Deallocates a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. */ -void sp_displaybitvec_free(BitVec */*notnull*/ instance); +void sp_displaybitvec_free(DisplayBitVec */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::get`]. @@ -1799,7 +1776,7 @@ void sp_displaybitvec_free(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); +bool sp_displaybitvec_get(DisplayBitVec */*notnull*/ instance, size_t index); /** * Calls method [`servicepoint::DisplayBitVec::is_empty`]. @@ -1808,7 +1785,7 @@ bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); * * This function is part of the `displaybitvec` module. */ -bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance); +bool sp_displaybitvec_is_empty(DisplayBitVec */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::len`]. @@ -1817,7 +1794,7 @@ bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -size_t sp_displaybitvec_len(BitVec */*notnull*/ instance); +size_t sp_displaybitvec_len(DisplayBitVec */*notnull*/ instance); /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. @@ -1826,7 +1803,7 @@ size_t sp_displaybitvec_len(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -BitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); +DisplayBitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); /** * Creates a new [DisplayBitVec] instance. @@ -1843,7 +1820,7 @@ BitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); * * This function is part of the `displaybitvec` module. */ -BitVec */*notnull*/ sp_displaybitvec_new(size_t size); +DisplayBitVec */*notnull*/ sp_displaybitvec_new(size_t size); /** * Calls method [`servicepoint::DisplayBitVec::set`]. @@ -1861,11 +1838,13 @@ BitVec */*notnull*/ sp_displaybitvec_new(size_t size); * * This function is part of the `displaybitvec` module. */ -void sp_displaybitvec_set(BitVec */*notnull*/ instance, +void sp_displaybitvec_set(DisplayBitVec */*notnull*/ instance, size_t index, bool value); /** + * Calls method [`servicepoint::DisplayBitVec::try_into_packet`]. + * * Creates a [BitVecCommand] and immediately turns that into a [Packet]. * * The provided [DisplayBitVec] gets consumed. @@ -1874,7 +1853,7 @@ void sp_displaybitvec_set(BitVec */*notnull*/ instance, * * This function is part of the `displaybitvec` module. */ -struct Packet *sp_displaybitvec_try_into_packet(BitVec */*notnull*/ bitvec, +struct Packet *sp_displaybitvec_try_into_packet(DisplayBitVec */*notnull*/ bitvec, size_t offset, BinaryOperation operation, CompressionCode compression); @@ -1911,13 +1890,52 @@ void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); /** + * Calls method [`servicepoint::FadeOutCommand::try_into_packet`]. + * *Tries to turn a [`FadeOutCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `fadeoutcommand` module. */ -struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ command); +struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ instance); + +/** + *Clones a [`GenericCommand`] instance. + * + * This function is part of the `genericcommand` module. + */ +struct GenericCommand */*notnull*/ sp_genericcommand_clone(struct GenericCommand */*notnull*/ instance); + +/** + *Deallocates a [`GenericCommand`] instance. + * + * This function is part of the `genericcommand` module. + */ +void sp_genericcommand_free(struct GenericCommand */*notnull*/ instance); + +/** + * Tries to turn a [Packet] into a [GenericCommand]. + * + * The packet is dropped in the process. + * + * Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. + * + * This function is part of the `genericcommand` module. + */ +struct GenericCommand */*notnull*/ sp_genericcommand_try_from_packet(struct Packet */*notnull*/ packet); + +/** + * Calls method [`servicepoint::GenericCommand::try_into_packet`]. + * + * Tries to turn a [GenericCommand] into a [Packet]. + * The [GenericCommand] gets consumed. + * + * Returns tag [CommandTag::Invalid] in case of an error. + * + * This function is part of the `genericcommand` module. + */ +struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnull*/ command); /** *Clones a [`GlobalBrightnessCommand`] instance. @@ -1958,13 +1976,15 @@ void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */ Brightness value); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::try_into_packet`]. + * *Tries to turn a [`GlobalBrightnessCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `globalbrightnesscommand` module. */ -struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); +struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Clones a [`HardResetCommand`] instance. @@ -1992,13 +2012,15 @@ void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); /** + * Calls method [`servicepoint::HardResetCommand::try_into_packet`]. + * *Tries to turn a [`HardResetCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `hardresetcommand` module. */ -struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ command); +struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ instance); /** *Clones a [`Packet`] instance. @@ -2147,7 +2169,9 @@ struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, uint16_t port); /** - * Sends a [SPCommand] to the display using the [UdpSocket]. + * Calls method [`servicepoint::UdpSocket::send_command`]. + * + * Sends a [GenericCommand] to the display using the [UdpSocket]. * * The passed `command` gets consumed. * @@ -2162,9 +2186,11 @@ struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, * This function is part of the `udpsocket` module. */ bool sp_udpsocket_send_command(struct UdpSocket */*notnull*/ connection, - struct Command command); + struct GenericCommand */*notnull*/ command); /** + * Calls method [`servicepoint::UdpSocket::send_header`]. + * * Sends a [Header] to the display using the [UdpSocket]. * * returns: true in case of success @@ -2181,6 +2207,8 @@ bool sp_udpsocket_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); /** + * Calls method [`servicepoint::UdpSocket::send_packet`]. + * * Sends a [Packet] to the display using the [UdpSocket]. * * The passed `packet` gets consumed. diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index a4775d9..609daac 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::wrap_functions, + macros::{derive_clone, derive_free, wrap_functions}, mem::{ heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, heap_remove, @@ -11,6 +11,7 @@ use servicepoint::{ HardResetCommand, Packet, TypedCommand, }; use std::ptr::{null_mut, NonNull}; +use crate::macros::wrap_methods; /// Pointer to one of the available command structs. #[repr(C)] @@ -55,214 +56,211 @@ pub enum CommandTag { /// /// Rust equivalent: [TypedCommand]. #[repr(C)] -pub struct SPCommand { +pub struct GenericCommand { /// Specifies which kind of command struct is contained in `data` pub tag: CommandTag, /// The pointer to the command struct pub data: CommandUnion, } -impl SPCommand { - const INVALID: SPCommand = SPCommand { +impl GenericCommand { + pub(crate) const INVALID: GenericCommand = GenericCommand { tag: CommandTag::Invalid, data: CommandUnion { null: null_mut() }, }; } -wrap_functions!(cmd_generic; +derive_clone!(GenericCommand); - /// Tries to turn a [Packet] into a [SPCommand]. +impl Clone for GenericCommand { + fn clone(&self) -> Self { + unsafe { + match self.tag { + CommandTag::Clear => GenericCommand { + tag: CommandTag::Clear, + data: CommandUnion { + clear: heap_clone(self.data.clear), + }, + }, + CommandTag::CharGrid => GenericCommand { + tag: CommandTag::CharGrid, + data: CommandUnion { + char_grid: heap_clone(self.data.char_grid), + }, + }, + CommandTag::Cp437Grid => GenericCommand { + tag: CommandTag::Cp437Grid, + data: CommandUnion { + cp437_grid: heap_clone(self.data.cp437_grid), + }, + }, + CommandTag::Bitmap => GenericCommand { + tag: CommandTag::Bitmap, + data: CommandUnion { + bitmap: heap_clone(self.data.bitmap), + }, + }, + CommandTag::GlobalBrightness => GenericCommand { + tag: CommandTag::GlobalBrightness, + data: CommandUnion { + global_brightness: heap_clone( + self.data.global_brightness, + ), + }, + }, + CommandTag::BrightnessGrid => GenericCommand { + tag: CommandTag::BrightnessGrid, + data: CommandUnion { + brightness_grid: heap_clone(self.data.brightness_grid), + }, + }, + CommandTag::BitVec => GenericCommand { + tag: CommandTag::BitVec, + data: CommandUnion { + bit_vec: heap_clone(self.data.bit_vec), + }, + }, + CommandTag::HardReset => GenericCommand { + tag: CommandTag::HardReset, + data: CommandUnion { + hard_reset: heap_clone(self.data.hard_reset), + }, + }, + CommandTag::FadeOut => GenericCommand { + tag: CommandTag::FadeOut, + data: CommandUnion { + fade_out: heap_clone(self.data.fade_out), + }, + }, + #[allow(deprecated)] + CommandTag::BitmapLegacy => GenericCommand { + tag: CommandTag::BitmapLegacy, + data: CommandUnion { + bitmap_legacy: heap_clone(self.data.bitmap_legacy), + }, + }, + CommandTag::Invalid => GenericCommand::INVALID, + } + } + } +} + +derive_free!(GenericCommand); + +impl Drop for GenericCommand { + fn drop(&mut self) { + unsafe { + match self.tag { + CommandTag::Invalid => (), + CommandTag::Bitmap => heap_drop(self.data.bitmap), + CommandTag::BitVec => heap_drop(self.data.bit_vec), + CommandTag::BrightnessGrid => { + heap_drop(self.data.brightness_grid) + } + CommandTag::CharGrid => heap_drop(self.data.char_grid), + CommandTag::Cp437Grid => heap_drop(self.data.cp437_grid), + CommandTag::GlobalBrightness => { + heap_drop(self.data.global_brightness) + } + CommandTag::Clear => heap_drop(self.data.clear), + CommandTag::HardReset => heap_drop(self.data.hard_reset), + CommandTag::FadeOut => heap_drop(self.data.fade_out), + CommandTag::BitmapLegacy => heap_drop(self.data.bitmap_legacy), + } + } + } +} + +wrap_functions!(associate GenericCommand; + + /// Tries to turn a [Packet] into a [GenericCommand]. /// /// The packet is dropped in the process. /// - /// Returns: pointer to new [SPCommand] instance or NULL if parsing failed. + /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. fn try_from_packet( packet: NonNull, - ) -> SPCommand { + ) -> NonNull { let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; - servicepoint::TypedCommand::try_from(packet) + let result = servicepoint::TypedCommand::try_from(packet) .map(|value| match value { - TypedCommand::Clear(clear) => SPCommand { + TypedCommand::Clear(clear) => GenericCommand { tag: CommandTag::Clear, data: CommandUnion { clear: heap_move_nonnull(clear), }, }, - TypedCommand::CharGrid(char_grid) => SPCommand { + TypedCommand::CharGrid(char_grid) => GenericCommand { tag: CommandTag::CharGrid, data: CommandUnion { char_grid: heap_move_nonnull(char_grid), }, }, - TypedCommand::Cp437Grid(cp437_grid) => SPCommand { + TypedCommand::Cp437Grid(cp437_grid) => GenericCommand { tag: CommandTag::Cp437Grid, data: CommandUnion { cp437_grid: heap_move_nonnull(cp437_grid), }, }, - TypedCommand::Bitmap(bitmap) => SPCommand { + TypedCommand::Bitmap(bitmap) => GenericCommand { tag: CommandTag::Bitmap, data: CommandUnion { bitmap: heap_move_nonnull(bitmap), }, }, - TypedCommand::Brightness(global_brightness) => SPCommand { + TypedCommand::Brightness(global_brightness) => GenericCommand { tag: CommandTag::GlobalBrightness, data: CommandUnion { global_brightness: heap_move_nonnull(global_brightness), }, }, - TypedCommand::BrightnessGrid(brightness_grid) => SPCommand { + TypedCommand::BrightnessGrid(brightness_grid) => GenericCommand { tag: CommandTag::BrightnessGrid, data: CommandUnion { brightness_grid: heap_move_nonnull(brightness_grid), }, }, - TypedCommand::BitVec(bitvec) => SPCommand { + TypedCommand::BitVec(bitvec) => GenericCommand { tag: CommandTag::BitVec, data: CommandUnion { bit_vec: heap_move_nonnull(bitvec), }, }, - TypedCommand::HardReset(hard_reset) => SPCommand { + TypedCommand::HardReset(hard_reset) => GenericCommand { tag: CommandTag::HardReset, data: CommandUnion { hard_reset: heap_move_nonnull(hard_reset), }, }, - TypedCommand::FadeOut(fade_out) => SPCommand { + TypedCommand::FadeOut(fade_out) => GenericCommand { tag: CommandTag::FadeOut, data: CommandUnion { fade_out: heap_move_nonnull(fade_out), }, }, #[allow(deprecated)] - TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand { + TypedCommand::BitmapLegacy(bitmap_legacy) => GenericCommand { tag: CommandTag::BitmapLegacy, data: CommandUnion { bitmap_legacy: heap_move_nonnull(bitmap_legacy), }, }, }) - .unwrap_or_else(move |_| SPCommand { + .unwrap_or_else(move |_| GenericCommand { tag: CommandTag::Invalid, data: CommandUnion { null: null_mut() }, - }) + }); + heap_move_nonnull(result) } - /// Clones an [SPCommand] instance. - /// - /// returns: a new [SPCommand] instance. - fn clone(command: SPCommand) -> SPCommand { - unsafe { - match command.tag { - CommandTag::Clear => SPCommand { - tag: CommandTag::Clear, - data: CommandUnion { - clear: heap_clone(command.data.clear), - }, - }, - CommandTag::CharGrid => SPCommand { - tag: CommandTag::CharGrid, - data: CommandUnion { - char_grid: heap_clone(command.data.char_grid), - }, - }, - CommandTag::Cp437Grid => SPCommand { - tag: CommandTag::Cp437Grid, - data: CommandUnion { - cp437_grid: heap_clone(command.data.cp437_grid), - }, - }, - CommandTag::Bitmap => SPCommand { - tag: CommandTag::Bitmap, - data: CommandUnion { - bitmap: heap_clone(command.data.bitmap), - }, - }, - CommandTag::GlobalBrightness => SPCommand { - tag: CommandTag::GlobalBrightness, - data: CommandUnion { - global_brightness: heap_clone( - command.data.global_brightness, - ), - }, - }, - CommandTag::BrightnessGrid => SPCommand { - tag: CommandTag::BrightnessGrid, - data: CommandUnion { - brightness_grid: heap_clone(command.data.brightness_grid), - }, - }, - CommandTag::BitVec => SPCommand { - tag: CommandTag::BitVec, - data: CommandUnion { - bit_vec: heap_clone(command.data.bit_vec), - }, - }, - CommandTag::HardReset => SPCommand { - tag: CommandTag::HardReset, - data: CommandUnion { - hard_reset: heap_clone(command.data.hard_reset), - }, - }, - CommandTag::FadeOut => SPCommand { - tag: CommandTag::FadeOut, - data: CommandUnion { - fade_out: heap_clone(command.data.fade_out), - }, - }, - #[allow(deprecated)] - CommandTag::BitmapLegacy => SPCommand { - tag: CommandTag::BitmapLegacy, - data: CommandUnion { - bitmap_legacy: heap_clone(command.data.bitmap_legacy), - }, - }, - CommandTag::Invalid => SPCommand::INVALID, - } - } - } +); - /// Deallocates an [SPCommand]. - /// - /// Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. - /// - /// # Examples - /// - /// ```C - /// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); - /// sp_command_free(c); - /// ``` - fn free(command: SPCommand) { - unsafe { - match command.tag { - CommandTag::Invalid => (), - CommandTag::Bitmap => heap_drop(command.data.bitmap), - CommandTag::BitVec => heap_drop(command.data.bit_vec), - CommandTag::BrightnessGrid => { - heap_drop(command.data.brightness_grid) - } - CommandTag::CharGrid => heap_drop(command.data.char_grid), - CommandTag::Cp437Grid => heap_drop(command.data.cp437_grid), - CommandTag::GlobalBrightness => { - heap_drop(command.data.global_brightness) - } - CommandTag::Clear => heap_drop(command.data.clear), - CommandTag::HardReset => heap_drop(command.data.hard_reset), - CommandTag::FadeOut => heap_drop(command.data.fade_out), - CommandTag::BitmapLegacy => heap_drop(command.data.bitmap_legacy), - } - } - } - - /// Tries to turn a [SPCommand] into a [Packet]. - /// The [SPCommand] gets consumed. +wrap_methods!{GenericCommand; + /// Tries to turn a [GenericCommand] into a [Packet]. + /// The [GenericCommand] gets consumed. /// /// Returns tag [CommandTag::Invalid] in case of an error. - fn into_packet( - command: SPCommand, - ) -> *mut Packet { + move fn try_into_packet(command) -> *mut Packet { match command.tag { CommandTag::Invalid => null_mut(), CommandTag::Bitmap => { @@ -296,6 +294,5 @@ wrap_functions!(cmd_generic; heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() }) } } - } - -); + }; +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 58e5957..74d22e3 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -50,7 +50,7 @@ macro_rules! wrap_origin_accessors { macro_rules! derive_command_from { ($command:ident) => { ::paste::paste! { - impl From<::servicepoint::[< $command Command >]> for $crate::commands::SPCommand { + impl From<::servicepoint::[< $command Command >]> for $crate::commands::GenericCommand { fn from(command: ::servicepoint::[< $command Command >]) -> Self { Self { tag: $crate::commands::CommandTag::$command, @@ -67,14 +67,12 @@ macro_rules! derive_command_from { macro_rules! derive_command_into_packet { ($command_type:ident) => { ::paste::paste! { - $crate::macros::wrap_functions!(associate $command_type; + $crate::macros::wrap_method!($command_type; #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: ::core::ptr::NonNull<$command_type>, - ) -> *mut ::servicepoint::Packet { - $crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into()) + move fn try_into_packet(instance) -> *mut ::servicepoint::Packet { + $crate::mem::heap_move_ok(instance.try_into()) } ); } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 9bae852..4ce7972 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -12,7 +12,6 @@ use std::ptr::NonNull; wrap_grid!(Bitmap, bool); wrap_functions!(bitmap; - /// Creates a new [Bitmap] with the specified dimensions. /// /// # Arguments @@ -76,35 +75,26 @@ wrap_functions!(bitmap; let bitvec = unsafe { heap_remove(bitvec) }; heap_move_ok(Bitmap::from_bitvec(width, bitvec)) } +); + +wrap_methods!(Bitmap; /// Consumes the Bitmap and returns the contained BitVec. - fn into_bitvec( - bitmap: NonNull - ) -> NonNull { - let bitmap = unsafe { heap_remove(bitmap) }; + move fn into_bitvec(bitmap) -> NonNull { heap_move_nonnull(bitmap.into()) - } + }; /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. /// /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - bitmap: NonNull, - x: usize, - y: usize, - compression: CompressionCode, - ) -> *mut Packet { - let bitmap = unsafe { heap_remove(bitmap) }; + move fn try_into_packet(bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { heap_move_ok(Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), compression, })) - } -); - -wrap_methods!(Bitmap; + }; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 2a76cf7..bc5da06 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,7 +1,7 @@ use crate::{ containers::{wrap_container, ByteSlice}, macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -35,29 +35,27 @@ wrap_functions!(associate DisplayBitVec; heap_move_nonnull(DisplayBitVec::from_slice(data)) } +); + +wrap_methods!(DisplayBitVec; /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. /// /// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - bitvec: NonNull, + move fn try_into_packet( + bitvec, offset: usize, operation: BinaryOperation, - compression: CompressionCode, + compression: CompressionCode ) -> *mut Packet { - let bitvec = unsafe { heap_remove(bitvec) }; heap_move_ok(Packet::try_from(BitVecCommand { bitvec, offset, operation, compression, })) - } - -); - -wrap_methods!(DisplayBitVec; + }; /// Gets the value of a bit. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a5d01f2..7c27d5c 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,7 +1,7 @@ use crate::{ 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}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -54,26 +54,21 @@ wrap_functions!(associate BrightnessGrid; ) } +); + +wrap_methods!(BrightnessGrid; /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. /// /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - grid: NonNull, - x: usize, - y: usize, - ) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; + move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), })) - } + }; -); - -wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 1a59fdd..1966f4a 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,7 +1,7 @@ use crate::{ 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}, + mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -23,41 +23,18 @@ wrap_functions!(associate CharGrid; /// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_free(grid); /// ``` - fn new( - width: usize, - height: usize, - ) -> NonNull { + fn new(width: usize, height: usize) -> NonNull { heap_move_nonnull(CharGrid::new(width, height)) } /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load( - width: usize, - height: usize, - data: ByteSlice, - ) -> *mut CharGrid { + fn load(width: usize, height: usize, data: ByteSlice) -> *mut CharGrid { let data = unsafe { data.as_slice() }; heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) } - /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. - /// - /// The provided [CharGrid] gets consumed. - /// - /// Returns NULL in case of an error. - fn try_into_packet( - grid: NonNull, - x: usize, - y: usize, - ) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(CharGridCommand { - grid, - origin: Origin::new(x, y), - })) - } ); @@ -102,4 +79,16 @@ wrap_methods!(CharGrid; mut fn fill(instance, value: u32) { instance.fill(char::from_u32(value).unwrap()) }; + + /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [CharGrid] gets consumed. + /// + /// Returns NULL in case of an error. + move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + heap_move_ok(Packet::try_from(CharGridCommand { + grid, + origin: Origin::new(x, y), + })) + }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index fc31d13..38efc58 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,7 +1,7 @@ use crate::{ 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}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, @@ -32,26 +32,21 @@ wrap_functions!(cp437grid; heap_move_some(Cp437Grid::load(width, height, data)) } +); + +wrap_methods!(Cp437Grid; /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. /// /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - grid: NonNull, - x: usize, - y: usize, - ) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; + move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), })) - } + }; -); - -wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. diff --git a/src/macros.rs b/src/macros.rs index e0560f5..768254b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -36,6 +36,12 @@ macro_rules! nonnull_as_mut { }; } +macro_rules! nonnull_as_move { + ($ident:ident) => { + $crate::mem::heap_remove($ident) + }; +} + macro_rules! wrap_method { ( $object_type:ident; @@ -229,6 +235,7 @@ macro_rules! wrap_functions { } pub(crate) use { - derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, - wrap_functions, wrap_methods, wrap_fields_accessor, wrap_method + derive_clone, derive_free, nonnull_as_move, nonnull_as_mut, nonnull_as_ref, + wrap_fields, wrap_fields_accessor, wrap_functions, wrap_method, + wrap_methods, }; diff --git a/src/udp.rs b/src/udp.rs index 321c4d1..7889937 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,6 @@ use crate::{ - commands::{CommandTag, SPCommand}, - macros::{derive_free, wrap_functions}, + commands::{CommandTag, GenericCommand}, + macros::{derive_free, wrap_functions, wrap_methods}, mem::{heap_move_ok, heap_remove}, }; use servicepoint::{Header, Packet, UdpSocketExt}; @@ -49,17 +49,20 @@ wrap_functions!(associate UdpSocket; heap_move_ok(UdpSocket::bind_connect(addr)) } +); + +wrap_methods! {UdpSocket; /// Sends a [Packet] to the display using the [UdpSocket]. /// /// The passed `packet` gets consumed. /// /// returns: true in case of success - fn send_packet(connection: NonNull, packet: NonNull) -> bool { + ref fn send_packet(connection, packet: NonNull) -> bool { let packet = unsafe { heap_remove(packet) }; - unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok() - } + connection.send(&Vec::from(packet)).is_ok() + }; - /// Sends a [SPCommand] to the display using the [UdpSocket]. + /// Sends a [GenericCommand] to the display using the [UdpSocket]. /// /// The passed `command` gets consumed. /// @@ -70,44 +73,26 @@ wrap_functions!(associate UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - fn send_command(connection: NonNull, command: SPCommand) -> bool { + ref fn send_command(connection, command: NonNull) -> bool { unsafe { - match command.tag { + let command = crate::macros::nonnull_as_mut!(command); + let result = match command.tag { CommandTag::Invalid => return false, - CommandTag::Bitmap => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap)), - CommandTag::BitVec => connection - .as_ref() - .send_command(heap_remove(command.data.bit_vec)), - CommandTag::BrightnessGrid => connection - .as_ref() - .send_command(heap_remove(command.data.brightness_grid)), - CommandTag::CharGrid => connection - .as_ref() - .send_command(heap_remove(command.data.char_grid)), - CommandTag::Cp437Grid => connection - .as_ref() - .send_command(heap_remove(command.data.cp437_grid)), - CommandTag::GlobalBrightness => connection - .as_ref() - .send_command(heap_remove(command.data.global_brightness)), - CommandTag::Clear => connection - .as_ref() - .send_command(heap_remove(command.data.clear)), - CommandTag::HardReset => connection - .as_ref() - .send_command(heap_remove(command.data.hard_reset)), - CommandTag::FadeOut => connection - .as_ref() - .send_command(heap_remove(command.data.fade_out)), - CommandTag::BitmapLegacy => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap_legacy)), - } + CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)), + CommandTag::BitVec => connection.send_command(heap_remove(command.data.bit_vec)), + CommandTag::BrightnessGrid => connection.send_command(heap_remove(command.data.brightness_grid)), + CommandTag::CharGrid => connection.send_command(heap_remove(command.data.char_grid)), + CommandTag::Cp437Grid => connection.send_command(heap_remove(command.data.cp437_grid)), + CommandTag::GlobalBrightness => connection.send_command(heap_remove(command.data.global_brightness)), + CommandTag::Clear => connection.send_command(heap_remove(command.data.clear)), + CommandTag::HardReset => connection.send_command(heap_remove(command.data.hard_reset)), + CommandTag::FadeOut => connection.send_command(heap_remove(command.data.fade_out)), + CommandTag::BitmapLegacy => connection.send_command(heap_remove(command.data.bitmap_legacy)), + }.is_some(); + *command = GenericCommand::INVALID; + result } - .is_some() - } + }; /// Sends a [Header] to the display using the [UdpSocket]. /// @@ -118,17 +103,14 @@ wrap_functions!(associate UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - fn send_header(udp_connection: NonNull, header: Header) -> bool { + ref fn send_header(udp_connection, header: Header) -> bool { let packet = Packet { header, payload: None, }; - unsafe { udp_connection.as_ref() } - .send(&Vec::from(packet)) - .is_ok() - } - -); + udp_connection.send(&Vec::from(packet)).is_ok() + }; +} mod _hidden { /// This is a type only used by cbindgen to have a type for pointers. From 5a849a87c7bb608ba0dcabbe2e82dd702329eee7 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 21:16:22 +0200 Subject: [PATCH 12/17] fix missing renames --- include/servicepoint.h | 84 +++++++++++++++--------------- src/commands/char_grid_command.rs | 2 +- src/commands/cp437_grid_command.rs | 2 +- src/commands/generic_command.rs | 11 ++-- src/containers/bitmap.rs | 2 +- src/containers/cp437_grid.rs | 15 ++---- 6 files changed, 53 insertions(+), 63 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 095c6cc..2648383 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1405,6 +1405,14 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridComm */ void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); +/** + * Moves the provided [CharGrid] into a new [CharGridCommand], + * leaving other fields as their default values. + * + * This function is part of the `chargridcommand` module. + */ +struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*notnull*/ grid); + /** * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. * @@ -1424,6 +1432,19 @@ void sp_chargridcommand_get_origin(struct CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); +/** + * Show UTF-8 encoded text on the screen. + * + * The passed [CharGrid] gets consumed. + * + * Returns: a new [CharGridCommand] instance. + * + * This function is part of the `chargridcommand` module. + */ +struct CharGridCommand */*notnull*/ sp_chargridcommand_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + /** * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. @@ -1489,48 +1510,6 @@ struct ClearCommand */*notnull*/ sp_clearcommand_new(void); */ struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ instance); -/** - * Moves the provided [CharGrid] into a new [CharGridCommand], - * leaving other fields as their default values. - * - * This function is part of the `cmd_chargrid` module. - */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_from_grid(CharGrid */*notnull*/ grid); - -/** - * Show UTF-8 encoded text on the screen. - * - * The passed [CharGrid] gets consumed. - * - * Returns: a new [CharGridCommand] instance. - * - * This function is part of the `cmd_chargrid` module. - */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - -/** - * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], - * leaving other fields as their default values. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_from_grid(Cp437Grid */*notnull*/ grid); - -/** - * Show text on the screen. - * - * The text is sent in the form of a 2D grid of [CP-437] encoded characters. - * - * The origin is relative to the top-left of the display. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - /** *Clones a [`Cp437Grid`] instance. * @@ -1672,6 +1651,14 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridC */ void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); +/** + * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], + * leaving other fields as their default values. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */*notnull*/ grid); + /** * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. * @@ -1691,6 +1678,19 @@ void sp_cp437gridcommand_get_origin(struct Cp437GridCommand */*notnull*/ command size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); +/** + * Show text on the screen. + * + * The text is sent in the form of a 2D grid of [CP-437] encoded characters. + * + * The origin is relative to the top-left of the display. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + /** * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index f8d5107..41e5000 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -14,7 +14,7 @@ wrap_fields!(CharGridCommand; wrap_origin_accessors!(CharGridCommand); -wrap_functions!(cmd_chargrid; +wrap_functions!(associate CharGridCommand; /// Show UTF-8 encoded text on the screen. /// diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 4977195..b43afb4 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -14,7 +14,7 @@ wrap_fields!(Cp437GridCommand; wrap_origin_accessors!(Cp437GridCommand); -wrap_functions!(cmd_cp437grid; +wrap_functions!(associate Cp437GridCommand; /// Show text on the screen. /// diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 609daac..d288a27 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{derive_clone, derive_free, wrap_functions}, + macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{ heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, heap_remove, @@ -11,7 +11,6 @@ use servicepoint::{ HardResetCommand, Packet, TypedCommand, }; use std::ptr::{null_mut, NonNull}; -use crate::macros::wrap_methods; /// Pointer to one of the available command structs. #[repr(C)] @@ -168,11 +167,12 @@ impl Drop for GenericCommand { CommandTag::BitmapLegacy => heap_drop(self.data.bitmap_legacy), } } + + *self = Self::INVALID; } } wrap_functions!(associate GenericCommand; - /// Tries to turn a [Packet] into a [GenericCommand]. /// /// The packet is dropped in the process. @@ -181,7 +181,7 @@ wrap_functions!(associate GenericCommand; fn try_from_packet( packet: NonNull, ) -> NonNull { - let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; + let packet = unsafe { heap_remove(packet) }; let result = servicepoint::TypedCommand::try_from(packet) .map(|value| match value { TypedCommand::Clear(clear) => GenericCommand { @@ -252,10 +252,9 @@ wrap_functions!(associate GenericCommand; }); heap_move_nonnull(result) } - ); -wrap_methods!{GenericCommand; +wrap_methods! { GenericCommand; /// Tries to turn a [GenericCommand] into a [Packet]. /// The [GenericCommand] gets consumed. /// diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 4ce7972..738925b 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -11,7 +11,7 @@ use std::ptr::NonNull; wrap_grid!(Bitmap, bool); -wrap_functions!(bitmap; +wrap_functions!(associate Bitmap; /// Creates a new [Bitmap] with the specified dimensions. /// /// # Arguments diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 38efc58..8905610 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -10,28 +10,19 @@ use std::ptr::NonNull; wrap_grid!(Cp437Grid, u8); -wrap_functions!(cp437grid; - +wrap_functions!(associate Cp437Grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// /// returns: [Cp437Grid] initialized to 0. - fn new( - width: usize, - height: usize, - ) -> NonNull { + fn new(width: usize, height: usize) -> NonNull { heap_move_nonnull(Cp437Grid::new(width, height)) } /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load( - width: usize, - height: usize, - data: ByteSlice, - ) -> *mut Cp437Grid { + fn load(width: usize, height: usize, data: ByteSlice) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; heap_move_some(Cp437Grid::load(width, height, data)) } - ); wrap_methods!(Cp437Grid; From 664625402f88bac5aeb61b649b1f64ef55b3c0d3 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 21:36:01 +0200 Subject: [PATCH 13/17] move where the modifier is --- src/commands/generic_command.rs | 2 +- src/commands/mod.rs | 2 +- src/containers/bitmap.rs | 6 +++--- src/containers/bitvec.rs | 16 ++++++++-------- src/containers/brightness_grid.rs | 4 ++-- src/containers/char_grid.rs | 8 ++++---- src/containers/cp437_grid.rs | 4 ++-- src/containers/mod.rs | 10 +++++----- src/macros.rs | 10 +++++----- src/udp.rs | 6 +++--- 10 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index d288a27..dad7584 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -259,7 +259,7 @@ wrap_methods! { GenericCommand; /// The [GenericCommand] gets consumed. /// /// Returns tag [CommandTag::Invalid] in case of an error. - move fn try_into_packet(command) -> *mut Packet { + fn try_into_packet(move command) -> *mut Packet { match command.tag { CommandTag::Invalid => null_mut(), CommandTag::Bitmap => { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 74d22e3..026d0f1 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -71,7 +71,7 @@ macro_rules! derive_command_into_packet { #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. - move fn try_into_packet(instance) -> *mut ::servicepoint::Packet { + fn try_into_packet(move instance) -> *mut ::servicepoint::Packet { $crate::mem::heap_move_ok(instance.try_into()) } ); diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 738925b..4fb55a2 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -79,7 +79,7 @@ wrap_functions!(associate Bitmap; wrap_methods!(Bitmap; /// Consumes the Bitmap and returns the contained BitVec. - move fn into_bitvec(bitmap) -> NonNull { + fn into_bitvec(move bitmap) -> NonNull { heap_move_nonnull(bitmap.into()) }; @@ -88,7 +88,7 @@ wrap_methods!(Bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { + fn try_into_packet(move bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { heap_move_ok(Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), @@ -99,7 +99,7 @@ wrap_methods!(Bitmap; /// 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(instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index bc5da06..6ae2cf6 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -43,8 +43,8 @@ wrap_methods!(DisplayBitVec; /// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet( - bitvec, + fn try_into_packet( + move bitvec, offset: usize, operation: BinaryOperation, compression: CompressionCode @@ -69,7 +69,7 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - ref fn get(instance, index: usize) -> bool { + fn get(ref instance, index: usize) -> bool { instance.get(index).map(|x| *x).unwrap_or(false) }; @@ -83,25 +83,25 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - mut fn set(instance, index: usize, value: bool); + fn set(mut instance, index: usize, value: bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - mut fn fill(instance, value: bool); + fn fill(mut instance, value: bool); /// Gets the length in bits. - ref fn len(instance) -> usize; + fn len(ref instance) -> usize; /// Returns true if length is 0. - ref fn is_empty(instance) -> bool; + fn is_empty(ref instance) -> 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(instance) -> ByteSlice { + fn as_raw_mut_slice(mut instance) -> ByteSlice { unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } }; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 7c27d5c..a71a953 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -62,7 +62,7 @@ wrap_methods!(BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), @@ -72,7 +72,7 @@ wrap_methods!(BrightnessGrid; /// 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(instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> ByteSlice { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 1966f4a..a360a26 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -49,7 +49,7 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(instance, x: usize, y: usize) -> u32 { + fn get(ref instance, x: usize, y: usize) -> u32 { instance.get(x, y) as u32 }; @@ -66,7 +66,7 @@ wrap_methods!(CharGrid; /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - mut fn set(instance, x: usize, y: usize, value: u32) { + fn set(mut instance, x: usize, y: usize, value: u32) { instance.set(x, y, char::from_u32(value).unwrap()) }; @@ -76,7 +76,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - mut fn fill(instance, value: u32) { + fn fill(mut instance, value: u32) { instance.fill(char::from_u32(value).unwrap()) }; @@ -85,7 +85,7 @@ wrap_methods!(CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 8905610..d4ed8a1 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -31,7 +31,7 @@ wrap_methods!(Cp437Grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), @@ -41,7 +41,7 @@ wrap_methods!(Cp437Grid; /// 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(instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/mod.rs b/src/containers/mod.rs index fcd76a1..0a9eb38 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -23,10 +23,10 @@ macro_rules! derive_get_width_height { ($object_type:ident) => { $crate::macros::wrap_methods! {$object_type; /// Gets the width. - ref fn width(instance) -> usize; + fn width(ref instance) -> usize; /// Gets the height. - ref fn height(instance) -> usize; + fn height(ref instance) -> usize; } }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(instance, x: usize, y: usize) -> $value_type; + fn get(ref instance, x: usize, y: usize) -> $value_type; /// Sets the value of the specified position. /// @@ -57,14 +57,14 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - mut fn set(instance, x: usize, y: usize, value: $value_type); + fn set(mut instance, 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(instance, value: $value_type); + fn fill(mut instance, value: $value_type); } }; } diff --git a/src/macros.rs b/src/macros.rs index 768254b..d2b974c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -46,14 +46,14 @@ macro_rules! wrap_method { ( $object_type:ident; $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? ) => { ::paste::paste!{ $crate::macros::wrap_method!( $object_type; $(#[$meta])+ - $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) $(-> $return_type)? { $instance.$function($($($param_name),*)?) } @@ -62,7 +62,7 @@ macro_rules! wrap_method { }; ($object_type:ident; $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? $impl:block ) => { @@ -88,7 +88,7 @@ macro_rules! wrap_methods { $object_type:ident; $( $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? $($impl:block)?; )+ @@ -97,7 +97,7 @@ macro_rules! wrap_methods { $( $crate::macros::wrap_method!($object_type; $(#[$meta])* - $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) $(-> $return_type)? $($impl)? ); diff --git a/src/udp.rs b/src/udp.rs index 7889937..aa307b0 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -57,7 +57,7 @@ wrap_methods! {UdpSocket; /// The passed `packet` gets consumed. /// /// returns: true in case of success - ref fn send_packet(connection, packet: NonNull) -> bool { + fn send_packet(ref connection, packet: NonNull) -> bool { let packet = unsafe { heap_remove(packet) }; connection.send(&Vec::from(packet)).is_ok() }; @@ -73,7 +73,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - ref fn send_command(connection, command: NonNull) -> bool { + fn send_command(ref connection, command: NonNull) -> bool { unsafe { let command = crate::macros::nonnull_as_mut!(command); let result = match command.tag { @@ -103,7 +103,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - ref fn send_header(udp_connection, header: Header) -> bool { + fn send_header(ref udp_connection, header: Header) -> bool { let packet = Packet { header, payload: None, From e8f11c08eab0da20e6fd7194c184222ceecd8d47 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 23:28:30 +0200 Subject: [PATCH 14/17] add parameter modifiers --- src/commands/bitmap_command.rs | 18 ++- src/commands/bitvec_command.rs | 12 +- src/commands/brightness_grid_command.rs | 16 ++- src/commands/char_grid_command.rs | 16 ++- src/commands/cp437_grid_command.rs | 16 ++- src/commands/generic_command.rs | 5 +- src/commands/global_brightness_command.rs | 2 +- src/commands/mod.rs | 31 ++--- src/containers/bitmap.rs | 19 ++- src/containers/bitvec.rs | 18 +-- src/containers/brightness_grid.rs | 15 +-- src/containers/char_grid.rs | 14 +-- src/containers/cp437_grid.rs | 8 +- src/containers/mod.rs | 10 +- src/macros.rs | 138 ++++++++++++---------- src/packet.rs | 94 +++++++-------- src/udp.rs | 12 +- 17 files changed, 214 insertions(+), 230 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 01e3620..5aaab26 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; @@ -22,13 +22,13 @@ wrap_functions!(associate BitmapCommand; /// /// Returns: a new [BitmapCommand] instance. fn new( - bitmap: NonNull, - origin_x: usize, - origin_y: usize, - compression: CompressionCode, + bitmap: move NonNull, + origin_x: val usize, + origin_y: val usize, + compression: val CompressionCode, ) -> NonNull { heap_move_nonnull(BitmapCommand { - bitmap: unsafe { heap_remove(bitmap) }, + bitmap, origin: Origin::new(origin_x, origin_y), compression, }) @@ -38,9 +38,7 @@ wrap_functions!(associate BitmapCommand; /// leaving other fields as their default values. /// /// Rust equivalent: `BitmapCommand::from(bitmap)` - fn from_bitmap( - bitmap: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) + fn from_bitmap(bitmap: move NonNull) -> NonNull { + heap_move_nonnull(bitmap.into()) } ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index e796380..4dd9ca9 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, @@ -32,13 +32,13 @@ wrap_functions!(associate BitVecCommand; /// /// The contained [`DisplayBitVec`] is always uncompressed. fn new( - bitvec: NonNull, - offset: usize, - operation: BinaryOperation, - compression: CompressionCode, + bitvec: move NonNull, + offset: val usize, + operation: val BinaryOperation, + compression: val CompressionCode, ) -> NonNull { heap_move_nonnull(BitVecCommand { - bitvec: unsafe { heap_remove(bitvec) }, + bitvec, offset, operation, compression, diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 853a3fe..f05b820 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; @@ -22,22 +22,20 @@ wrap_functions!(associate BrightnessGridCommand; /// /// Returns: a new [BrightnessGridCommand] instance. fn new( - grid: NonNull, - origin_x: usize, - origin_y: usize, + grid: move NonNull, + origin_x: val usize, + origin_y: val usize ) -> NonNull { heap_move_nonnull(BrightnessGridCommand { - grid: unsafe { heap_remove(grid) }, + grid, origin: Origin::new(origin_x, origin_y), }) } /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], /// leaving other fields as their default values. - fn from_grid( - grid: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + fn from_grid(grid: move NonNull) -> NonNull { + heap_move_nonnull(grid.into()) } ); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 41e5000..12b58ca 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; @@ -22,22 +22,20 @@ wrap_functions!(associate CharGridCommand; /// /// Returns: a new [CharGridCommand] instance. fn new( - grid: NonNull, - origin_x: usize, - origin_y: usize, + grid: move NonNull, + origin_x: val usize, + origin_y: val usize, ) -> NonNull { heap_move_nonnull(CharGridCommand { - grid: unsafe { heap_remove(grid) }, + grid, origin: Origin::new(origin_x, origin_y), }) } /// Moves the provided [CharGrid] into a new [CharGridCommand], /// leaving other fields as their default values. - fn from_grid( - grid: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + fn from_grid(grid: move NonNull) -> NonNull { + heap_move_nonnull(grid.into()) } ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index b43afb4..921e2a4 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; @@ -22,22 +22,20 @@ wrap_functions!(associate Cp437GridCommand; /// /// The origin is relative to the top-left of the display. fn new( - grid: NonNull, - origin_x: usize, - origin_y: usize, + grid: move NonNull, + origin_x: val usize, + origin_y: val usize, ) -> NonNull { heap_move_nonnull(Cp437GridCommand { - grid: unsafe { heap_remove(grid) }, + grid, origin: Origin::new(origin_x, origin_y), }) } /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// leaving other fields as their default values. - fn from_grid( - grid: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + fn from_grid(grid: move NonNull) -> NonNull { + heap_move_nonnull(grid.into()) } ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index dad7584..efd402c 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -178,10 +178,7 @@ wrap_functions!(associate GenericCommand; /// The packet is dropped in the process. /// /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. - fn try_from_packet( - packet: NonNull, - ) -> NonNull { - let packet = unsafe { heap_remove(packet) }; + fn try_from_packet(packet: move NonNull) -> NonNull { let result = servicepoint::TypedCommand::try_from(packet) .map(|value| match value { TypedCommand::Clear(clear) => GenericCommand { diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 76cbeb5..d2fef10 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -11,7 +11,7 @@ wrap_functions!(associate GlobalBrightnessCommand; /// Set the brightness of all tiles to the same value. /// /// Returns: a new [GlobalBrightnessCommand] instance. - fn new(brightness: Brightness) -> NonNull { + fn new(brightness: val Brightness) -> NonNull { heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 026d0f1..da16c18 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -17,31 +17,22 @@ pub use generic_command::*; macro_rules! wrap_origin_accessors { ( $object_type:ident ) => { ::paste::paste! { - $crate::macros::wrap_functions!(associate $object_type; + $crate::macros::wrap_methods!($object_type; #[doc = " Reads the origin field of the [`" $object_type "`]."] fn get_origin( - command: ::core::ptr::NonNull<$object_type>, - origin_x: ::core::ptr::NonNull, - origin_y: ::core::ptr::NonNull, + ref command, + origin_x: mut ::core::ptr::NonNull, + origin_y: mut ::core::ptr::NonNull ) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; - } - } + let origin = command.origin; + *origin_x = origin.x; + *origin_y = origin.y; + }; #[doc = " Overwrites the origin field of the [`" $object_type "`]."] - fn set_origin( - command: ::core::ptr::NonNull<$object_type>, - origin_x: usize, - origin_y: usize, - ) { - unsafe { - $crate::macros::nonnull_as_mut!(command).origin = - ::servicepoint::Origin::new(origin_x, origin_y); - } - } + fn set_origin(mut command, origin_x: val usize, origin_y: val usize) { + command.origin = ::servicepoint::Origin::new(origin_x, origin_y); + }; ); } }; diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 4fb55a2..1386a8d 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,7 +1,7 @@ use crate::{ 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}, + macros::{wrap_functions, wrap_methods}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, @@ -35,7 +35,7 @@ wrap_functions!(associate Bitmap; /// sp_bitmap_set(grid, 0, 0, false); /// sp_bitmap_free(grid); /// ``` - fn new(width: usize, height: usize) -> *mut Bitmap { + fn new(width: val usize, height: val usize) -> *mut Bitmap { heap_move_some(Bitmap::new(width, height)) } @@ -55,9 +55,9 @@ wrap_functions!(associate Bitmap; /// /// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. fn load( - width: usize, - height: usize, - data: ByteSlice, + width: val usize, + height: val usize, + data: val ByteSlice, ) -> *mut Bitmap { let data = unsafe { data.as_slice() }; heap_move_ok(Bitmap::load(width, height, data)) @@ -69,10 +69,9 @@ wrap_functions!(associate Bitmap; /// /// Returns NULL in case of error. fn from_bitvec( - width: usize, - bitvec: NonNull, + width: val usize, + bitvec: move NonNull, ) -> *mut Bitmap { - let bitvec = unsafe { heap_remove(bitvec) }; heap_move_ok(Bitmap::from_bitvec(width, bitvec)) } ); @@ -88,7 +87,7 @@ wrap_methods!(Bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { + fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> *mut Packet { heap_move_ok(Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 6ae2cf6..cdb7c36 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_container, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{ @@ -23,14 +23,14 @@ wrap_functions!(associate DisplayBitVec; /// # Panics /// /// - when `size` is not divisible by 8. - fn new(size: usize) -> NonNull { + fn new(size: val usize) -> NonNull { heap_move_nonnull(DisplayBitVec::repeat(false, size)) } /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// /// returns: [DisplayBitVec] instance containing data. - fn load(data: ByteSlice) -> NonNull { + fn load(data: val ByteSlice) -> NonNull { let data = unsafe { data.as_slice() }; heap_move_nonnull(DisplayBitVec::from_slice(data)) } @@ -45,9 +45,9 @@ wrap_methods!(DisplayBitVec; /// Returns NULL in case of an error. fn try_into_packet( move bitvec, - offset: usize, - operation: BinaryOperation, - compression: CompressionCode + offset: val usize, + operation: val BinaryOperation, + compression: val CompressionCode ) -> *mut Packet { heap_move_ok(Packet::try_from(BitVecCommand { bitvec, @@ -69,7 +69,7 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn get(ref instance, index: usize) -> bool { + fn get(ref instance, index: val usize) -> bool { instance.get(index).map(|x| *x).unwrap_or(false) }; @@ -83,14 +83,14 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn set(mut instance, index: usize, value: bool); + fn set(mut instance, index: val usize, value: val bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - fn fill(mut instance, value: bool); + fn fill(mut instance, value: val bool); /// Gets the length in bits. fn len(ref instance) -> usize; diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a71a953..b0da48a 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ @@ -30,10 +30,7 @@ wrap_functions!(associate BrightnessGrid; /// TypedCommand *command = sp_command_char_brightness(grid); /// sp_udp_free(connection); /// ``` - fn new( - width: usize, - height: usize, - ) -> NonNull { + fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(BrightnessGrid::new(width, height)) } @@ -43,9 +40,9 @@ wrap_functions!(associate BrightnessGrid; /// /// returns: new [BrightnessGrid] instance, or NULL in case of an error. fn load( - width: usize, - height: usize, - data: ByteSlice, + width: val usize, + height: val usize, + data: val ByteSlice, ) -> *mut BrightnessGrid { let data = unsafe { data.as_slice() }; heap_move_some( @@ -62,7 +59,7 @@ wrap_methods!(BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { heap_move_ok(Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index a360a26..715588f 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; @@ -23,14 +23,14 @@ wrap_functions!(associate CharGrid; /// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_free(grid); /// ``` - fn new(width: usize, height: usize) -> NonNull { + fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(CharGrid::new(width, height)) } /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load(width: usize, height: usize, data: ByteSlice) -> *mut CharGrid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid { let data = unsafe { data.as_slice() }; heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) } @@ -49,7 +49,7 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: usize, y: usize) -> u32 { + fn get(ref instance, x: val usize, y: val usize) -> u32 { instance.get(x, y) as u32 }; @@ -66,7 +66,7 @@ wrap_methods!(CharGrid; /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - fn set(mut instance, x: usize, y: usize, value: u32) { + fn set(mut instance, x: val usize, y: val usize, value: val u32) { instance.set(x, y, char::from_u32(value).unwrap()) }; @@ -76,7 +76,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - fn fill(mut instance, value: u32) { + fn fill(mut instance, value: val u32) { instance.fill(char::from_u32(value).unwrap()) }; @@ -85,7 +85,7 @@ wrap_methods!(CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { heap_move_ok(Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index d4ed8a1..ecc36c6 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ @@ -14,12 +14,12 @@ wrap_functions!(associate Cp437Grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// /// returns: [Cp437Grid] initialized to 0. - fn new(width: usize, height: usize) -> NonNull { + fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(Cp437Grid::new(width, height)) } /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load(width: usize, height: usize, data: ByteSlice) -> *mut Cp437Grid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; heap_move_some(Cp437Grid::load(width, height, data)) } @@ -31,7 +31,7 @@ wrap_methods!(Cp437Grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { heap_move_ok(Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 0a9eb38..35bbe3b 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -14,8 +14,8 @@ pub use cp437_grid::*; macro_rules! wrap_container { ($object_type:ident) => { - derive_clone!($object_type); - derive_free!($object_type); + $crate::macros::derive_clone!($object_type); + $crate::macros::derive_free!($object_type); }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: usize, y: usize) -> $value_type; + fn get(ref instance, x: val usize, y: val usize) -> $value_type; /// Sets the value of the specified position. /// @@ -57,14 +57,14 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn set(mut instance, x: usize, y: usize, value: $value_type); + fn set(mut instance, x: val usize, y: val usize, value: val $value_type); /// Sets the state of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to - fn fill(mut instance, value: $value_type); + fn fill(mut instance, value: val $value_type); } }; } diff --git a/src/macros.rs b/src/macros.rs index d2b974c..8cae819 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,8 +3,9 @@ macro_rules! derive_free { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Deallocates a [`" $typ "`] instance."] - fn free(instance: ::core::ptr::NonNull<$typ>) { - unsafe { $crate::mem::heap_drop(instance) } + #[allow(dropping_copy_types)] + fn free(instance: move ::core::ptr::NonNull<$typ>) { + ::std::mem::drop(instance) } ); } @@ -16,8 +17,8 @@ macro_rules! derive_clone { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Clones a [`" $typ "`] instance."] - fn clone(instance: ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { - unsafe { $crate::mem::heap_clone(instance) } + fn clone(instance: ref ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { + $crate::mem::heap_move_nonnull(instance.clone()) } ); } @@ -36,24 +37,18 @@ macro_rules! nonnull_as_mut { }; } -macro_rules! nonnull_as_move { - ($ident:ident) => { - $crate::mem::heap_remove($ident) - }; -} - macro_rules! wrap_method { ( $object_type:ident; $(#[$meta:meta])+ - fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? ) => { ::paste::paste!{ $crate::macros::wrap_method!( $object_type; $(#[$meta])+ - fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? { $instance.$function($($($param_name),*)?) } @@ -62,7 +57,7 @@ macro_rules! wrap_method { }; ($object_type:ident; $(#[$meta:meta])+ - fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? $impl:block ) => { @@ -72,12 +67,10 @@ macro_rules! wrap_method { /// $(#[$meta])* fn $function( - $instance: ::core::ptr::NonNull<$object_type> - $(,$($param_name: $param_type),*)? - ) $(-> $return_type)? { - let $instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !($instance) }; - $impl - } + $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> + $(,$($param_name: $param_modifier $param_type),*)? + ) $(-> $return_type)? + $impl ); } }; @@ -88,7 +81,7 @@ macro_rules! wrap_methods { $object_type:ident; $( $(#[$meta:meta])+ - fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? $($impl:block)?; )+ @@ -97,7 +90,7 @@ macro_rules! wrap_methods { $( $crate::macros::wrap_method!($object_type; $(#[$meta])* - fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? $($impl)? ); @@ -109,45 +102,35 @@ macro_rules! wrap_methods { macro_rules! wrap_fields_accessor { (get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - fn []( - instance: ::core::ptr::NonNull<$object_type> - ) -> $prop_type { - let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name }; - return $prop_name; + fn [](ref instance) -> $prop_type { + return instance.$prop_name; } } } }; (mut get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Gets a reference to the field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] /// /// - The returned reference inherits the lifetime of object in which it is contained. /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - fn []( - instance: ::core::ptr::NonNull<$object_type> - ) -> ::core::ptr::NonNull<$prop_type> { - let $prop_name = unsafe { &mut $crate::macros::nonnull_as_mut!(instance).$prop_name }; - return ::core::ptr::NonNull::from($prop_name); + fn [](mut instance) -> ::core::ptr::NonNull<$prop_type> { + return ::core::ptr::NonNull::from(&mut instance.$prop_name); } } } }; (set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Sets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - fn []( - instance: ::core::ptr::NonNull<$object_type>, - value: $prop_type, - ) { - let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; + fn [](mut instance, value: val $prop_type) { instance.$prop_name = value; } } @@ -155,17 +138,12 @@ macro_rules! wrap_fields_accessor { }; (move set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Sets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] /// The provided value is moved into the instance, potentially invalidating previously taken references. - fn []( - instance: ::core::ptr::NonNull<$object_type>, - value: ::core::ptr::NonNull<$prop_type>, - ) { - let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; - let $prop_name = unsafe { $crate::mem::heap_remove(value) }; - instance.$prop_name = $prop_name; + fn [](mut instance, value: move ::core::ptr::NonNull<$prop_type>) { + instance.$prop_name = value; } } } @@ -190,18 +168,30 @@ macro_rules! wrap_fields { }; } -macro_rules! wrap_functions { +macro_rules! apply_param_modifier { + (move, $param_name:ident) => { + unsafe { $crate::mem::heap_remove($param_name) } + }; + (val, $param_name:ident) => { + $param_name + }; + (mut, $param_name:ident) => { + unsafe { $crate::macros::nonnull_as_mut!($param_name) } + }; + (ref, $param_name:ident) => { + unsafe { $crate::macros::nonnull_as_ref!($param_name) } + }; +} + +macro_rules! wrap_function { ( $module:ident; - $( - $(#[$meta:meta])+ - fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) - $(-> $return_type:ty)? - $block:block - )+ + $(#[$meta:meta])+ + fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) + $(-> $return_type:ty)? + $block:block ) => { ::paste::paste! { - $( $(#[$meta])* #[doc = ""] #[doc = " This function is part of the `" $module "` module."] @@ -209,7 +199,33 @@ macro_rules! wrap_functions { pub unsafe extern "C" fn [< sp_ $module _ $function >]( $($param_name: $param_type),* ) $(-> $return_type)? - $block + { + $( + let $param_name = $crate::macros::apply_param_modifier!($param_modifier, $param_name); + )* + $block + } + } + } +} + +macro_rules! wrap_functions { + ( + $module:ident; + $( + $(#[$meta:meta])+ + fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) + $(-> $return_type:ty)? + $block:block + )+ + ) => { + ::paste::paste! { + $( + $crate::macros::wrap_function!($module; + $(#[$meta])+ + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? + $block + ); )+ } }; @@ -217,7 +233,7 @@ macro_rules! wrap_functions { associate $object_type:ident; $( $(#[$meta:meta])+ - fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) + fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? $block:block )+ @@ -226,7 +242,7 @@ macro_rules! wrap_functions { $crate::macros::wrap_functions!{[< $object_type:lower >]; $( $(#[$meta])+ - fn $function($($param_name: $param_type),*) $(-> $return_type)? + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? $block )+ } @@ -235,7 +251,7 @@ macro_rules! wrap_functions { } pub(crate) use { - derive_clone, derive_free, nonnull_as_move, nonnull_as_mut, nonnull_as_ref, - wrap_fields, wrap_fields_accessor, wrap_functions, wrap_method, - wrap_methods, + apply_param_modifier, derive_clone, derive_free, nonnull_as_mut, + nonnull_as_ref, wrap_fields, wrap_fields_accessor, wrap_function, + wrap_functions, wrap_method, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index 28ade85..5dc172d 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,17 +1,18 @@ use crate::{ containers::ByteSlice, - macros::{derive_clone, derive_free, wrap_fields, wrap_functions}, + macros::{ + derive_clone, derive_free, wrap_fields, wrap_functions, wrap_methods, + }, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; -wrap_functions!(packet; - +wrap_functions!(associate Packet; /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise - fn try_load(data: ByteSlice) -> *mut Packet { + fn try_load(data: val ByteSlice) -> *mut Packet { let data = unsafe { data.as_slice() }; heap_move_ok(servicepoint::Packet::try_from(data)) } @@ -19,7 +20,7 @@ wrap_functions!(packet; /// Creates a raw [Packet] from parts. /// /// returns: new instance. Will never return null. - fn from_parts(header: Header, payload: ByteSlice) -> NonNull { + fn from_parts(header: val Header, payload: val ByteSlice) -> NonNull { let payload = if payload == ByteSlice::INVALID { None } else { @@ -28,44 +29,6 @@ wrap_functions!(packet; heap_move_nonnull(Packet { header, payload }) } - - /// Returns a pointer to the current payload of the provided packet. - /// - /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. - /// - /// The returned memory can be changed and will be valid until a new payload is set. - fn get_payload(packet: NonNull) -> ByteSlice { - unsafe { - match &mut (*packet.as_ptr()).payload { - None => ByteSlice::INVALID, - Some(payload) => ByteSlice::from_slice(payload), - } - } - } - - /// Sets the payload of the provided packet to the provided data. - /// - /// This makes previous payload pointers invalid. - fn set_payload(packet: NonNull, data: ByteSlice) { - unsafe { - (*packet.as_ptr()).payload = if data == ByteSlice::INVALID { - None - } else { - Some(data.as_slice().to_vec()) - } - } - } - - /// Serialize the packet into the provided buffer. - /// - /// # Panics - /// - /// - if the buffer is not big enough to hold header+payload. - fn serialize_to(packet: NonNull, buffer: ByteSlice) -> usize { - unsafe { - packet.as_ref().serialize_to(buffer.as_slice_mut()).unwrap_or(0) - } - } ); derive_clone!(Packet); @@ -75,20 +38,51 @@ wrap_fields!(Packet; prop header: Header { get; get mut; set; }; ); +wrap_methods! { Packet; + /// Returns a pointer to the current payload of the provided packet. + /// + /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. + /// + /// The returned memory can be changed and will be valid until a new payload is set. + fn get_payload(mut packet) -> ByteSlice { + match &mut packet.payload { + None => ByteSlice::INVALID, + Some(payload) => unsafe { ByteSlice::from_slice(payload) }, + } + }; + + /// Sets the payload of the provided packet to the provided data. + /// + /// This makes previous payload pointers invalid. + fn set_payload(mut packet, data: val ByteSlice) { + packet.payload = if data == ByteSlice::INVALID { + None + } else { + Some(unsafe { data.as_slice().to_vec() }) + } + }; + + /// Serialize the packet into the provided buffer. + /// + /// # Panics + /// + /// - if the buffer is not big enough to hold header+payload. + fn serialize_to(mut packet, buffer: val ByteSlice) -> usize { + unsafe { + packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0) + } + }; +} + wrap_functions!(sp; /// Converts u16 into [CommandCode]. /// /// If the provided value is not valid, false is returned and result is not changed. - fn u16_to_command_code( - code: u16, - result: *mut CommandCode, - ) -> bool { + fn u16_to_command_code(code: val u16, result: mut NonNull) -> bool { match CommandCode::try_from(code) { Ok(code) => { - unsafe { - *result = code; - } + *result = code; true } Err(_) => false, diff --git a/src/udp.rs b/src/udp.rs index aa307b0..13718f4 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -25,7 +25,7 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open(host: NonNull) -> *mut UdpSocket { + fn open(host: val NonNull) -> *mut UdpSocket { let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); @@ -44,7 +44,7 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open_ipv4(ip1: u8, ip2: u8, ip3: u8, ip4: u8, port: u16) -> *mut UdpSocket { + fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); heap_move_ok(UdpSocket::bind_connect(addr)) } @@ -57,8 +57,7 @@ wrap_methods! {UdpSocket; /// The passed `packet` gets consumed. /// /// returns: true in case of success - fn send_packet(ref connection, packet: NonNull) -> bool { - let packet = unsafe { heap_remove(packet) }; + fn send_packet(ref connection, packet: move NonNull) -> bool { connection.send(&Vec::from(packet)).is_ok() }; @@ -73,9 +72,8 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - fn send_command(ref connection, command: NonNull) -> bool { + fn send_command(ref connection, command: mut NonNull) -> bool { unsafe { - let command = crate::macros::nonnull_as_mut!(command); let result = match command.tag { CommandTag::Invalid => return false, CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)), @@ -103,7 +101,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - fn send_header(ref udp_connection, header: Header) -> bool { + fn send_header(ref udp_connection, header: val Header) -> bool { let packet = Packet { header, payload: None, From 0968605d0b3605a9dedd11dae6e0a166e3a147f6 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 17:29:03 +0200 Subject: [PATCH 15/17] cleanups, add semicolon everywhere --- include/servicepoint.h | 136 +++++++++++++++++++++- src/commands/bitmap_command.rs | 4 +- src/commands/bitvec_command.rs | 4 +- src/commands/brightness_grid_command.rs | 6 +- src/commands/cc_only_commands.rs | 2 +- src/commands/char_grid_command.rs | 6 +- src/commands/cp437_grid_command.rs | 6 +- src/commands/generic_command.rs | 2 +- src/commands/global_brightness_command.rs | 4 +- src/commands/mod.rs | 2 +- src/containers/bitmap.rs | 8 +- src/containers/bitvec.rs | 6 +- src/containers/brightness_grid.rs | 4 +- src/containers/char_grid.rs | 7 +- src/containers/cp437_grid.rs | 4 +- src/lib.rs | 2 +- src/macros.rs | 64 +++++----- src/packet.rs | 7 +- src/udp.rs | 5 +- 19 files changed, 192 insertions(+), 87 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 2648383..703ab37 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -636,6 +636,8 @@ extern "C" { #endif // __cplusplus /** + * Calls method [`servicepoint::Bitmap::clone`]. + * *Clones a [`Bitmap`] instance. * * This function is part of the `bitmap` module. @@ -667,6 +669,8 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** + * Calls method [`servicepoint::Bitmap::free`]. + * *Deallocates a [`Bitmap`] instance. * * This function is part of the `bitmap` module. @@ -821,6 +825,8 @@ struct Packet *sp_bitmap_try_into_packet(struct Bitmap */*notnull*/ bitmap, size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::clone`]. + * *Clones a [`BitmapCommand`] instance. * * This function is part of the `bitmapcommand` module. @@ -828,6 +834,8 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); struct BitmapCommand */*notnull*/ sp_bitmapcommand_clone(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::free`]. + * *Deallocates a [`BitmapCommand`] instance. * * This function is part of the `bitmapcommand` module. @@ -845,6 +853,8 @@ void sp_bitmapcommand_free(struct BitmapCommand */*notnull*/ instance); struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** + * Calls method [`servicepoint::BitmapCommand::get_bitmap_mut`]. + * * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -855,6 +865,8 @@ struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */* struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::get_compression`]. + * * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -862,6 +874,8 @@ struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand CompressionCode sp_bitmapcommand_get_compression(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::get_origin`]. + * * Reads the origin field of the [`BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -885,6 +899,8 @@ struct BitmapCommand */*notnull*/ sp_bitmapcommand_new(struct Bitmap */*notnull* CompressionCode compression); /** + * Calls method [`servicepoint::BitmapCommand::set_bitmap`]. + * * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -894,6 +910,8 @@ void sp_bitmapcommand_set_bitmap(struct BitmapCommand */*notnull*/ instance, struct Bitmap */*notnull*/ value); /** + * Calls method [`servicepoint::BitmapCommand::set_compression`]. + * * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -902,6 +920,8 @@ void sp_bitmapcommand_set_compression(struct BitmapCommand */*notnull*/ instance CompressionCode value); /** + * Calls method [`servicepoint::BitmapCommand::set_origin`]. + * * Overwrites the origin field of the [`BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -922,6 +942,8 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::clone`]. + * *Clones a [`BitVecCommand`] instance. * * This function is part of the `bitveccommand` module. @@ -929,6 +951,8 @@ struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull* struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::free`]. + * *Deallocates a [`BitVecCommand`] instance. * * This function is part of the `bitveccommand` module. @@ -936,6 +960,8 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_bitvec_mut`]. + * * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -946,6 +972,8 @@ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_compression`]. + * * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -953,6 +981,8 @@ DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_offset`]. + * * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -960,6 +990,8 @@ CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull Offset sp_bitveccommand_get_offset(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_operation`]. + * * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -988,6 +1020,8 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_new(DisplayBitVec */*notnull* CompressionCode compression); /** + * Calls method [`servicepoint::BitVecCommand::set_bitvec`]. + * * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -997,6 +1031,8 @@ void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, DisplayBitVec */*notnull*/ value); /** + * Calls method [`servicepoint::BitVecCommand::set_compression`]. + * * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1005,6 +1041,8 @@ void sp_bitveccommand_set_compression(struct BitVecCommand */*notnull*/ instance CompressionCode value); /** + * Calls method [`servicepoint::BitVecCommand::set_offset`]. + * * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1013,6 +1051,8 @@ void sp_bitveccommand_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); /** + * Calls method [`servicepoint::BitVecCommand::set_operation`]. + * * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1032,6 +1072,8 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGrid::clone`]. + * *Clones a [`BrightnessGrid`] instance. * * This function is part of the `brightnessgrid` module. @@ -1064,6 +1106,8 @@ void sp_brightnessgrid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); /** + * Calls method [`servicepoint::BrightnessGrid::free`]. + * *Deallocates a [`BrightnessGrid`] instance. * * This function is part of the `brightnessgrid` module. @@ -1180,6 +1224,8 @@ struct Packet *sp_brightnessgrid_try_into_packet(BrightnessGrid */*notnull*/ gri size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGridCommand::clone`]. + * *Clones a [`BrightnessGridCommand`] instance. * * This function is part of the `brightnessgridcommand` module. @@ -1187,6 +1233,8 @@ size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_clone(struct BrightnessGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGridCommand::free`]. + * *Deallocates a [`BrightnessGridCommand`] instance. * * This function is part of the `brightnessgridcommand` module. @@ -1202,6 +1250,8 @@ void sp_brightnessgridcommand_free(struct BrightnessGridCommand */*notnull*/ ins struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(BrightnessGrid */*notnull*/ grid); /** + * Calls method [`servicepoint::BrightnessGridCommand::get_grid_mut`]. + * * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1212,6 +1262,8 @@ struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(Bri BrightnessGrid */*notnull*/ sp_brightnessgridcommand_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGridCommand::get_origin`]. + * * Reads the origin field of the [`BrightnessGridCommand`]. * * This function is part of the `brightnessgridcommand` module. @@ -1234,6 +1286,8 @@ struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_new(Brightnes size_t origin_y); /** + * Calls method [`servicepoint::BrightnessGridCommand::set_grid`]. + * * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1243,6 +1297,8 @@ void sp_brightnessgridcommand_set_grid(struct BrightnessGridCommand */*notnull*/ BrightnessGrid */*notnull*/ value); /** + * Calls method [`servicepoint::BrightnessGridCommand::set_origin`]. + * * Overwrites the origin field of the [`BrightnessGridCommand`]. * * This function is part of the `brightnessgridcommand` module. @@ -1263,6 +1319,8 @@ void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGrid::clone`]. + * *Clones a [`CharGrid`] instance. * * This function is part of the `chargrid` module. @@ -1284,6 +1342,8 @@ CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); void sp_chargrid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** + * Calls method [`servicepoint::CharGrid::free`]. + * *Deallocates a [`CharGrid`] instance. * * This function is part of the `chargrid` module. @@ -1392,6 +1452,8 @@ struct Packet *sp_chargrid_try_into_packet(CharGrid */*notnull*/ grid, size_t sp_chargrid_width(CharGrid */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGridCommand::clone`]. + * *Clones a [`CharGridCommand`] instance. * * This function is part of the `chargridcommand` module. @@ -1399,6 +1461,8 @@ size_t sp_chargrid_width(CharGrid */*notnull*/ instance); struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGridCommand::free`]. + * *Deallocates a [`CharGridCommand`] instance. * * This function is part of the `chargridcommand` module. @@ -1414,6 +1478,8 @@ void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*notnull*/ grid); /** + * Calls method [`servicepoint::CharGridCommand::get_grid_mut`]. + * * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1424,6 +1490,8 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*not CharGrid */*notnull*/ sp_chargridcommand_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGridCommand::get_origin`]. + * * Reads the origin field of the [`CharGridCommand`]. * * This function is part of the `chargridcommand` module. @@ -1446,6 +1514,8 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_new(CharGrid */*notnull*/ size_t origin_y); /** + * Calls method [`servicepoint::CharGridCommand::set_grid`]. + * * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1455,6 +1525,8 @@ void sp_chargridcommand_set_grid(struct CharGridCommand */*notnull*/ instance, CharGrid */*notnull*/ value); /** + * Calls method [`servicepoint::CharGridCommand::set_origin`]. + * * Overwrites the origin field of the [`CharGridCommand`]. * * This function is part of the `chargridcommand` module. @@ -1475,6 +1547,8 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::ClearCommand::clone`]. + * *Clones a [`ClearCommand`] instance. * * This function is part of the `clearcommand` module. @@ -1482,6 +1556,8 @@ struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notn struct ClearCommand */*notnull*/ sp_clearcommand_clone(struct ClearCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::ClearCommand::free`]. + * *Deallocates a [`ClearCommand`] instance. * * This function is part of the `clearcommand` module. @@ -1511,6 +1587,8 @@ struct ClearCommand */*notnull*/ sp_clearcommand_new(void); struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437Grid::clone`]. + * *Clones a [`Cp437Grid`] instance. * * This function is part of the `cp437grid` module. @@ -1542,6 +1620,8 @@ struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); void sp_cp437grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** + * Calls method [`servicepoint::Cp437Grid::free`]. + * *Deallocates a [`Cp437Grid`] instance. * * This function is part of the `cp437grid` module. @@ -1638,6 +1718,8 @@ struct Packet *sp_cp437grid_try_into_packet(Cp437Grid */*notnull*/ grid, size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437GridCommand::clone`]. + * *Clones a [`Cp437GridCommand`] instance. * * This function is part of the `cp437gridcommand` module. @@ -1645,6 +1727,8 @@ size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437GridCommand::free`]. + * *Deallocates a [`Cp437GridCommand`] instance. * * This function is part of the `cp437gridcommand` module. @@ -1660,6 +1744,8 @@ void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */*notnull*/ grid); /** + * Calls method [`servicepoint::Cp437GridCommand::get_grid_mut`]. + * * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1670,6 +1756,8 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */* Cp437Grid */*notnull*/ sp_cp437gridcommand_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437GridCommand::get_origin`]. + * * Reads the origin field of the [`Cp437GridCommand`]. * * This function is part of the `cp437gridcommand` module. @@ -1692,6 +1780,8 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_new(Cp437Grid */*notnul size_t origin_y); /** + * Calls method [`servicepoint::Cp437GridCommand::set_grid`]. + * * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1701,6 +1791,8 @@ void sp_cp437gridcommand_set_grid(struct Cp437GridCommand */*notnull*/ instance, Cp437Grid */*notnull*/ value); /** + * Calls method [`servicepoint::Cp437GridCommand::set_origin`]. + * * Overwrites the origin field of the [`Cp437GridCommand`]. * * This function is part of the `cp437gridcommand` module. @@ -1732,6 +1824,8 @@ struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*no struct ByteSlice sp_displaybitvec_as_raw_mut_slice(DisplayBitVec */*notnull*/ instance); /** + * Calls method [`servicepoint::DisplayBitVec::clone`]. + * *Clones a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. @@ -1752,6 +1846,8 @@ DisplayBitVec */*notnull*/ sp_displaybitvec_clone(DisplayBitVec */*notnull*/ ins void sp_displaybitvec_fill(DisplayBitVec */*notnull*/ instance, bool value); /** + * Calls method [`servicepoint::DisplayBitVec::free`]. + * *Deallocates a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. @@ -1867,6 +1963,8 @@ struct Packet *sp_displaybitvec_try_into_packet(DisplayBitVec */*notnull*/ bitve void sp_envlogger_init(void); /** + * Calls method [`servicepoint::FadeOutCommand::clone`]. + * *Clones a [`FadeOutCommand`] instance. * * This function is part of the `fadeoutcommand` module. @@ -1874,6 +1972,8 @@ void sp_envlogger_init(void); struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_clone(struct FadeOutCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::FadeOutCommand::free`]. + * *Deallocates a [`FadeOutCommand`] instance. * * This function is part of the `fadeoutcommand` module. @@ -1901,6 +2001,8 @@ struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GenericCommand::clone`]. + * *Clones a [`GenericCommand`] instance. * * This function is part of the `genericcommand` module. @@ -1908,6 +2010,8 @@ struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnul struct GenericCommand */*notnull*/ sp_genericcommand_clone(struct GenericCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GenericCommand::free`]. + * *Deallocates a [`GenericCommand`] instance. * * This function is part of the `genericcommand` module. @@ -1938,6 +2042,8 @@ struct GenericCommand */*notnull*/ sp_genericcommand_try_from_packet(struct Pack struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnull*/ command); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::clone`]. + * *Clones a [`GlobalBrightnessCommand`] instance. * * This function is part of the `globalbrightnesscommand` module. @@ -1945,6 +2051,8 @@ struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnul struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::free`]. + * *Deallocates a [`GlobalBrightnessCommand`] instance. * * This function is part of the `globalbrightnesscommand` module. @@ -1952,6 +2060,8 @@ struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(str void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::get_brightness`]. + * * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * * This function is part of the `globalbrightnesscommand` module. @@ -1968,6 +2078,8 @@ Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessComm struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brightness brightness); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::set_brightness`]. + * * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * * This function is part of the `globalbrightnesscommand` module. @@ -1987,6 +2099,8 @@ void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */ struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::HardResetCommand::clone`]. + * *Clones a [`HardResetCommand`] instance. * * This function is part of the `hardresetcommand` module. @@ -1994,6 +2108,8 @@ struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnes struct HardResetCommand */*notnull*/ sp_hardresetcommand_clone(struct HardResetCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::HardResetCommand::free`]. + * *Deallocates a [`HardResetCommand`] instance. * * This function is part of the `hardresetcommand` module. @@ -2023,6 +2139,8 @@ struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::clone`]. + * *Clones a [`Packet`] instance. * * This function is part of the `packet` module. @@ -2030,6 +2148,8 @@ struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*no struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::free`]. + * *Deallocates a [`Packet`] instance. * * This function is part of the `packet` module. @@ -2047,6 +2167,8 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); /** + * Calls method [`servicepoint::Packet::get_header`]. + * * Gets the value of field `header` of the [`servicepoint::Packet`]. * * This function is part of the `packet` module. @@ -2054,6 +2176,8 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::get_header_mut`]. + * * Gets a reference to the field `header` of the [`servicepoint::Packet`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -2064,6 +2188,8 @@ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::get_payload`]. + * * Returns a pointer to the current payload of the provided packet. * * Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. @@ -2075,6 +2201,8 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ i struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); /** + * Calls method [`servicepoint::Packet::serialize_to`]. + * * Serialize the packet into the provided buffer. * * # Panics @@ -2087,6 +2215,8 @@ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); /** + * Calls method [`servicepoint::Packet::set_header`]. + * * Sets the value of field `header` of the [`servicepoint::Packet`]. * * This function is part of the `packet` module. @@ -2095,6 +2225,8 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); /** + * Calls method [`servicepoint::Packet::set_payload`]. + * * Sets the payload of the provided packet to the provided data. * * This makes previous payload pointers invalid. @@ -2121,9 +2253,11 @@ struct Packet *sp_packet_try_load(struct ByteSlice data); * This function is part of the `sp` module. */ bool sp_sp_u16_to_command_code(uint16_t code, - CommandCode *result); + CommandCode */*notnull*/ result); /** + * Calls method [`servicepoint::UdpSocket::free`]. + * *Deallocates a [`UdpSocket`] instance. * * This function is part of the `udpsocket` module. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 5aaab26..c7290ce 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -32,7 +32,7 @@ wrap_functions!(associate BitmapCommand; origin: Origin::new(origin_x, origin_y), compression, }) - } + }; /// Move the provided [Bitmap] into a new [BitmapCommand], /// leaving other fields as their default values. @@ -40,5 +40,5 @@ wrap_functions!(associate BitmapCommand; /// Rust equivalent: `BitmapCommand::from(bitmap)` fn from_bitmap(bitmap: move NonNull) -> NonNull { heap_move_nonnull(bitmap.into()) - } + }; ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 4dd9ca9..6b26227 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -18,7 +18,6 @@ wrap_fields!(BitVecCommand; ); wrap_functions!(associate BitVecCommand; - /// Set pixel data starting at the pixel offset on screen. /// /// The screen will continuously overwrite more pixel data without regarding the offset, meaning @@ -43,6 +42,5 @@ wrap_functions!(associate BitVecCommand; operation, compression, }) - } - + }; ); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index f05b820..e53a1a0 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -15,7 +15,6 @@ wrap_fields!(BrightnessGridCommand; wrap_origin_accessors!(BrightnessGridCommand); wrap_functions!(associate BrightnessGridCommand; - /// Set the brightness of individual tiles in a rectangular area of the display. /// /// The passed [BrightnessGrid] gets consumed. @@ -30,12 +29,11 @@ wrap_functions!(associate BrightnessGridCommand; grid, origin: Origin::new(origin_x, origin_y), }) - } + }; /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], /// leaving other fields as their default values. fn from_grid(grid: move NonNull) -> NonNull { heap_move_nonnull(grid.into()) - } - + }; ); diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 6635ae9..6f06bd8 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -14,7 +14,7 @@ macro_rules! wrap_cc_only { #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] fn new() -> ::core::ptr::NonNull<[< $command Command >]> { heap_move_nonnull([< $command Command >]) - } + }; ); } }; diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 12b58ca..fb9f7d0 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -15,7 +15,6 @@ wrap_fields!(CharGridCommand; wrap_origin_accessors!(CharGridCommand); wrap_functions!(associate CharGridCommand; - /// Show UTF-8 encoded text on the screen. /// /// The passed [CharGrid] gets consumed. @@ -30,12 +29,11 @@ wrap_functions!(associate CharGridCommand; grid, origin: Origin::new(origin_x, origin_y), }) - } + }; /// Moves the provided [CharGrid] into a new [CharGridCommand], /// leaving other fields as their default values. fn from_grid(grid: move NonNull) -> NonNull { heap_move_nonnull(grid.into()) - } - + }; ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 921e2a4..9e7e75b 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -15,7 +15,6 @@ wrap_fields!(Cp437GridCommand; wrap_origin_accessors!(Cp437GridCommand); wrap_functions!(associate Cp437GridCommand; - /// Show text on the screen. /// /// The text is sent in the form of a 2D grid of [CP-437] encoded characters. @@ -30,12 +29,11 @@ wrap_functions!(associate Cp437GridCommand; grid, origin: Origin::new(origin_x, origin_y), }) - } + }; /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// leaving other fields as their default values. fn from_grid(grid: move NonNull) -> NonNull { heap_move_nonnull(grid.into()) - } - + }; ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index efd402c..d4282c4 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -248,7 +248,7 @@ wrap_functions!(associate GenericCommand; data: CommandUnion { null: null_mut() }, }); heap_move_nonnull(result) - } + }; ); wrap_methods! { GenericCommand; diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index d2fef10..2217c43 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -7,14 +7,12 @@ use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; wrap_functions!(associate GlobalBrightnessCommand; - /// Set the brightness of all tiles to the same value. /// /// Returns: a new [GlobalBrightnessCommand] instance. fn new(brightness: val Brightness) -> NonNull { heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) - } - + }; ); wrap_command!(GlobalBrightness); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index da16c18..37f9e14 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -64,7 +64,7 @@ macro_rules! derive_command_into_packet { /// Returns: NULL or a [Packet] containing the command. fn try_into_packet(move instance) -> *mut ::servicepoint::Packet { $crate::mem::heap_move_ok(instance.try_into()) - } + }; ); } } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 1386a8d..6367b81 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -37,14 +37,14 @@ wrap_functions!(associate Bitmap; /// ``` fn new(width: val usize, height: val usize) -> *mut Bitmap { heap_move_some(Bitmap::new(width, height)) - } + }; /// Creates a new [Bitmap] with a size matching the screen. /// /// returns: [Bitmap] initialized to all pixels off. fn new_max_sized() -> NonNull { heap_move_nonnull(Bitmap::max_sized()) - } + }; /// Loads a [Bitmap] with the specified dimensions from the provided data. /// @@ -61,7 +61,7 @@ wrap_functions!(associate Bitmap; ) -> *mut Bitmap { let data = unsafe { data.as_slice() }; heap_move_ok(Bitmap::load(width, height, data)) - } + }; /// Tries to convert the BitVec to a Bitmap. /// @@ -73,7 +73,7 @@ wrap_functions!(associate Bitmap; bitvec: move NonNull, ) -> *mut Bitmap { heap_move_ok(Bitmap::from_bitvec(width, bitvec)) - } + }; ); wrap_methods!(Bitmap; diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index cdb7c36..76297b0 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -11,7 +11,6 @@ use std::ptr::NonNull; wrap_container!(DisplayBitVec); wrap_functions!(associate DisplayBitVec; - /// Creates a new [DisplayBitVec] instance. /// /// # Arguments @@ -25,7 +24,7 @@ wrap_functions!(associate DisplayBitVec; /// - when `size` is not divisible by 8. fn new(size: val usize) -> NonNull { heap_move_nonnull(DisplayBitVec::repeat(false, size)) - } + }; /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// @@ -33,8 +32,7 @@ wrap_functions!(associate DisplayBitVec; fn load(data: val ByteSlice) -> NonNull { let data = unsafe { data.as_slice() }; heap_move_nonnull(DisplayBitVec::from_slice(data)) - } - + }; ); wrap_methods!(DisplayBitVec; diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index b0da48a..a9bcad4 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -32,7 +32,7 @@ wrap_functions!(associate BrightnessGrid; /// ``` fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(BrightnessGrid::new(width, height)) - } + }; /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. /// @@ -49,7 +49,7 @@ wrap_functions!(associate BrightnessGrid; ByteGrid::load(width, height, data) .map(move |grid| grid.map(Brightness::saturating_from)), ) - } + }; ); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 715588f..9001d2c 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -10,7 +10,6 @@ wrap_container!(CharGrid); derive_get_width_height!(CharGrid); wrap_functions!(associate CharGrid; - /// Creates a new [CharGrid] with the specified dimensions. /// /// returns: [CharGrid] initialized to 0. @@ -25,7 +24,7 @@ wrap_functions!(associate CharGrid; /// ``` fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(CharGrid::new(width, height)) - } + }; /// Loads a [CharGrid] with the specified dimensions from the provided data. /// @@ -33,9 +32,7 @@ wrap_functions!(associate CharGrid; fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid { let data = unsafe { data.as_slice() }; heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) - } - - + }; ); wrap_methods!(CharGrid; diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index ecc36c6..48f62fc 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -16,13 +16,13 @@ wrap_functions!(associate Cp437Grid; /// returns: [Cp437Grid] initialized to 0. fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(Cp437Grid::new(width, height)) - } + }; /// Loads a [Cp437Grid] with the specified dimensions from the provided data. fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; heap_move_some(Cp437Grid::load(width, height, data)) - } + }; ); wrap_methods!(Cp437Grid; diff --git a/src/lib.rs b/src/lib.rs index 5a1a5de..f884a83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,6 @@ mod feature_env_logger { /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). fn init() { env_logger::init(); - } + }; ); } diff --git a/src/macros.rs b/src/macros.rs index 8cae819..bfbd4b3 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,48 +1,36 @@ macro_rules! derive_free { ($typ:ident) => { ::paste::paste! { - $crate::macros::wrap_functions!([< $typ:lower >]; + $crate::macros::wrap_method!($typ; #[doc = "Deallocates a [`" $typ "`] instance."] #[allow(dropping_copy_types)] - fn free(instance: move ::core::ptr::NonNull<$typ>) { + fn free(move instance) { ::std::mem::drop(instance) - } + }; ); } }; } macro_rules! derive_clone { - ($typ:ident) => { + ($object_type:ident) => { ::paste::paste! { - $crate::macros::wrap_functions!([< $typ:lower >]; - #[doc = "Clones a [`" $typ "`] instance."] - fn clone(instance: ref ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { + $crate::macros::wrap_method!($object_type; + #[doc = "Clones a [`" $object_type "`] instance."] + fn clone(ref instance) -> ::core::ptr::NonNull<$object_type> { $crate::mem::heap_move_nonnull(instance.clone()) - } + }; ); } }; } -macro_rules! nonnull_as_ref { - ($ident:ident) => { - $ident.as_ref() - }; -} - -macro_rules! nonnull_as_mut { - ($ident:ident) => { - (&mut *$ident.as_ptr()) - }; -} - macro_rules! wrap_method { ( $object_type:ident; $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)? + $(-> $return_type:ty)?; ) => { ::paste::paste!{ $crate::macros::wrap_method!( @@ -51,7 +39,7 @@ macro_rules! wrap_method { fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? { $instance.$function($($($param_name),*)?) - } + }; ); } }; @@ -59,7 +47,7 @@ macro_rules! wrap_method { $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? - $impl:block + $impl:block; ) => { paste::paste! { $crate::macros::wrap_functions!([< $object_type:lower >]; @@ -70,7 +58,7 @@ macro_rules! wrap_method { $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> $(,$($param_name: $param_modifier $param_type),*)? ) $(-> $return_type)? - $impl + $impl; ); } }; @@ -92,7 +80,7 @@ macro_rules! wrap_methods { $(#[$meta])* fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? - $($impl)? + $($impl)?; ); )+ } @@ -107,7 +95,7 @@ macro_rules! wrap_fields_accessor { "` of the [`servicepoint::" $object_type "`]."] fn [](ref instance) -> $prop_type { return instance.$prop_name; - } + }; } } }; @@ -121,7 +109,7 @@ macro_rules! wrap_fields_accessor { /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. fn [](mut instance) -> ::core::ptr::NonNull<$prop_type> { return ::core::ptr::NonNull::from(&mut instance.$prop_name); - } + }; } } }; @@ -132,7 +120,7 @@ macro_rules! wrap_fields_accessor { "` of the [`servicepoint::" $object_type "`]."] fn [](mut instance, value: val $prop_type) { instance.$prop_name = value; - } + }; } } }; @@ -144,7 +132,7 @@ macro_rules! wrap_fields_accessor { /// The provided value is moved into the instance, potentially invalidating previously taken references. fn [](mut instance, value: move ::core::ptr::NonNull<$prop_type>) { instance.$prop_name = value; - } + }; } } }; @@ -176,10 +164,10 @@ macro_rules! apply_param_modifier { $param_name }; (mut, $param_name:ident) => { - unsafe { $crate::macros::nonnull_as_mut!($param_name) } + unsafe { (&mut *$param_name.as_ptr()) } }; (ref, $param_name:ident) => { - unsafe { $crate::macros::nonnull_as_ref!($param_name) } + unsafe { $param_name.as_ref() } }; } @@ -189,7 +177,7 @@ macro_rules! wrap_function { $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? - $block:block + $block:block; ) => { ::paste::paste! { $(#[$meta])* @@ -216,7 +204,7 @@ macro_rules! wrap_functions { $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? - $block:block + $block:block; )+ ) => { ::paste::paste! { @@ -224,7 +212,7 @@ macro_rules! wrap_functions { $crate::macros::wrap_function!($module; $(#[$meta])+ fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? - $block + $block; ); )+ } @@ -235,7 +223,7 @@ macro_rules! wrap_functions { $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? - $block:block + $block:block; )+ ) => { ::paste::paste! { @@ -243,7 +231,7 @@ macro_rules! wrap_functions { $( $(#[$meta])+ fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? - $block + $block; )+ } } @@ -251,7 +239,7 @@ macro_rules! wrap_functions { } pub(crate) use { - apply_param_modifier, derive_clone, derive_free, nonnull_as_mut, - nonnull_as_ref, wrap_fields, wrap_fields_accessor, wrap_function, + apply_param_modifier, derive_clone, derive_free, + wrap_fields, wrap_fields_accessor, wrap_function, wrap_functions, wrap_method, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index 5dc172d..4f1d38f 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -15,7 +15,7 @@ wrap_functions!(associate Packet; fn try_load(data: val ByteSlice) -> *mut Packet { let data = unsafe { data.as_slice() }; heap_move_ok(servicepoint::Packet::try_from(data)) - } + }; /// Creates a raw [Packet] from parts. /// @@ -28,7 +28,7 @@ wrap_functions!(associate Packet; }; heap_move_nonnull(Packet { header, payload }) - } + }; ); derive_clone!(Packet); @@ -87,6 +87,5 @@ wrap_functions!(sp; } Err(_) => false, } - } - + }; ); diff --git a/src/udp.rs b/src/udp.rs index 13718f4..a12281f 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -29,9 +29,8 @@ wrap_functions!(associate UdpSocket; let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); - heap_move_ok(UdpSocket::bind_connect(host)) - } + }; /// Creates a new instance of [UdpSocket]. /// @@ -47,7 +46,7 @@ wrap_functions!(associate UdpSocket; fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); heap_move_ok(UdpSocket::bind_connect(addr)) - } + }; ); From 39c7c27c868d69ef642768756c033f89a3488e76 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 18:59:46 +0200 Subject: [PATCH 16/17] implement return modifiers --- src/commands/bitmap_command.rs | 11 ++-- src/commands/bitvec_command.rs | 7 ++- src/commands/brightness_grid_command.rs | 11 ++-- src/commands/cc_only_commands.rs | 8 ++- src/commands/char_grid_command.rs | 11 ++-- src/commands/cp437_grid_command.rs | 11 ++-- src/commands/generic_command.rs | 9 ++-- src/commands/global_brightness_command.rs | 5 +- src/commands/mod.rs | 4 +- src/containers/bitmap.rs | 29 +++++----- src/containers/bitvec.rs | 23 ++++---- src/containers/brightness_grid.rs | 23 ++++---- src/containers/char_grid.rs | 17 +++--- src/containers/cp437_grid.rs | 17 +++--- src/containers/mod.rs | 6 +-- src/macros.rs | 65 +++++++++++++++-------- src/packet.rs | 15 +++--- src/udp.rs | 18 +++---- 18 files changed, 144 insertions(+), 146 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index c7290ce..40fbc1c 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; @@ -26,19 +25,19 @@ wrap_functions!(associate BitmapCommand; origin_x: val usize, origin_y: val usize, compression: val CompressionCode, - ) -> NonNull { - heap_move_nonnull(BitmapCommand { + ) -> move NonNull { + BitmapCommand { bitmap, origin: Origin::new(origin_x, origin_y), compression, - }) + } }; /// Move the provided [Bitmap] into a new [BitmapCommand], /// leaving other fields as their default values. /// /// Rust equivalent: `BitmapCommand::from(bitmap)` - fn from_bitmap(bitmap: move NonNull) -> NonNull { - heap_move_nonnull(bitmap.into()) + fn from_bitmap(bitmap: move NonNull) -> move NonNull { + bitmap.into() }; ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 6b26227..f62b492 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, @@ -35,12 +34,12 @@ wrap_functions!(associate BitVecCommand; offset: val usize, operation: val BinaryOperation, compression: val CompressionCode, - ) -> NonNull { - heap_move_nonnull(BitVecCommand { + ) -> move NonNull { + BitVecCommand { bitvec, offset, operation, compression, - }) + } }; ); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index e53a1a0..007d645 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; @@ -24,16 +23,16 @@ wrap_functions!(associate BrightnessGridCommand; grid: move NonNull, origin_x: val usize, origin_y: val usize - ) -> NonNull { - heap_move_nonnull(BrightnessGridCommand { + ) -> move NonNull { + BrightnessGridCommand { grid, origin: Origin::new(origin_x, origin_y), - }) + } }; /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> NonNull { - heap_move_nonnull(grid.into()) + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() }; ); diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 6f06bd8..ef49fa1 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,6 +1,4 @@ -use crate::{ - commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull, -}; +use crate::{commands::wrap_command, macros::wrap_functions}; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; macro_rules! wrap_cc_only { @@ -12,8 +10,8 @@ macro_rules! wrap_cc_only { $(#[$meta])* /// #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] - fn new() -> ::core::ptr::NonNull<[< $command Command >]> { - heap_move_nonnull([< $command Command >]) + fn new() -> move ::core::ptr::NonNull<[< $command Command >]> { + [< $command Command >] }; ); } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index fb9f7d0..06f2070 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; @@ -24,16 +23,16 @@ wrap_functions!(associate CharGridCommand; grid: move NonNull, origin_x: val usize, origin_y: val usize, - ) -> NonNull { - heap_move_nonnull(CharGridCommand { + ) -> move NonNull { + CharGridCommand { grid, origin: Origin::new(origin_x, origin_y), - }) + } }; /// Moves the provided [CharGrid] into a new [CharGridCommand], /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> NonNull { - heap_move_nonnull(grid.into()) + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() }; ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 9e7e75b..d4c0e88 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; @@ -24,16 +23,16 @@ wrap_functions!(associate Cp437GridCommand; grid: move NonNull, origin_x: val usize, origin_y: val usize, - ) -> NonNull { - heap_move_nonnull(Cp437GridCommand { + ) -> move NonNull { + Cp437GridCommand { grid, origin: Origin::new(origin_x, origin_y), - }) + } }; /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> NonNull { - heap_move_nonnull(grid.into()) + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() }; ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index d4282c4..c6a1323 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -178,8 +178,8 @@ wrap_functions!(associate GenericCommand; /// The packet is dropped in the process. /// /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. - fn try_from_packet(packet: move NonNull) -> NonNull { - let result = servicepoint::TypedCommand::try_from(packet) + fn try_from_packet(packet: move NonNull) -> move NonNull { + servicepoint::TypedCommand::try_from(packet) .map(|value| match value { TypedCommand::Clear(clear) => GenericCommand { tag: CommandTag::Clear, @@ -246,8 +246,7 @@ wrap_functions!(associate GenericCommand; .unwrap_or_else(move |_| GenericCommand { tag: CommandTag::Invalid, data: CommandUnion { null: null_mut() }, - }); - heap_move_nonnull(result) + }) }; ); @@ -256,7 +255,7 @@ wrap_methods! { GenericCommand; /// The [GenericCommand] gets consumed. /// /// Returns tag [CommandTag::Invalid] in case of an error. - fn try_into_packet(move command) -> *mut Packet { + fn try_into_packet(move command) -> val *mut Packet { match command.tag { CommandTag::Invalid => null_mut(), CommandTag::Bitmap => { diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 2217c43..6a883ab 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; @@ -10,8 +9,8 @@ wrap_functions!(associate GlobalBrightnessCommand; /// Set the brightness of all tiles to the same value. /// /// Returns: a new [GlobalBrightnessCommand] instance. - fn new(brightness: val Brightness) -> NonNull { - heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) + fn new(brightness: val Brightness) -> move NonNull { + GlobalBrightnessCommand::from(brightness) }; ); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 37f9e14..1d6e8ff 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -62,8 +62,8 @@ macro_rules! derive_command_into_packet { #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet(move instance) -> *mut ::servicepoint::Packet { - $crate::mem::heap_move_ok(instance.try_into()) + fn try_into_packet(move instance) -> move_ok *mut ::servicepoint::Packet { + instance.try_into() }; ); } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 6367b81..b47cfbc 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, @@ -35,15 +34,15 @@ wrap_functions!(associate Bitmap; /// sp_bitmap_set(grid, 0, 0, false); /// sp_bitmap_free(grid); /// ``` - fn new(width: val usize, height: val usize) -> *mut Bitmap { - heap_move_some(Bitmap::new(width, height)) + fn new(width: val usize, height: val usize) -> move_some *mut Bitmap { + Bitmap::new(width, height) }; /// Creates a new [Bitmap] with a size matching the screen. /// /// returns: [Bitmap] initialized to all pixels off. - fn new_max_sized() -> NonNull { - heap_move_nonnull(Bitmap::max_sized()) + fn new_max_sized() -> move NonNull { + Bitmap::max_sized() }; /// Loads a [Bitmap] with the specified dimensions from the provided data. @@ -58,9 +57,9 @@ wrap_functions!(associate Bitmap; width: val usize, height: val usize, data: val ByteSlice, - ) -> *mut Bitmap { + ) -> move_ok *mut Bitmap { let data = unsafe { data.as_slice() }; - heap_move_ok(Bitmap::load(width, height, data)) + Bitmap::load(width, height, data) }; /// Tries to convert the BitVec to a Bitmap. @@ -71,15 +70,15 @@ wrap_functions!(associate Bitmap; fn from_bitvec( width: val usize, bitvec: move NonNull, - ) -> *mut Bitmap { - heap_move_ok(Bitmap::from_bitvec(width, bitvec)) + ) -> move_ok *mut Bitmap { + Bitmap::from_bitvec(width, bitvec) }; ); wrap_methods!(Bitmap; /// Consumes the Bitmap and returns the contained BitVec. - fn into_bitvec(move bitmap) -> NonNull { - heap_move_nonnull(bitmap.into()) + fn into_bitvec(move bitmap) -> move NonNull { + bitmap.into() }; /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. @@ -87,18 +86,18 @@ wrap_methods!(Bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> *mut Packet { - heap_move_ok(Packet::try_from(BitmapCommand { + fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet { + Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), compression, - })) + }) }; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - fn data_ref_mut(mut instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> val ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 76297b0..50a4eee 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_container, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -22,16 +21,16 @@ wrap_functions!(associate DisplayBitVec; /// # Panics /// /// - when `size` is not divisible by 8. - fn new(size: val usize) -> NonNull { - heap_move_nonnull(DisplayBitVec::repeat(false, size)) + fn new(size: val usize) -> move NonNull { + DisplayBitVec::repeat(false, size) }; /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// /// returns: [DisplayBitVec] instance containing data. - fn load(data: val ByteSlice) -> NonNull { + fn load(data: val ByteSlice) -> move NonNull { let data = unsafe { data.as_slice() }; - heap_move_nonnull(DisplayBitVec::from_slice(data)) + DisplayBitVec::from_slice(data) }; ); @@ -46,13 +45,13 @@ wrap_methods!(DisplayBitVec; offset: val usize, operation: val BinaryOperation, compression: val CompressionCode - ) -> *mut Packet { - heap_move_ok(Packet::try_from(BitVecCommand { + ) -> move_ok *mut Packet { + Packet::try_from(BitVecCommand { bitvec, offset, operation, compression, - })) + }) }; /// Gets the value of a bit. @@ -67,7 +66,7 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn get(ref instance, index: val usize) -> bool { + fn get(ref instance, index: val usize) -> val bool { instance.get(index).map(|x| *x).unwrap_or(false) }; @@ -91,15 +90,15 @@ wrap_methods!(DisplayBitVec; fn fill(mut instance, value: val bool); /// Gets the length in bits. - fn len(ref instance) -> usize; + fn len(ref instance) -> val usize; /// Returns true if length is 0. - fn is_empty(ref instance) -> bool; + fn is_empty(ref instance) -> val bool; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - fn as_raw_mut_slice(mut instance) -> ByteSlice { + fn as_raw_mut_slice(mut instance) -> val ByteSlice { unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } }; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a9bcad4..f7cd099 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -12,7 +11,6 @@ use std::{mem::transmute, ptr::NonNull}; wrap_grid!(BrightnessGrid, Brightness); wrap_functions!(associate BrightnessGrid; - /// Creates a new [BrightnessGrid] with the specified dimensions. /// /// returns: [BrightnessGrid] initialized to 0. @@ -30,8 +28,8 @@ wrap_functions!(associate BrightnessGrid; /// TypedCommand *command = sp_command_char_brightness(grid); /// sp_udp_free(connection); /// ``` - fn new(width: val usize, height: val usize) -> NonNull { - heap_move_nonnull(BrightnessGrid::new(width, height)) + fn new(width: val usize, height: val usize) -> move NonNull { + BrightnessGrid::new(width, height) }; /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. @@ -43,14 +41,11 @@ wrap_functions!(associate BrightnessGrid; width: val usize, height: val usize, data: val ByteSlice, - ) -> *mut BrightnessGrid { + ) -> move_some *mut BrightnessGrid { let data = unsafe { data.as_slice() }; - heap_move_some( - ByteGrid::load(width, height, data) - .map(move |grid| grid.map(Brightness::saturating_from)), - ) + ByteGrid::load(width, height, data) + .map(move |grid| grid.map(Brightness::saturating_from)) }; - ); wrap_methods!(BrightnessGrid; @@ -59,17 +54,17 @@ wrap_methods!(BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { - heap_move_ok(Packet::try_from(BrightnessGridCommand { + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), - })) + }) }; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - fn data_ref_mut(mut instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> val ByteSlice { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 9001d2c..a43f93d 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,7 +1,6 @@ use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -22,16 +21,16 @@ wrap_functions!(associate CharGrid; /// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_free(grid); /// ``` - fn new(width: val usize, height: val usize) -> NonNull { - heap_move_nonnull(CharGrid::new(width, height)) + fn new(width: val usize, height: val usize) -> move NonNull { + CharGrid::new(width, height) }; /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_ok *mut CharGrid { let data = unsafe { data.as_slice() }; - heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) + CharGrid::load_utf8(width, height, data.to_vec()) }; ); @@ -46,7 +45,7 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: val usize, y: val usize) -> u32 { + fn get(ref instance, x: val usize, y: val usize) -> val u32 { instance.get(x, y) as u32 }; @@ -82,10 +81,10 @@ wrap_methods!(CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { - heap_move_ok(Packet::try_from(CharGridCommand { + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), - })) + }) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 48f62fc..e19ff72 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, @@ -14,14 +13,14 @@ wrap_functions!(associate Cp437Grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// /// returns: [Cp437Grid] initialized to 0. - fn new(width: val usize, height: val usize) -> NonNull { - heap_move_nonnull(Cp437Grid::new(width, height)) + fn new(width: val usize, height: val usize) -> move NonNull { + Cp437Grid::new(width, height) }; /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut Cp437Grid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_some *mut Cp437Grid { let data = unsafe { data.as_slice() }; - heap_move_some(Cp437Grid::load(width, height, data)) + Cp437Grid::load(width, height, data) }; ); @@ -31,17 +30,17 @@ wrap_methods!(Cp437Grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { - heap_move_ok(Packet::try_from(Cp437GridCommand { + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), - })) + }) }; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - fn data_ref_mut(mut instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> val ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 35bbe3b..608d038 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -23,10 +23,10 @@ macro_rules! derive_get_width_height { ($object_type:ident) => { $crate::macros::wrap_methods! {$object_type; /// Gets the width. - fn width(ref instance) -> usize; + fn width(ref instance) -> val usize; /// Gets the height. - fn height(ref instance) -> usize; + fn height(ref instance) -> val usize; } }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: val usize, y: val usize) -> $value_type; + fn get(ref instance, x: val usize, y: val usize) -> val $value_type; /// Sets the value of the specified position. /// diff --git a/src/macros.rs b/src/macros.rs index bfbd4b3..086c901 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -17,8 +17,8 @@ macro_rules! derive_clone { ::paste::paste! { $crate::macros::wrap_method!($object_type; #[doc = "Clones a [`" $object_type "`] instance."] - fn clone(ref instance) -> ::core::ptr::NonNull<$object_type> { - $crate::mem::heap_move_nonnull(instance.clone()) + fn clone(ref instance) -> move ::core::ptr::NonNull<$object_type> { + instance.clone() }; ); } @@ -30,14 +30,14 @@ macro_rules! wrap_method { $object_type:ident; $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)?; + $(-> $return_modifier:ident $return_type:ty)?; ) => { ::paste::paste!{ $crate::macros::wrap_method!( $object_type; $(#[$meta])+ fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) - $(-> $return_type)? { + $(-> $return_modifier $return_type)? { $instance.$function($($($param_name),*)?) }; ); @@ -46,7 +46,7 @@ macro_rules! wrap_method { ($object_type:ident; $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $impl:block; ) => { paste::paste! { @@ -57,7 +57,7 @@ macro_rules! wrap_method { fn $function( $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> $(,$($param_name: $param_modifier $param_type),*)? - ) $(-> $return_type)? + ) $(-> $return_modifier $return_type)? $impl; ); } @@ -70,7 +70,7 @@ macro_rules! wrap_methods { $( $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $($impl:block)?; )+ ) => { @@ -79,7 +79,7 @@ macro_rules! wrap_methods { $crate::macros::wrap_method!($object_type; $(#[$meta])* fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) - $(-> $return_type)? + $(-> $return_modifier $return_type)? $($impl)?; ); )+ @@ -93,8 +93,8 @@ macro_rules! wrap_fields_accessor { $crate::macros::wrap_method! {$object_type; #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - fn [](ref instance) -> $prop_type { - return instance.$prop_name; + fn [](ref instance) -> val $prop_type { + instance.$prop_name }; } } @@ -107,8 +107,8 @@ macro_rules! wrap_fields_accessor { /// /// - The returned reference inherits the lifetime of object in which it is contained. /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - fn [](mut instance) -> ::core::ptr::NonNull<$prop_type> { - return ::core::ptr::NonNull::from(&mut instance.$prop_name); + fn [](mut instance) -> val ::core::ptr::NonNull<$prop_type> { + ::core::ptr::NonNull::from(&mut instance.$prop_name) }; } } @@ -171,16 +171,31 @@ macro_rules! apply_param_modifier { }; } +macro_rules! apply_return_modifier { + (val, $result:ident) => { + $result + }; + (move, $result:ident) => { + $crate::mem::heap_move_nonnull($result) + }; + (move_ok, $result:ident) => { + $crate::mem::heap_move_ok($result) + }; + (move_some, $result:ident) => { + $crate::mem::heap_move_some($result) + }; +} + macro_rules! wrap_function { ( $module:ident; $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $block:block; ) => { ::paste::paste! { - $(#[$meta])* + $(#[$meta])+ #[doc = ""] #[doc = " This function is part of the `" $module "` module."] #[no_mangle] @@ -191,10 +206,14 @@ macro_rules! wrap_function { $( let $param_name = $crate::macros::apply_param_modifier!($param_modifier, $param_name); )* - $block + let result = $block; + $( + let result = $crate::macros::apply_return_modifier!($return_modifier, result); + )? + result } } - } + }; } macro_rules! wrap_functions { @@ -203,7 +222,7 @@ macro_rules! wrap_functions { $( $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $block:block; )+ ) => { @@ -211,7 +230,7 @@ macro_rules! wrap_functions { $( $crate::macros::wrap_function!($module; $(#[$meta])+ - fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_modifier $return_type)? $block; ); )+ @@ -222,7 +241,7 @@ macro_rules! wrap_functions { $( $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $block:block; )+ ) => { @@ -230,7 +249,7 @@ macro_rules! wrap_functions { $crate::macros::wrap_functions!{[< $object_type:lower >]; $( $(#[$meta])+ - fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_modifier $return_type)? $block; )+ } @@ -239,7 +258,7 @@ macro_rules! wrap_functions { } pub(crate) use { - apply_param_modifier, derive_clone, derive_free, - wrap_fields, wrap_fields_accessor, wrap_function, - wrap_functions, wrap_method, wrap_methods, + apply_param_modifier, apply_return_modifier, derive_clone, derive_free, + wrap_fields, wrap_fields_accessor, wrap_function, wrap_functions, + wrap_method, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index 4f1d38f..abfa562 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -3,7 +3,6 @@ use crate::{ macros::{ derive_clone, derive_free, wrap_fields, wrap_functions, wrap_methods, }, - mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; @@ -12,22 +11,22 @@ wrap_functions!(associate Packet; /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise - fn try_load(data: val ByteSlice) -> *mut Packet { + fn try_load(data: val ByteSlice) -> move_ok *mut Packet { let data = unsafe { data.as_slice() }; - heap_move_ok(servicepoint::Packet::try_from(data)) + servicepoint::Packet::try_from(data) }; /// Creates a raw [Packet] from parts. /// /// returns: new instance. Will never return null. - fn from_parts(header: val Header, payload: val ByteSlice) -> NonNull { + fn from_parts(header: val Header, payload: val ByteSlice) -> move NonNull { let payload = if payload == ByteSlice::INVALID { None } else { Some(Vec::from(unsafe { payload.as_slice() })) }; - heap_move_nonnull(Packet { header, payload }) + Packet { header, payload } }; ); @@ -44,7 +43,7 @@ wrap_methods! { Packet; /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. /// /// The returned memory can be changed and will be valid until a new payload is set. - fn get_payload(mut packet) -> ByteSlice { + fn get_payload(mut packet) -> val ByteSlice { match &mut packet.payload { None => ByteSlice::INVALID, Some(payload) => unsafe { ByteSlice::from_slice(payload) }, @@ -67,7 +66,7 @@ wrap_methods! { Packet; /// # Panics /// /// - if the buffer is not big enough to hold header+payload. - fn serialize_to(mut packet, buffer: val ByteSlice) -> usize { + fn serialize_to(mut packet, buffer: val ByteSlice) -> val usize { unsafe { packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0) } @@ -79,7 +78,7 @@ wrap_functions!(sp; /// Converts u16 into [CommandCode]. /// /// If the provided value is not valid, false is returned and result is not changed. - fn u16_to_command_code(code: val u16, result: mut NonNull) -> bool { + fn u16_to_command_code(code: val u16, result: mut NonNull) -> val bool { match CommandCode::try_from(code) { Ok(code) => { *result = code; diff --git a/src/udp.rs b/src/udp.rs index a12281f..ba9b336 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,7 +1,7 @@ use crate::{ commands::{CommandTag, GenericCommand}, macros::{derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_ok, heap_remove}, + mem::heap_remove, }; use servicepoint::{Header, Packet, UdpSocketExt}; use std::{ @@ -13,7 +13,6 @@ use std::{ derive_free!(UdpSocket); wrap_functions!(associate UdpSocket; - /// Creates a new instance of [UdpSocket]. /// /// returns: NULL if connection fails, or connected instance @@ -25,11 +24,11 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open(host: val NonNull) -> *mut UdpSocket { + fn open(host: val NonNull) -> move_ok *mut UdpSocket { let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); - heap_move_ok(UdpSocket::bind_connect(host)) + UdpSocket::bind_connect(host) }; /// Creates a new instance of [UdpSocket]. @@ -43,11 +42,10 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket { + fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> move_ok *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); - heap_move_ok(UdpSocket::bind_connect(addr)) + UdpSocket::bind_connect(addr) }; - ); wrap_methods! {UdpSocket; @@ -56,7 +54,7 @@ wrap_methods! {UdpSocket; /// The passed `packet` gets consumed. /// /// returns: true in case of success - fn send_packet(ref connection, packet: move NonNull) -> bool { + fn send_packet(ref connection, packet: move NonNull) -> val bool { connection.send(&Vec::from(packet)).is_ok() }; @@ -71,7 +69,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - fn send_command(ref connection, command: mut NonNull) -> bool { + fn send_command(ref connection, command: mut NonNull) -> val bool { unsafe { let result = match command.tag { CommandTag::Invalid => return false, @@ -100,7 +98,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - fn send_header(ref udp_connection, header: val Header) -> bool { + fn send_header(ref udp_connection, header: val Header) -> val bool { let packet = Packet { header, payload: None, From 82696b2d1ae49042dab5944f5fef67a7159fd437 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 19:26:41 +0200 Subject: [PATCH 17/17] add slice modifier --- src/containers/bitmap.rs | 7 ++----- src/containers/bitvec.rs | 7 ++----- src/containers/brightness_grid.rs | 7 +++---- src/containers/byte_slice.rs | 1 + src/containers/char_grid.rs | 3 +-- src/containers/cp437_grid.rs | 7 ++----- src/macros.rs | 6 ++++++ src/packet.rs | 3 +-- 8 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index b47cfbc..b6ed4f3 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -56,9 +56,8 @@ wrap_functions!(associate Bitmap; fn load( width: val usize, height: val usize, - data: val ByteSlice, + data: slice ByteSlice, ) -> move_ok *mut Bitmap { - let data = unsafe { data.as_slice() }; Bitmap::load(width, height, data) }; @@ -97,7 +96,5 @@ wrap_methods!(Bitmap; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - fn data_ref_mut(mut instance) -> val ByteSlice { - unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } - }; + fn data_ref_mut(mut instance) -> slice ByteSlice; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 50a4eee..b16dd92 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -28,8 +28,7 @@ wrap_functions!(associate DisplayBitVec; /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// /// returns: [DisplayBitVec] instance containing data. - fn load(data: val ByteSlice) -> move NonNull { - let data = unsafe { data.as_slice() }; + fn load(data: slice ByteSlice) -> move NonNull { DisplayBitVec::from_slice(data) }; ); @@ -98,7 +97,5 @@ wrap_methods!(DisplayBitVec; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - fn as_raw_mut_slice(mut instance) -> val ByteSlice { - unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } - }; + fn as_raw_mut_slice(mut instance) -> slice ByteSlice; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index f7cd099..19f6b7a 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -40,9 +40,8 @@ wrap_functions!(associate BrightnessGrid; fn load( width: val usize, height: val usize, - data: val ByteSlice, + data: slice ByteSlice, ) -> move_some *mut BrightnessGrid { - let data = unsafe { data.as_slice() }; ByteGrid::load(width, height, data) .map(move |grid| grid.map(Brightness::saturating_from)) }; @@ -64,13 +63,13 @@ wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - fn data_ref_mut(mut instance) -> val ByteSlice { + fn data_ref_mut(mut instance) -> slice ByteSlice { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); let br_slice = instance.data_ref_mut(); unsafe { - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + transmute::<&mut [Brightness], &mut [u8]>(br_slice) } }; ); diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index d12cd88..81380e9 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -33,6 +33,7 @@ impl ByteSlice { }; pub(crate) unsafe fn as_slice(&self) -> &[u8] { + assert!(!self.start.is_null()); unsafe { std::slice::from_raw_parts(self.start, self.length) } } diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index a43f93d..98a2c5b 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -28,8 +28,7 @@ wrap_functions!(associate CharGrid; /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_ok *mut CharGrid { - let data = unsafe { data.as_slice() }; + fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_ok *mut CharGrid { CharGrid::load_utf8(width, height, data.to_vec()) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index e19ff72..62f0eb2 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -18,8 +18,7 @@ wrap_functions!(associate Cp437Grid; }; /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_some *mut Cp437Grid { - let data = unsafe { data.as_slice() }; + fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_some *mut Cp437Grid { Cp437Grid::load(width, height, data) }; ); @@ -40,7 +39,5 @@ wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - fn data_ref_mut(mut instance) -> val ByteSlice { - unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } - }; + fn data_ref_mut(mut instance) -> slice ByteSlice; ); diff --git a/src/macros.rs b/src/macros.rs index 086c901..79cd3af 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -169,6 +169,9 @@ macro_rules! apply_param_modifier { (ref, $param_name:ident) => { unsafe { $param_name.as_ref() } }; + (slice, $param_name:ident) => { + unsafe { $param_name.as_slice() } + }; } macro_rules! apply_return_modifier { @@ -184,6 +187,9 @@ macro_rules! apply_return_modifier { (move_some, $result:ident) => { $crate::mem::heap_move_some($result) }; + (slice, $result:ident) => { + unsafe { ByteSlice::from_slice($result) } + }; } macro_rules! wrap_function { diff --git a/src/packet.rs b/src/packet.rs index abfa562..bec75e5 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -11,8 +11,7 @@ wrap_functions!(associate Packet; /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise - fn try_load(data: val ByteSlice) -> move_ok *mut Packet { - let data = unsafe { data.as_slice() }; + fn try_load(data: slice ByteSlice) -> move_ok *mut Packet { servicepoint::Packet::try_from(data) };