add move fn to dsl, rename SPCommand to GenericCommand, remove DisplayBitVec command

This commit is contained in:
Vinzenz Schroeter 2025-06-23 20:26:07 +02:00
parent e434130784
commit 323ba6128e
13 changed files with 335 additions and 356 deletions

View file

@ -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<Packet>,
) -> SPCommand {
) -> NonNull<GenericCommand> {
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() })
}
}
}
);
};
}

View file

@ -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())
}
);
}