From 970040bdcdf37ed549c439798463f85264ac3b93 Mon Sep 17 00:00:00 2001 From: silvanshade Date: Sun, 29 Jan 2023 10:47:17 -0700 Subject: [PATCH] Allow ivar names to be elided --- crates/icrate/examples/delegate.rs | 12 +++++------ crates/objc2/src/macros/__field_helpers.rs | 23 ++++++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/icrate/examples/delegate.rs b/crates/icrate/examples/delegate.rs index c7f75b6a2..e2dbddb48 100644 --- a/crates/icrate/examples/delegate.rs +++ b/crates/icrate/examples/delegate.rs @@ -14,12 +14,12 @@ extern "C" {} declare_class!( #[derive(Debug)] struct CustomAppDelegate { - pub ivar: IvarEncode, - another_ivar: IvarBool<"_another_ivar">, - box_ivar: IvarDrop, "_box_ivar">, - maybe_box_ivar: IvarDrop>, "_maybe_box_ivar">, - id_ivar: IvarDrop, "_id_ivar">, - maybe_id_ivar: IvarDrop>, "_maybe_id_ivar">, + pub ivar: IvarEncode, + another_ivar: IvarBool, + box_ivar: IvarDrop>, + maybe_box_ivar: IvarDrop>>, + id_ivar: IvarDrop>, + maybe_id_ivar: IvarDrop>>, } mod ivars; diff --git a/crates/objc2/src/macros/__field_helpers.rs b/crates/objc2/src/macros/__field_helpers.rs index 3a18a3b82..46ea20106 100644 --- a/crates/objc2/src/macros/__field_helpers.rs +++ b/crates/objc2/src/macros/__field_helpers.rs @@ -180,7 +180,7 @@ macro_rules! __parse_fields { ( ( $(#[$m:meta])* - $vis:vis $field_name:ident: IvarDrop<$ty:ty, $ivar_name:literal> + $vis:vis $field_name:ident: IvarDrop<$ty:ty $(, $ivar_name:literal)?> $(, $($rest_fields:tt)*)? ) ($($ivar_helper_module_v:vis mod $ivar_helper_module:ident)?) @@ -208,7 +208,7 @@ macro_rules! __parse_fields { // - Caller upholds that the ivars are properly initialized. unsafe impl $crate::declare::IvarType for $field_name { type Type = IvarDrop<$ty>; - const NAME: &'static $crate::__macro_helpers::str = $ivar_name; + const NAME: &'static $crate::__macro_helpers::str = $crate::__select_ivar_name!($field_name; $($ivar_name)?); } ) ($($ivar_type_name)* $field_name) ( @@ -227,7 +227,7 @@ macro_rules! __parse_fields { ( ( $(#[$m:meta])* - $vis:vis $field_name:ident: IvarEncode<$ty:ty, $ivar_name:literal> + $vis:vis $field_name:ident: IvarEncode<$ty:ty $(, $ivar_name:literal)?> $(, $($rest_fields:tt)*)? ) ($($ivar_helper_module_v:vis mod $ivar_helper_module:ident)?) @@ -252,7 +252,7 @@ macro_rules! __parse_fields { // SAFETY: See above unsafe impl $crate::declare::IvarType for $field_name { type Type = IvarEncode<$ty>; - const NAME: &'static $crate::__macro_helpers::str = $ivar_name; + const NAME: &'static $crate::__macro_helpers::str = $crate::__select_ivar_name!($field_name; $($ivar_name)?); } ) ($($ivar_type_name)* $field_name) ( @@ -271,7 +271,7 @@ macro_rules! __parse_fields { ( ( $(#[$m:meta])* - $vis:vis $field_name:ident: IvarBool<$ivar_name:literal> + $vis:vis $field_name:ident: IvarBool$(<$ivar_name:literal>)? $(, $($rest_fields:tt)*)? ) ($($ivar_helper_module_v:vis mod $ivar_helper_module:ident)?) @@ -296,7 +296,7 @@ macro_rules! __parse_fields { // SAFETY: See above unsafe impl $crate::declare::IvarType for $field_name { type Type = IvarBool; - const NAME: &'static $crate::__macro_helpers::str = $ivar_name; + const NAME: &'static $crate::__macro_helpers::str = $crate::__select_ivar_name!($field_name; $($ivar_name)?); } ) ($($ivar_type_name)* $field_name) ( @@ -349,3 +349,14 @@ macro_rules! __parse_fields { } } } + +#[doc(hidden)] +#[macro_export] +macro_rules! __select_ivar_name { + ($field_name:ident; $ivar_name:literal) => { + $ivar_name + }; + ($field_name:ident;) => { + concat!('_', stringify!($field_name)) + }; +}