derive try_into_packet automatically

This commit is contained in:
Vinzenz Schroeter 2025-06-22 12:27:42 +02:00
parent 8116375fd0
commit 5ecb84ed16
9 changed files with 102 additions and 100 deletions

View file

@ -906,7 +906,7 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command,
size_t origin_y); 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. * Returns: NULL or a [Packet] containing the command.
* *
@ -1054,7 +1054,7 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance,
BinaryOperation value); 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. * 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_x,
size_t */*notnull*/ origin_y); 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. * 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_x,
size_t origin_y); 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. *Clones a [`CharGrid`] instance.
* *
@ -1470,6 +1470,15 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command,
size_t origin_x, size_t origin_x,
size_t origin_y); 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. *Clones a [`ClearCommand`] instance.
* *
@ -1495,6 +1504,15 @@ void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance);
*/ */
struct ClearCommand */*notnull*/ sp_clearcommand_new(void); 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], * Moves the provided [CharGrid] into a new [CharGridCommand],
* leaving other fields as their default values. * 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_x,
size_t origin_y); 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], * Moves the provided [Cp437Grid] into a new [Cp437GridCommand],
* leaving other fields as their default values. * 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_x,
size_t origin_y); 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. * Clones an [SPCommand] instance.
* *
@ -1780,6 +1780,15 @@ void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command
size_t origin_x, size_t origin_x,
size_t origin_y); 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`]. * 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); 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. *Clones a [`GlobalBrightnessCommand`] instance.
* *
@ -1928,13 +1946,6 @@ void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/
*/ */
Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); 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. * 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, void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance,
Brightness value); 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. *Clones a [`HardResetCommand`] instance.
* *
@ -1977,6 +1997,15 @@ void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance);
*/ */
struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); 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. *Clones a [`Packet`] instance.
* *

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
commands::{wrap_command, wrap_origin_accessors}, commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions}, 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; use std::ptr::NonNull;
wrap_command!(Bitmap); wrap_command!(Bitmap);
@ -43,13 +43,4 @@ wrap_functions!(associate BitmapCommand;
) -> NonNull<BitmapCommand> { ) -> NonNull<BitmapCommand> {
heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) 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<BitmapCommand>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
); );

View file

@ -1,11 +1,10 @@
use crate::{ use crate::{
commands::wrap_command, commands::wrap_command,
macros::{wrap_fields, wrap_functions}, macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_move_ok, heap_remove}, mem::{heap_move_nonnull, heap_remove},
}; };
use servicepoint::{ use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
Packet,
}; };
use std::ptr::NonNull; 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<BitVecCommand>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
); );

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
commands::{wrap_command, wrap_origin_accessors}, commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions}, 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; use std::ptr::NonNull;
wrap_command!(BrightnessGrid); wrap_command!(BrightnessGrid);
@ -40,13 +40,4 @@ wrap_functions!(associate BrightnessGridCommand;
heap_move_nonnull(unsafe { heap_remove(grid) }.into()) 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<BrightnessGridCommand>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
); );

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
commands::{wrap_command, wrap_origin_accessors}, commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions}, 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; use std::ptr::NonNull;
wrap_command!(CharGrid); wrap_command!(CharGrid);
@ -40,13 +40,4 @@ wrap_functions!(cmd_chargrid;
heap_move_nonnull(unsafe { heap_remove(grid) }.into()) 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<CharGridCommand>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
); );

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
commands::{wrap_command, wrap_origin_accessors}, commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions}, 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; use std::ptr::NonNull;
wrap_command!(Cp437Grid); wrap_command!(Cp437Grid);
@ -40,13 +40,4 @@ wrap_functions!(cmd_cp437grid;
heap_move_nonnull(unsafe { heap_remove(grid) }.into()) 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<Cp437GridCommand>,
) -> *mut Packet {
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
); );

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
commands::wrap_command, commands::wrap_command,
macros::{wrap_fields, wrap_functions}, 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; use std::ptr::NonNull;
wrap_functions!(associate GlobalBrightnessCommand; wrap_functions!(associate GlobalBrightnessCommand;
@ -15,11 +15,6 @@ wrap_functions!(associate GlobalBrightnessCommand;
heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) heap_move_nonnull(GlobalBrightnessCommand::from(brightness))
} }
/// Turns the command into a packet
fn into_packet(command: NonNull<GlobalBrightnessCommand>) -> NonNull<Packet> {
heap_move_nonnull(unsafe { heap_remove(command) }.into())
}
); );
wrap_command!(GlobalBrightness); wrap_command!(GlobalBrightness);

View file

@ -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 { macro_rules! wrap_command {
($command:ident, $object_type:ident) => { ($command:ident, $object_type:ident) => {
$crate::macros::wrap_clone!($object_type); $crate::macros::wrap_clone!($object_type);
$crate::macros::wrap_free!($object_type); $crate::macros::wrap_free!($object_type);
$crate::commands::derive_command_from!($command); $crate::commands::derive_command_from!($command);
$crate::commands::derive_command_into_packet!($object_type);
}; };
($command:ident) => { ($command:ident) => {
::paste::paste! { ::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,
};

View file

@ -36,7 +36,10 @@ impl ByteSlice {
unsafe { std::slice::from_raw_parts(self.start, self.length) } 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] { pub(crate) unsafe fn as_slice_mut(&self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } unsafe { std::slice::from_raw_parts_mut(self.start, self.length) }
} }