Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ivar names to be elided #411

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/icrate/examples/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ extern "C" {}
declare_class!(
#[derive(Debug)]
struct CustomAppDelegate {
pub ivar: IvarEncode<u8, "_ivar">,
another_ivar: IvarBool<"_another_ivar">,
box_ivar: IvarDrop<Box<i32>, "_box_ivar">,
maybe_box_ivar: IvarDrop<Option<Box<i32>>, "_maybe_box_ivar">,
id_ivar: IvarDrop<Id<NSString, Shared>, "_id_ivar">,
maybe_id_ivar: IvarDrop<Option<Id<NSString, Shared>>, "_maybe_id_ivar">,
pub ivar: IvarEncode<u8>,
another_ivar: IvarBool,
box_ivar: IvarDrop<Box<i32>>,
maybe_box_ivar: IvarDrop<Option<Box<i32>>>,
id_ivar: IvarDrop<Id<NSString, Shared>>,
maybe_id_ivar: IvarDrop<Option<Id<NSString, Shared>>>,
}

mod ivars;
Expand Down
23 changes: 17 additions & 6 deletions crates/objc2/src/macros/__field_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
Expand Down Expand Up @@ -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)
(
Expand All @@ -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)?)
Expand All @@ -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)
(
Expand All @@ -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)?)
Expand All @@ -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)
(
Expand Down Expand Up @@ -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))
};
}