@@ -307,9 +307,9 @@ macro_rules! __fn_args {
307
307
/// Rust struct).
308
308
///
309
309
/// 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` .
313
313
///
314
314
/// The class is guaranteed to have been created and registered with the
315
315
/// Objective-C runtime after the [`ClassType::class`] function has been
@@ -406,6 +406,8 @@ macro_rules! __fn_args {
406
406
///
407
407
/// unsafe impl ClassType for MyCustomObject {
408
408
/// type Super = NSObject;
409
+ /// // Optionally specify a different name
410
+ /// // const NAME: &'static str = "MyCustomObject";
409
411
/// }
410
412
///
411
413
/// unsafe impl MyCustomObject {
@@ -575,6 +577,8 @@ macro_rules! declare_class {
575
577
unsafe impl ClassType for $for: ty {
576
578
$( #[ inherits( $( $inheritance_rest: ty) ,+) ] ) ?
577
579
type Super = $superclass: ty;
580
+
581
+ $( const NAME : & ' static str = $name_const: literal; ) ?
578
582
}
579
583
580
584
$( $methods: tt) *
@@ -612,7 +616,7 @@ macro_rules! declare_class {
612
616
// Creation
613
617
unsafe impl ClassType for $for {
614
618
type Super = $superclass;
615
- const NAME : & ' static str = stringify !( $name) ;
619
+ const NAME : & ' static str = $crate :: __select_name !( $name; $ ( $name_const ) ? ) ;
616
620
617
621
fn class( ) -> & ' static $crate:: runtime:: Class {
618
622
// TODO: Use `core::cell::LazyCell`
@@ -624,7 +628,7 @@ macro_rules! declare_class {
624
628
let superclass = <$superclass as $crate:: ClassType >:: class( ) ;
625
629
let err_str = concat!(
626
630
"could not create new class " ,
627
- stringify !( $name) ,
631
+ $crate :: __select_name !( $name; $ ( $name_const ) ? ) ,
628
632
". Perhaps a class with that name already exists?" ,
629
633
) ;
630
634
let mut builder = $crate:: declare:: ClassBuilder :: new( Self :: NAME , superclass) . expect( err_str) ;
@@ -656,7 +660,7 @@ macro_rules! declare_class {
656
660
} ) ;
657
661
658
662
// 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( )
660
664
}
661
665
662
666
#[ inline]
@@ -690,6 +694,17 @@ macro_rules! declare_class {
690
694
} ;
691
695
}
692
696
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
+
693
708
#[ doc( hidden) ]
694
709
#[ macro_export]
695
710
macro_rules! __declare_class_methods {
0 commit comments