diff --git a/example/src/announce.c b/example/src/announce.c index 33da334..a0bdf9a 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -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); diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 9d17dc7..921bbd0 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -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; diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index 8838123..7a58c3a 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -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; diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 102e27d..01e3620 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -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); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 02cb8c8..e796380 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -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; diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7bd5aef..853a3fe 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -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); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index a0ad2db..f8d5107 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -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); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 7ccb1a3..4977195 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -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); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index ef7ce6e..76cbeb5 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -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; }; ); diff --git a/src/macros.rs b/src/macros.rs index 20259fc..8dcaf55 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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 []( 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 []( 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 []( 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 []( 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 }; diff --git a/src/packet.rs b/src/packet.rs index b719b8a..28ade85 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -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;