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
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 {
0 commit comments