Skip to content

Commit b10c663

Browse files
committed
Add ClassType::NAME
1 parent c712e0c commit b10c663

File tree

8 files changed

+25
-3
lines changed

8 files changed

+25
-3
lines changed

objc2/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
* Added `IvarDrop<T>` to allow storing complex `Drop` values in ivars
1414
(currently `rc::Id<T, O>`, `Box<T>`, `Option<rc::Id<T, O>>` or
1515
`Option<Box<T>>`).
16+
* **BREAKING**: Added required `ClassType::NAME` constant for statically
17+
determining the name of a specific class.
1618

1719
### Removed
1820
* **BREAKING**: `MaybeUninit` no longer implements `IvarType` directly; use
1921
`Ivar::write` instead.
2022

23+
2124
## 0.3.0-beta.2 - 2022-08-28
2225

2326
### Added

objc2/examples/class_with_lifetime.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ impl<'a> MyObject<'a> {
7878

7979
unsafe impl<'a> ClassType for MyObject<'a> {
8080
type Super = NSObject;
81+
const NAME: &'static str = "MyObject";
8182

8283
fn class() -> &'static Class {
8384
// TODO: Use std::lazy::LazyCell
8485
static REGISTER_CLASS: Once = Once::new();
8586

8687
REGISTER_CLASS.call_once(|| {
8788
let superclass = NSObject::class();
88-
let mut builder = ClassBuilder::new("MyObject", superclass).unwrap();
89+
let mut builder = ClassBuilder::new(Self::NAME, superclass).unwrap();
8990

9091
builder.add_static_ivar::<NumberIvar<'a>>();
9192

objc2/src/class_type.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ use crate::Message;
1818
///
1919
/// The class returned by [`Self::class`] must be a subclass of the class that
2020
/// [`Self::Super`] represents, and `as_super`/`as_super_mut` must be
21-
/// implemented correctly.
21+
/// implemented correctly. Finally [`Self::NAME`] must be correct.
2222
///
2323
/// In pseudocode:
2424
/// ```ignore
2525
/// Self::class().superclass() == <Self::Super as ClassType>::class()
26+
/// Self::class().name() == Self::NAME
2627
/// ```
2728
///
2829
///
@@ -68,6 +69,9 @@ pub unsafe trait ClassType: Message {
6869
/// [`runtime::Object`]: crate::runtime::Object
6970
type Super: Message;
7071

72+
/// The name of the Objective-C class that this type represents.
73+
const NAME: &'static str;
74+
7175
/// Get a reference to the Objective-C class that this type represents.
7276
///
7377
/// May register the class with the runtime if it wasn't already.

objc2/src/foundation/object.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ __inner_extern_class! {
1717

1818
unsafe impl ClassType for NSObject {
1919
type Super = Object;
20+
const NAME: &'static str = "NSObject";
2021

2122
#[inline]
2223
fn class() -> &'static Class {

objc2/src/macros/declare_class.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ macro_rules! declare_class {
612612
// Creation
613613
unsafe impl ClassType for $for {
614614
type Super = $superclass;
615+
const NAME: &'static str = stringify!($name);
615616

616617
fn class() -> &'static $crate::runtime::Class {
617618
// TODO: Use `core::cell::LazyCell`
@@ -626,7 +627,7 @@ macro_rules! declare_class {
626627
stringify!($name),
627628
". Perhaps a class with that name already exists?",
628629
);
629-
let mut builder = $crate::declare::ClassBuilder::new(stringify!($name), superclass).expect(err_str);
630+
let mut builder = $crate::declare::ClassBuilder::new(Self::NAME, superclass).expect(err_str);
630631

631632
// Ivars
632633
$(

objc2/src/macros/extern_class.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ macro_rules! __inner_extern_class {
254254

255255
unsafe impl<$($t_for $(: $b_for)?),*> ClassType for $for {
256256
type Super = $superclass;
257+
const NAME: &'static str = stringify!($name);
257258

258259
#[inline]
259260
fn class() -> &'static $crate::runtime::Class {

objc2/src/rc/test_object.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,13 @@ impl RcTestObject {
162162
unsafe { Id::new(msg_send![Self::class(), new]) }.unwrap()
163163
}
164164
}
165+
166+
#[cfg(test)]
167+
mod tests {
168+
use super::*;
169+
170+
#[test]
171+
fn ensure_declared_name() {
172+
assert_eq!(RcTestObject::class().name(), RcTestObject::NAME);
173+
}
174+
}

tests/src/test_object.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ unsafe impl RefEncode for MyTestObject {
2222

2323
unsafe impl ClassType for MyTestObject {
2424
type Super = NSObject;
25+
const NAME: &'static str = "MyTestObject";
2526

2627
fn class() -> &'static Class {
2728
class!(MyTestObject)

0 commit comments

Comments
 (0)