Skip to content

Commit d294f2b

Browse files
committed
Allow directly specifying class name in declare_class! macro
1 parent b10c663 commit d294f2b

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

objc2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1515
`Option<Box<T>>`).
1616
* **BREAKING**: Added required `ClassType::NAME` constant for statically
1717
determining the name of a specific class.
18+
* Allow directly specifying class name in declare_class! macro.
1819

1920
### Removed
2021
* **BREAKING**: `MaybeUninit` no longer implements `IvarType` directly; use

objc2/examples/delegate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {}
1111

1212
#[cfg(all(feature = "apple", target_os = "macos"))]
1313
extern_class!(
14+
#[derive(Debug)]
1415
struct NSResponder;
1516

1617
unsafe impl ClassType for NSResponder {
@@ -20,6 +21,7 @@ extern_class!(
2021

2122
#[cfg(all(feature = "apple", target_os = "macos"))]
2223
declare_class!(
24+
#[derive(Debug)]
2325
struct CustomAppDelegate {
2426
pub ivar: u8,
2527
another_ivar: bool,
@@ -32,6 +34,7 @@ declare_class!(
3234
unsafe impl ClassType for CustomAppDelegate {
3335
#[inherits(NSObject)]
3436
type Super = NSResponder;
37+
const NAME: &'static str = "MyCustomAppDelegate";
3538
}
3639

3740
unsafe impl CustomAppDelegate {
@@ -100,6 +103,7 @@ impl CustomAppDelegate {
100103
fn main() {
101104
let delegate = CustomAppDelegate::new(42, true);
102105

106+
println!("{:?}", delegate);
103107
println!("{:?}", delegate.ivar);
104108
println!("{:?}", delegate.another_ivar);
105109
println!("{:?}", delegate.box_ivar);

objc2/src/macros/declare_class.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ macro_rules! __fn_args {
307307
/// Rust struct).
308308
///
309309
/// Note that the class name should be unique across the entire application!
310-
/// As a tip, you can declare the class with the desired unique name like
311-
/// `MyCrateCustomObject` using this macro, and then expose a renamed type
312-
/// alias like `pub type CustomObject = MyCrateCustomObject;` instead.
310+
/// You can declare the class with the desired unique name like
311+
/// `"MyCrateCustomObject"` by specifying it in `ClassType::NAME`, and then
312+
/// give the exposed type a different name like `CustomObject`.
313313
///
314314
/// The class is guaranteed to have been created and registered with the
315315
/// Objective-C runtime after the [`ClassType::class`] function has been
@@ -406,6 +406,8 @@ macro_rules! __fn_args {
406406
///
407407
/// unsafe impl ClassType for MyCustomObject {
408408
/// type Super = NSObject;
409+
/// // Optionally specify a different name
410+
/// // const NAME: &'static str = "MyCustomObject";
409411
/// }
410412
///
411413
/// unsafe impl MyCustomObject {
@@ -575,6 +577,8 @@ macro_rules! declare_class {
575577
unsafe impl ClassType for $for:ty {
576578
$(#[inherits($($inheritance_rest:ty),+)])?
577579
type Super = $superclass:ty;
580+
581+
$(const NAME: &'static str = $name_const:literal;)?
578582
}
579583

580584
$($methods:tt)*
@@ -612,7 +616,7 @@ macro_rules! declare_class {
612616
// Creation
613617
unsafe impl ClassType for $for {
614618
type Super = $superclass;
615-
const NAME: &'static str = stringify!($name);
619+
const NAME: &'static str = $crate::__select_name!($name; $($name_const)?);
616620

617621
fn class() -> &'static $crate::runtime::Class {
618622
// TODO: Use `core::cell::LazyCell`
@@ -624,7 +628,7 @@ macro_rules! declare_class {
624628
let superclass = <$superclass as $crate::ClassType>::class();
625629
let err_str = concat!(
626630
"could not create new class ",
627-
stringify!($name),
631+
$crate::__select_name!($name; $($name_const)?),
628632
". Perhaps a class with that name already exists?",
629633
);
630634
let mut builder = $crate::declare::ClassBuilder::new(Self::NAME, superclass).expect(err_str);
@@ -656,7 +660,7 @@ macro_rules! declare_class {
656660
});
657661

658662
// We just registered the class, so it should be available
659-
$crate::runtime::Class::get(stringify!($name)).unwrap()
663+
$crate::runtime::Class::get(Self::NAME).unwrap()
660664
}
661665

662666
#[inline]
@@ -690,6 +694,17 @@ macro_rules! declare_class {
690694
};
691695
}
692696

697+
#[doc(hidden)]
698+
#[macro_export]
699+
macro_rules! __select_name {
700+
($_name:ident; $name_const:literal) => {
701+
$name_const
702+
};
703+
($name:ident;) => {
704+
$crate::__macro_helpers::stringify!($name)
705+
};
706+
}
707+
693708
#[doc(hidden)]
694709
#[macro_export]
695710
macro_rules! __declare_class_methods {

0 commit comments

Comments
 (0)