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, 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)
return 1;
sp_udpsocket_send_packet(sock, packet);

View file

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

View file

@ -11,7 +11,7 @@ int main(void) {
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)
return 1;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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