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) } }