simplify property exports

This commit is contained in:
Vinzenz Schroeter 2025-06-22 14:52:44 +02:00
parent 7a836783e1
commit 92ce27af68
11 changed files with 70 additions and 119 deletions

View file

@ -20,7 +20,7 @@ int main(void) {
sp_chargrid_set(grid, 3, 1, 'l'); sp_chargrid_set(grid, 3, 1, 'l');
sp_chargrid_set(grid, 4, 1, 'd'); 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) if (packet == NULL)
return 1; return 1;
sp_udpsocket_send_packet(sock, packet); sp_udpsocket_send_packet(sock, packet);

View file

@ -26,7 +26,7 @@ int main(void) {
BrightnessGrid *grid = sp_brightnessgrid_new(TILE_WIDTH, TILE_HEIGHT); BrightnessGrid *grid = sp_brightnessgrid_new(TILE_WIDTH, TILE_HEIGHT);
make_brightness_pattern(grid); 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) if (packet == NULL)
return -2; return -2;

View file

@ -11,7 +11,7 @@ int main(void) {
sp_bitmap_fill(pixels, true); 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) if (packet == NULL)
return 1; return 1;

View file

@ -9,8 +9,8 @@ use std::ptr::NonNull;
wrap_command!(Bitmap); wrap_command!(Bitmap);
wrap_fields!(BitmapCommand; wrap_fields!(BitmapCommand;
prop bitmap: Bitmap { mut get(); move set(value); }; prop bitmap: Bitmap { get mut; set move; };
prop compression: CompressionCode { get(); set(value); }; prop compression: CompressionCode { get; set; };
); );
wrap_origin_accessors!(BitmapCommand); wrap_origin_accessors!(BitmapCommand);

View file

