From 75e2df41fec1c064b53e6daa730e445266d7b488 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 17 Jun 2025 23:56:11 +0200 Subject: [PATCH] export value fields via macro --- include/servicepoint.h | 64 +++++++++++------- src/commands/bitmap_command.rs | 37 +++++------ src/commands/bitvec_command.rs | 75 +++++---------------- src/commands/global_brightness_command.rs | 26 +++----- src/macros.rs | 79 +++++++++++++++++++++-- src/packet.rs | 12 +++- 6 files changed, 165 insertions(+), 128 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 6c2da20..02d0688 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1166,9 +1166,9 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*not struct Bitmap */*notnull*/ sp_cmd_bitmap_get(struct BitmapCommand */*notnull*/ command); /** - * Reads the compression kind of the [BitmapCommand]. + *Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. */ -CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ command); +CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); /** * Reads the origin field of the [BitmapCommand]. @@ -1196,10 +1196,10 @@ void sp_cmd_bitmap_set(struct BitmapCommand */*notnull*/ command, struct Bitmap */*notnull*/ bitmap); /** - * Overwrites the compression kind of the [BitmapCommand]. + *Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. */ -void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ command, - CompressionCode compression); +void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, + CompressionCode value); /** * Overwrites the origin field of the [BitmapCommand]. @@ -1231,19 +1231,19 @@ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); BitVec *sp_cmd_bitvec_get(struct BitVecCommand */*notnull*/ command); /** - * Reads the compression kind of the [BitVecCommand]. + *Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. */ -CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ command); +CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); /** - * Reads the offset field of the [BitVecCommand]. + *Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. */ -Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ command); +Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); /** - * Returns the [BinaryOperation] of the command. + *Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. */ -BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ command); +BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); /** * Set pixel data starting at the pixel offset on screen. @@ -1271,22 +1271,22 @@ void sp_cmd_bitvec_set(struct BitVecCommand */*notnull*/ command, BitVec */*notnull*/ bitvec); /** - * Overwrites the compression kind of the [BitVecCommand]. + *Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. */ -void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ command, - CompressionCode compression); +void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, + CompressionCode value); /** - * Overwrites the offset field of the [BitVecCommand]. + *Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. */ -void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ command, - Offset offset); +void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, + Offset value); /** - * Overwrites the [BinaryOperation] of the command. + *Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. */ -void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ command, - BinaryOperation operation); +void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, + BinaryOperation value); /** * Tries to turn a [BitVecCommand] into a [Packet]. @@ -1305,7 +1305,10 @@ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struc */ void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance); -Brightness sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); +/** + *Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + */ +Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); @@ -1317,10 +1320,10 @@ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBri struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** - * Moves the provided bitmap to be contained in the command. + *Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. */ -void sp_cmd_brightness_global_set(struct GlobalBrightnessCommand */*notnull*/ command, - Brightness brightness); +void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, + Brightness value); /** *Clones a [BrightnessGridCommand] instance. @@ -1712,12 +1715,17 @@ void sp_packet_free(struct Packet */*notnull*/ instance); struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); +/** + *Gets the value of field `header` of the [`servicepoint::Packet`]. + */ +struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); + /** * Returns a pointer to the header field of the provided packet. * * The returned header can be changed and will be valid for the lifetime of the packet. */ -struct Header */*notnull*/ sp_packet_get_header(struct Packet */*notnull*/ packet); +struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ packet); /** * Returns a pointer to the current payload of the provided packet. @@ -1738,6 +1746,12 @@ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); void sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); +/** + *Sets the value of field `header` of the [`servicepoint::Packet`]. + */ +void sp_packet_set_header(struct Packet */*notnull*/ instance, + struct Header value); + /** * Sets the payload of the provided packet to the provided data. * diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 24400c7..3bfcf20 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; @@ -48,6 +48,22 @@ pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet( wrap_clone!(sp_cmd_bitmap::BitmapCommand); wrap_free!(sp_cmd_bitmap::BitmapCommand); +wrap_fields!( + sp_cmd_bitmap::BitmapCommand; + //prop bitmap: NonNull { + // get() { + // return NonNull::from(bitmap); + // }; + // set(value) { + // return unsafe { heap_remove(value) }; + // }; + //}; + prop compression: CompressionCode { + get(); + set(value); + }; +); + /// Returns a pointer to the provided `BitmapCommand`. /// /// # Safety @@ -97,22 +113,3 @@ pub unsafe extern "C" fn sp_cmd_bitmap_set_origin( command.as_mut().origin = Origin::new(origin_x, origin_y); } } - -/// Overwrites the compression kind of the [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( - mut command: NonNull, - compression: CompressionCode, -) { - unsafe { - command.as_mut().compression = compression; - } -} - -/// Reads the compression kind of the [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_get_compression( - command: NonNull, -) -> CompressionCode { - unsafe { command.as_ref().compression } -} diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index aeff1cd..7ccbf31 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -48,6 +48,22 @@ pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet( wrap_clone!(sp_cmd_bitvec::BitVecCommand); wrap_free!(sp_cmd_bitvec::BitVecCommand); +wrap_fields!( + sp_cmd_bitvec::BitVecCommand; + prop offset: Offset { + get(); + set(value); + }; + prop operation: BinaryOperation { + get(); + set(value); + }; + prop compression: CompressionCode { + get(); + set(value); + }; +); + /// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get( @@ -66,60 +82,3 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set( command.as_mut().bitvec = heap_remove(bitvec); } } - -/// Reads the offset field of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( - command: NonNull, -) -> Offset { - unsafe { command.as_ref().offset } -} - -/// Overwrites the offset field of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( - mut command: NonNull, - offset: Offset, -) { - unsafe { - command.as_mut().offset = offset; - } -} - -/// Returns the [BinaryOperation] of the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( - command: NonNull, -) -> BinaryOperation { - unsafe { command.as_ref().operation } -} - -/// Overwrites the [BinaryOperation] of the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_operation( - mut command: NonNull, - operation: BinaryOperation, -) { - unsafe { - command.as_mut().operation = operation; - } -} - -/// Overwrites the compression kind of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_compression( - mut command: NonNull, - compression: CompressionCode, -) { - unsafe { - command.as_mut().compression = compression; - } -} - -/// Reads the compression kind of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_compression( - command: NonNull, -) -> CompressionCode { - unsafe { command.as_ref().compression } -} diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 20a350a..e695ef0 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_remove}, }; use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; @@ -25,20 +25,10 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet( wrap_clone!(sp_cmd_brightness_global::GlobalBrightnessCommand); wrap_free!(sp_cmd_brightness_global::GlobalBrightnessCommand); -/// Moves the provided bitmap to be contained in the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_set( - mut command: NonNull, - brightness: Brightness, -) { - unsafe { - command.as_mut().brightness = brightness; - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_get( - mut command: NonNull, -) -> Brightness { - unsafe { command.as_mut().brightness } -} +wrap_fields!( + sp_cmd_brightness_global::GlobalBrightnessCommand; + prop brightness: Brightness { + get(); + set(value); + }; +); diff --git a/src/macros.rs b/src/macros.rs index 80256dd..df21e23 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -54,7 +54,7 @@ macro_rules! wrap_method { instance: NonNull<$object_type>, $($param_name: $param_type),* ) $(-> $return_type)? { - let instance = unsafe {$crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance)}; + let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; $(let $param_let_name = $param_let_expr;)* #[allow( unused_variables, @@ -62,16 +62,85 @@ macro_rules! wrap_method { let result = instance.$function($($param_name),*); $( $( - let $it = result; - let result = $return_expr; + let $it = result; + let result = $return_expr; )? - return result; + return result; )? } } }; } +macro_rules! wrap_fields { + ( + $prefix:ident :: $object_type:ty; + $( + prop $prop_name:ident : $prop_type:ty { + $( + get() $({ + $(#[$get_meta:meta])* + $(return $get_expr:expr;)? + })?; + )? + $( + set($value:ident) + $({ + $(#[$set_meta:meta])* + $(return $set_expr:expr;)? + })?; + )? + }; + )+ + ) => { + paste::paste! { + $( + $( + #[doc = concat!("Gets the value of field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] + $($( + #[doc = ""] + #[$get_meta] + )*)? + #[no_mangle] + pub unsafe extern "C" fn [<$prefix _ get _ $prop_name>]( + instance: NonNull<$object_type> + ) -> $prop_type { + let instance = unsafe { $crate::macros::nonnull_as_ref!(instance) }; + let $prop_name = instance.$prop_name; + $($( + let $prop_name = $get_expr; + )?)? + return $prop_name; + } + )? + + $( + #[doc = concat!("Sets the value of field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] + $($( + #[doc = ""] + #[$set_meta] + )*)? + #[no_mangle] + pub unsafe extern "C" fn [<$prefix _ set _ $prop_name>]( + instance: 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; + } + )? + )+ + } + }; +} + pub(crate) use { - nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_free, wrap_method, + nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_fields, wrap_free, + wrap_method, }; diff --git a/src/packet.rs b/src/packet.rs index a9b347c..af49fc8 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; @@ -36,7 +36,7 @@ pub unsafe extern "C" fn sp_packet_from_parts( /// /// The returned header can be changed and will be valid for the lifetime of the packet. #[no_mangle] -pub unsafe extern "C" fn sp_packet_get_header( +pub unsafe extern "C" fn sp_packet_get_header_mut( packet: NonNull, ) -> NonNull
{ NonNull::from(unsafe { &mut (*packet.as_ptr()).header }) @@ -94,6 +94,14 @@ pub unsafe extern "C" fn sp_packet_serialize_to( wrap_clone!(sp_packet::Packet); wrap_free!(sp_packet::Packet); +wrap_fields!( + sp_packet::Packet; + prop header: Header { + get(); + set(value); + }; +); + /// Converts u16 into [CommandCode]. /// /// If the provided value is not valid, false is returned and result is not changed.