Skip to content

Commit 1054a7b

Browse files
committed
Rename ClassDecl to ClassBuilder to be more in line with Rust naming
Also `ProtocolDecl` to `ProtocolBuilder`
1 parent 52ff35d commit 1054a7b

File tree

5 files changed

+69
-60
lines changed

5 files changed

+69
-60
lines changed

objc2-foundation/examples/class_with_lifetime.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::marker::PhantomData;
33
use std::sync::Once;
44

5-
use objc2::declare::ClassDecl;
5+
use objc2::declare::ClassBuilder;
66
use objc2::rc::{Id, Owned, Shared};
77
use objc2::runtime::{Class, Object, Sel};
88
use objc2::{msg_send, sel};
@@ -48,8 +48,8 @@ impl<'a> MyObject<'a> {
4848
fn class() -> &'static Class {
4949
MYOBJECT_REGISTER_CLASS.call_once(|| {
5050
let superclass = NSObject::class();
51-
let mut decl = ClassDecl::new("MyObject", superclass).unwrap();
52-
decl.add_ivar::<Option<&mut u8>>("_number_ptr");
51+
let mut builder = ClassBuilder::new("MyObject", superclass).unwrap();
52+
builder.add_ivar::<Option<&mut u8>>("_number_ptr");
5353

5454
unsafe extern "C" fn init_with_ptr(
5555
this: *mut Object,
@@ -68,10 +68,10 @@ impl<'a> MyObject<'a> {
6868
unsafe {
6969
let init_with_ptr: unsafe extern "C" fn(*mut Object, Sel, *mut u8) -> *mut Object =
7070
init_with_ptr;
71-
decl.add_method(sel!(initWithPtr:), init_with_ptr);
71+
builder.add_method(sel!(initWithPtr:), init_with_ptr);
7272
}
7373

74-
decl.register();
74+
builder.register();
7575
});
7676

7777
Class::get("MyObject").unwrap()

objc2-foundation/examples/custom_class.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Once;
22

3-
use objc2::declare::ClassDecl;
3+
use objc2::declare::ClassBuilder;
44
use objc2::rc::{Id, Owned};
55
use objc2::runtime::{Class, Object, Sel};
66
use objc2::{msg_send, sel};
@@ -39,8 +39,8 @@ impl MYObject {
3939
fn class() -> &'static Class {
4040
MYOBJECT_REGISTER_CLASS.call_once(|| {
4141
let superclass = NSObject::class();
42-
let mut decl = ClassDecl::new("MYObject", superclass).unwrap();
43-
decl.add_ivar::<u32>("_number");
42+
let mut builder = ClassBuilder::new("MYObject", superclass).unwrap();
43+
builder.add_ivar::<u32>("_number");
4444

4545
// Add ObjC methods for getting and setting the number
4646
extern "C" fn my_object_set_number(this: &mut MYObject, _cmd: Sel, number: u32) {
@@ -53,12 +53,12 @@ impl MYObject {
5353

5454
unsafe {
5555
let set_number: extern "C" fn(&mut MYObject, Sel, u32) = my_object_set_number;
56-
decl.add_method(sel!(setNumber:), set_number);
56+
builder.add_method(sel!(setNumber:), set_number);
5757
let get_number: extern "C" fn(&MYObject, Sel) -> u32 = my_object_get_number;
58-
decl.add_method(sel!(number), get_number);
58+
builder.add_method(sel!(number), get_number);
5959
}
6060

61-
decl.register();
61+
builder.register();
6262
});
6363

6464
Class::get("MYObject").unwrap()

objc2/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
3636
```
3737
* Allow specifying any receiver `T: Message` for methods added with
3838
`ClassBuilder::add_method`.
39+
* Renamed `ClassDecl` and `ProtocolDecl` to `ClassBuilder` and
40+
`ProtocolBuilder`. The old names are kept as deprecated aliases.
3941

4042
### Fixed
4143
* Properly sealed the `MessageArguments` trait (it already had a hidden

objc2/src/declare.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Functionality for declaring Objective-C classes.
22
//!
3-
//! Classes can be declared using the [`ClassDecl`] struct. Instance variables
3+
//! Classes can be declared using the [`ClassBuilder`] struct. Instance variables
44
//! and methods can then be added before the class is ultimately registered.
55
//!
66
//! # Example
@@ -11,11 +11,11 @@
1111
//!
1212
//! ```no_run
1313
//! use objc2::{class, sel};
14-
//! use objc2::declare::ClassDecl;
14+
//! use objc2::declare::ClassBuilder;
1515
//! use objc2::runtime::{Class, Object, Sel};
1616
//!
1717
//! let superclass = class!(NSObject);
18-
//! let mut decl = ClassDecl::new("MyNumber", superclass).unwrap();
18+
//! let mut decl = ClassBuilder::new("MyNumber", superclass).unwrap();
1919
//!
2020
//! // Add an instance variable
2121
//! decl.add_ivar::<u32>("_number");
@@ -124,10 +124,14 @@ fn log2_align_of<T>() -> u8 {
124124
/// A type for declaring a new class and adding new methods and ivars to it
125125
/// before registering it.
126126
#[derive(Debug)]
127-
pub struct ClassDecl {
127+
pub struct ClassBuilder {
128128
cls: NonNull<Class>,
129129
}
130130

131+
#[doc(hidden)]
132+
#[deprecated = "Use `ClassBuilder` instead."]
133+
pub type ClassDecl = ClassBuilder;
134+
131135
// SAFETY: The stuff that touch global state does so using locks internally.
132136
//
133137
// Modifying the class itself can only be done through `&mut`, so Sync is
@@ -138,11 +142,11 @@ pub struct ClassDecl {
138142
// when doing so...).
139143
//
140144
// Finally, there are no requirements that the class must be registered on the
141-
// same thread that allocated it.
142-
unsafe impl Send for ClassDecl {}
143-
unsafe impl Sync for ClassDecl {}
145+
// same thread that allocated it (so Send is safe).
146+
unsafe impl Send for ClassBuilder {}
147+
unsafe impl Sync for ClassBuilder {}
144148

145-
impl ClassDecl {
149+
impl ClassBuilder {
146150
fn as_ptr(&self) -> *mut ffi::objc_class {
147151
self.cls.as_ptr().cast()
148152
}
@@ -154,16 +158,16 @@ impl ClassDecl {
154158
NonNull::new(cls.cast()).map(|cls| Self { cls })
155159
}
156160

157-
/// Constructs a [`ClassDecl`] with the given name and superclass.
161+
/// Constructs a [`ClassBuilder`] with the given name and superclass.
158162
///
159163
/// Returns [`None`] if the class couldn't be allocated, or a class with
160164
/// that name already exist.
161165
pub fn new(name: &str, superclass: &Class) -> Option<Self> {
162166
Self::with_superclass(name, Some(superclass))
163167
}
164168

165-
/// Constructs a [`ClassDecl`] declaring a new root class with the given
166-
/// name.
169+
/// Constructs a [`ClassBuilder`] declaring a new root class with the
170+
/// given name.
167171
///
168172
/// Returns [`None`] if the class couldn't be allocated.
169173
///
@@ -177,11 +181,10 @@ impl ClassDecl {
177181
/// Functionality it expects, like implementations of `-retain` and
178182
/// `-release` used by ARC, will not be present otherwise.
179183
pub fn root(name: &str, intitialize_fn: extern "C" fn(&Class, Sel)) -> Option<Self> {
180-
let mut decl = Self::with_superclass(name, None);
181-
if let Some(ref mut decl) = decl {
182-
unsafe { decl.add_class_method(sel!(initialize), intitialize_fn) };
183-
}
184-
decl
184+
Self::with_superclass(name, None).map(|mut this| {
185+
unsafe { this.add_class_method(sel!(initialize), intitialize_fn) };
186+
this
187+
})
185188
}
186189

187190
/// Adds a method with the given name and implementation.
@@ -295,8 +298,8 @@ impl ClassDecl {
295298

296299
// fn add_property(&self, name: &str, attributes: &[ffi::objc_property_attribute_t]);
297300

298-
/// Registers the [`ClassDecl`], consuming it, and returns a reference to
299-
/// the newly registered [`Class`].
301+
/// Registers the [`ClassBuilder`], consuming it, and returns a reference
302+
/// to the newly registered [`Class`].
300303
pub fn register(self) -> &'static Class {
301304
// Forget self, otherwise the class will be disposed in drop
302305
let cls = ManuallyDrop::new(self).cls;
@@ -305,7 +308,7 @@ impl ClassDecl {
305308
}
306309
}
307310

308-
impl Drop for ClassDecl {
311+
impl Drop for ClassBuilder {
309312
fn drop(&mut self) {
310313
unsafe { ffi::objc_disposeClassPair(self.as_ptr()) }
311314
}
@@ -314,20 +317,24 @@ impl Drop for ClassDecl {
314317
/// A type for declaring a new protocol and adding new methods to it
315318
/// before registering it.
316319
#[derive(Debug)]
317-
pub struct ProtocolDecl {
320+
pub struct ProtocolBuilder {
318321
proto: NonNull<Protocol>,
319322
}
320323

321-
// SAFETY: Similar to ClassDecl
322-
unsafe impl Send for ProtocolDecl {}
323-
unsafe impl Sync for ProtocolDecl {}
324+
#[doc(hidden)]
325+
#[deprecated = "Use `ProtocolBuilder` instead."]
326+
pub type ProtocolDecl = ProtocolBuilder;
327+
328+
// SAFETY: Similar to ClassBuilder
329+
unsafe impl Send for ProtocolBuilder {}
330+
unsafe impl Sync for ProtocolBuilder {}
324331

325-
impl ProtocolDecl {
332+
impl ProtocolBuilder {
326333
fn as_ptr(&self) -> *mut ffi::objc_protocol {
327334
self.proto.as_ptr().cast()
328335
}
329336

330-
/// Constructs a [`ProtocolDecl`] with the given name.
337+
/// Constructs a [`ProtocolBuilder`] with the given name.
331338
///
332339
/// Returns [`None`] if the protocol couldn't be allocated.
333340
pub fn new(name: &str) -> Option<Self> {
@@ -391,7 +398,7 @@ impl ProtocolDecl {
391398
}
392399
}
393400

394-
/// Registers the [`ProtocolDecl`], consuming it and returning a reference
401+
/// Registers the [`ProtocolBuilder`], consuming it and returning a reference
395402
/// to the newly registered [`Protocol`].
396403
pub fn register(self) -> &'static Protocol {
397404
unsafe {

objc2/src/test_utils.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::ops::{Deref, DerefMut};
22
use std::os::raw::c_char;
33
use std::sync::Once;
44

5-
use crate::declare::{ClassDecl, ProtocolDecl};
5+
use crate::declare::{ClassBuilder, ProtocolBuilder};
66
use crate::runtime::{Class, Object, Protocol, Sel};
77
use crate::{ffi, Encode, Encoding, MessageReceiver};
88

@@ -75,11 +75,11 @@ pub(crate) fn custom_class() -> &'static Class {
7575
// The runtime will call this method, so it has to be implemented
7676
extern "C" fn custom_obj_class_initialize(_this: &Class, _cmd: Sel) {}
7777

78-
let mut decl = ClassDecl::root("CustomObject", custom_obj_class_initialize).unwrap();
78+
let mut builder = ClassBuilder::root("CustomObject", custom_obj_class_initialize).unwrap();
7979
let proto = custom_protocol();
8080

81-
decl.add_protocol(proto);
82-
decl.add_ivar::<u32>("_foo");
81+
builder.add_protocol(proto);
82+
builder.add_ivar::<u32>("_foo");
8383

8484
extern "C" fn custom_obj_set_foo(this: &mut Object, _cmd: Sel, foo: u32) {
8585
unsafe {
@@ -121,22 +121,22 @@ pub(crate) fn custom_class() -> &'static Class {
121121

122122
unsafe {
123123
let set_foo: extern "C" fn(&mut Object, Sel, u32) = custom_obj_set_foo;
124-
decl.add_method(sel!(setFoo:), set_foo);
124+
builder.add_method(sel!(setFoo:), set_foo);
125125
let get_foo: extern "C" fn(&Object, Sel) -> u32 = custom_obj_get_foo;
126-
decl.add_method(sel!(foo), get_foo);
126+
builder.add_method(sel!(foo), get_foo);
127127
let get_struct: extern "C" fn(&Object, Sel) -> CustomStruct = custom_obj_get_struct;
128-
decl.add_method(sel!(customStruct), get_struct);
128+
builder.add_method(sel!(customStruct), get_struct);
129129
let class_method: extern "C" fn(&Class, Sel) -> u32 = custom_obj_class_method;
130-
decl.add_class_method(sel!(classFoo), class_method);
130+
builder.add_class_method(sel!(classFoo), class_method);
131131

132132
let protocol_instance_method: extern "C" fn(&mut Object, Sel, u32) = custom_obj_set_bar;
133-
decl.add_method(sel!(setBar:), protocol_instance_method);
133+
builder.add_method(sel!(setBar:), protocol_instance_method);
134134
let protocol_class_method: extern "C" fn(&Class, Sel, i32, i32) -> i32 =
135135
custom_obj_add_number_to_number;
136-
decl.add_class_method(sel!(addNumber:toNumber:), protocol_class_method);
136+
builder.add_class_method(sel!(addNumber:toNumber:), protocol_class_method);
137137
}
138138

139-
decl.register();
139+
builder.register();
140140
});
141141

142142
class!(CustomObject)
@@ -146,13 +146,13 @@ pub(crate) fn custom_protocol() -> &'static Protocol {
146146
static REGISTER_CUSTOM_PROTOCOL: Once = Once::new();
147147

148148
REGISTER_CUSTOM_PROTOCOL.call_once(|| {
149-
let mut decl = ProtocolDecl::new("CustomProtocol").unwrap();
149+
let mut builder = ProtocolBuilder::new("CustomProtocol").unwrap();
150150

151-
decl.add_method_description::<(i32,), ()>(sel!(setBar:), true);
152-
decl.add_method_description::<(), *const c_char>(sel!(getName), false);
153-
decl.add_class_method_description::<(i32, i32), i32>(sel!(addNumber:toNumber:), true);
151+
builder.add_method_description::<(i32,), ()>(sel!(setBar:), true);
152+
builder.add_method_description::<(), *const c_char>(sel!(getName), false);
153+
builder.add_class_method_description::<(i32, i32), i32>(sel!(addNumber:toNumber:), true);
154154

155-
decl.register();
155+
builder.register();
156156
});
157157

158158
Protocol::get("CustomProtocol").unwrap()
@@ -163,12 +163,12 @@ pub(crate) fn custom_subprotocol() -> &'static Protocol {
163163

164164
REGISTER_CUSTOM_SUBPROTOCOL.call_once(|| {
165165
let super_proto = custom_protocol();
166-
let mut decl = ProtocolDecl::new("CustomSubProtocol").unwrap();
166+
let mut builder = ProtocolBuilder::new("CustomSubProtocol").unwrap();
167167

168-
decl.add_protocol(super_proto);
169-
decl.add_method_description::<(u32,), u32>(sel!(calculateFoo:), true);
168+
builder.add_protocol(super_proto);
169+
builder.add_method_description::<(u32,), u32>(sel!(calculateFoo:), true);
170170

171-
decl.register();
171+
builder.register();
172172
});
173173

174174
Protocol::get("CustomSubProtocol").unwrap()
@@ -183,7 +183,7 @@ pub(crate) fn custom_subclass() -> &'static Class {
183183

184184
REGISTER_CUSTOM_SUBCLASS.call_once(|| {
185185
let superclass = custom_class();
186-
let mut decl = ClassDecl::new("CustomSubclassObject", superclass).unwrap();
186+
let mut builder = ClassBuilder::new("CustomSubclassObject", superclass).unwrap();
187187

188188
extern "C" fn custom_subclass_get_foo(this: &Object, _cmd: Sel) -> u32 {
189189
let foo: u32 = unsafe { msg_send![super(this, custom_class()), foo] };
@@ -192,10 +192,10 @@ pub(crate) fn custom_subclass() -> &'static Class {
192192

193193
unsafe {
194194
let get_foo: extern "C" fn(&Object, Sel) -> u32 = custom_subclass_get_foo;
195-
decl.add_method(sel!(foo), get_foo);
195+
builder.add_method(sel!(foo), get_foo);
196196
}
197197

198-
decl.register();
198+
builder.register();
199199
});
200200

201201
class!(CustomSubclassObject)

0 commit comments

Comments
 (0)