@ -11,10 +11,10 @@ use std::ptr::NonNull;
wrap_command!(BitVec); wrap_command!(BitVec);
wrap_fields!(BitVecCommand; wrap_fields!(BitVecCommand;
prop bitvec: DisplayBitVec { mut get(); move set(value); }; prop bitvec: DisplayBitVec { get mut; set move; };
prop offset: Offset { get(); set(value); }; prop offset: Offset { get; set; };
prop operation: BinaryOperation { get(); set(value); }; prop operation: BinaryOperation { get; set; };
prop compression: CompressionCode { get(); set(value); }; prop compression: CompressionCode { get; set; };
); );
wrap_functions!(associate BitVecCommand; wrap_functions!(associate BitVecCommand;

View file

@ -9,7 +9,7 @@ use std::ptr::NonNull;
wrap_command!(BrightnessGrid); wrap_command!(BrightnessGrid);
wrap_fields!(BrightnessGridCommand; wrap_fields!(BrightnessGridCommand;
prop grid: BrightnessGrid { mut get(); move set(grid); }; prop grid: BrightnessGrid { get mut; set move; };
); );
wrap_origin_accessors!(BrightnessGridCommand); wrap_origin_accessors!(BrightnessGridCommand);

View file

@ -9,7 +9,7 @@ use std::ptr::NonNull;
wrap_command!(CharGrid); wrap_command!(CharGrid);
wrap_fields!(CharGridCommand; wrap_fields!(CharGridCommand;
prop grid: CharGrid { mut get(); move set(grid); }; prop grid: CharGrid { get mut; set move; };
); );
wrap_origin_accessors!(CharGridCommand); wrap_origin_accessors!(CharGridCommand);

View file

@ -9,7 +9,7 @@ use std::ptr::NonNull;
wrap_command!(Cp437Grid); wrap_command!(Cp437Grid);
wrap_fields!(Cp437GridCommand; wrap_fields!(Cp437GridCommand;
prop grid: Cp437Grid { mut get(); move set(grid); }; prop grid: Cp437Grid { get mut; set move; };
); );
wrap_origin_accessors!(Cp437GridCommand); wrap_origin_accessors!(Cp437GridCommand);

View file

@ -20,8 +20,5 @@ wrap_functions!(associate GlobalBrightnessCommand;
wrap_command!(GlobalBrightness); wrap_command!(GlobalBrightness);
wrap_fields!(GlobalBrightnessCommand; wrap_fields!(GlobalBrightnessCommand;
prop brightness: Brightness { prop brightness: Brightness { get; set; };
get();
set(value);
};
); );

View file

@ -55,7 +55,7 @@ macro_rules! wrap_methods {
$crate::macros::wrap_functions!([< $object_type:lower >]; $crate::macros::wrap_functions!([< $object_type:lower >];
$( $(
#[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."]
#[doc = ""] ///
$(#[$meta])* $(#[$meta])*
fn $function( fn $function(
instance: ::core::ptr::NonNull<$object_type>, instance: ::core::ptr::NonNull<$object_type>,
@ -83,132 +83,90 @@ macro_rules! wrap_methods {
}; };
} }
macro_rules! wrap_fields { macro_rules! wrap_fields_accessor {
( (get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
$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;)?
})?;
)?
};
)+
) => {
paste::paste! { paste::paste! {
$crate::macros::wrap_functions!([< $object_type:lower >]; $crate::macros::wrap_functions! {associate $object_type;
$( #[doc = " Gets the value of field `" $prop_name
$( "` of the [`servicepoint::" $object_type "`]."]
#[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."]
$($(
#[doc = ""]
#[$get_meta]
)*)?
fn [<get _ $prop_name>]( fn [<get _ $prop_name>](
instance: ::core::ptr::NonNull<$object_type> instance: ::core::ptr::NonNull<$object_type>
) -> $prop_type { ) -> $prop_type {
let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name }; let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name };
$($(
let $prop_name = $get_expr;
)?)?
return $prop_name; return $prop_name;
} }
)? }
}
$( };
#[doc = concat!(" Gets a reference to the field `", stringify!($prop_name), (mut get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
"` of the [`servicepoint::",stringify!($object_type),"`].")] paste::paste! {
$($( $crate::macros::wrap_functions! {associate $object_type;
#[doc = ""] #[doc = " Gets a reference to the field `" $prop_name
#[$get_mut_meta] "` of the [`servicepoint::" $object_type "`]."]
)*)? ///
#[doc = ""] /// - The returned reference inherits the lifetime of object in which it is contained.
#[doc = " - 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.
#[doc = " - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command."]
fn [<get _ $prop_name _mut>]( fn [<get _ $prop_name _mut>](
instance: ::core::ptr::NonNull<$object_type> instance: ::core::ptr::NonNull<$object_type>
) -> ::core::ptr::NonNull<$prop_type> { ) -> ::core::ptr::NonNull<$prop_type> {
let $prop_name = unsafe { &mut $crate::macros::nonnull_as_mut!(instance).$prop_name }; 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); return ::core::ptr::NonNull::from($prop_name);
} }
)? }
}
$( };
#[doc = concat!(" Sets the value of field `", stringify!($prop_name), (set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
"` of the [`servicepoint::",stringify!($object_type),"`].")] paste::paste! {
$($( $crate::macros::wrap_functions! {associate $object_type;
#[doc = ""] #[doc = " Sets the value of field `" $prop_name
#[$set_meta] "` of the [`servicepoint::" $object_type "`]."]
)*)?
fn [<set _ $prop_name>]( fn [<set _ $prop_name>](
instance: ::core::ptr::NonNull<$object_type>, instance: ::core::ptr::NonNull<$object_type>,
value: $prop_type, value: $prop_type,
) { ) {
let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) };
$($(
let $value = value;
let value = $set_expr;
)?)?
instance.$prop_name = value; instance.$prop_name = value;
} }
)? }
}
$( };
#[doc = concat!(" Sets the value of field `", stringify!($prop_name), (move set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
"` of the [`servicepoint::",stringify!($object_type),"`].")] paste::paste! {
#[doc = concat!(" The provided value is moved into the instance, ", $crate::macros::wrap_functions! {associate $object_type;
"potentially invalidating previously taken references.")] #[doc = " Sets the value of field `" $prop_name
$($( "` of the [`servicepoint::" $object_type "`]."]
#[doc = ""] /// The provided value is moved into the instance, potentially invalidating previously taken references.
#[$set_move_meta]
)*)?
fn [<set _ $prop_name>]( fn [<set _ $prop_name>](
instance: ::core::ptr::NonNull<$object_type>, instance: ::core::ptr::NonNull<$object_type>,
value: ::core::ptr::NonNull<$prop_type>, value: ::core::ptr::NonNull<$prop_type>,
) { ) {
let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) };
let value = unsafe { $crate::mem::heap_remove(value) }; let $prop_name = unsafe { $crate::mem::heap_remove(value) };
$($( instance.$prop_name = $prop_name;
let $set_move_value = value;
let value = $set_move_expr;
)?)?
instance.$prop_name = value;
} }
)? }
)+
);
} }
}; };
} }
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 { macro_rules! wrap_functions {
( (
$module:ident; $module:ident;
@ -255,5 +213,5 @@ macro_rules! wrap_functions {
pub(crate) use { pub(crate) use {
derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields,
wrap_functions, wrap_methods, wrap_functions, wrap_methods, wrap_fields_accessor
}; };

View file

@ -72,11 +72,7 @@ derive_clone!(Packet);
derive_free!(Packet); derive_free!(Packet);
wrap_fields!(Packet; wrap_fields!(Packet;
prop header: Header { prop header: Header { get; get mut; set; };
get();
mut get();
set(value);
};
); );
wrap_functions!(sp; wrap_functions!(sp;