File tree 5 files changed +24
-9
lines changed 5 files changed +24
-9
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
14
14
(default) feature flag ` "foundation" ` .
15
15
* Added ` declare_class! ` , ` extern_class! ` and ` ns_string! ` macros from
16
16
` objc2-foundation ` .
17
+ * Added helper method ` ClassBuilder::add_static_ivar ` .
17
18
18
19
### Changed
19
20
* ** BREAKING** : Change selector syntax in ` declare_class! ` macro to be more Rust-like.
Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ impl<'a> MyObject<'a> {
71
71
let superclass = NSObject :: class ( ) ;
72
72
let mut builder = ClassBuilder :: new ( "MyObject" , superclass) . unwrap ( ) ;
73
73
74
- builder. add_ivar :: < < NumberIvar < ' a > as IvarType > :: Type > ( < NumberIvar < ' a > > :: NAME ) ;
74
+ builder. add_static_ivar :: < NumberIvar < ' a > > ( ) ;
75
75
76
76
/// Helper struct since we can't access the instance variable
77
77
/// from inside MyObject, since it hasn't been initialized yet!
Original file line number Diff line number Diff line change @@ -402,9 +402,11 @@ impl ClassBuilder {
402
402
403
403
/// Adds an ivar with type `T` and the provided name.
404
404
///
405
+ ///
405
406
/// # Panics
406
407
///
407
- /// If the ivar wasn't successfully added.
408
+ /// If the ivar wasn't successfully added (for example, there already was
409
+ /// an ivar with that name).
408
410
pub fn add_ivar < T : Encode > ( & mut self , name : & str ) {
409
411
let c_name = CString :: new ( name) . unwrap ( ) ;
410
412
let encoding = CString :: new ( T :: ENCODING . to_string ( ) ) . unwrap ( ) ;
@@ -422,6 +424,16 @@ impl ClassBuilder {
422
424
assert ! ( success. as_bool( ) , "Failed to add ivar {}" , name) ;
423
425
}
424
426
427
+ /// Adds an instance variable from an [`IvarType`].
428
+ ///
429
+ ///
430
+ /// # Panics
431
+ ///
432
+ /// Same as [`ClassBuilder::add_ivar`].
433
+ pub fn add_static_ivar < T : IvarType > ( & mut self ) {
434
+ self . add_ivar :: < T :: Type > ( T :: NAME ) ;
435
+ }
436
+
425
437
/// Adds the given protocol to self.
426
438
///
427
439
/// # Panics
Original file line number Diff line number Diff line change @@ -72,10 +72,14 @@ unsafe impl<T: IvarType> IvarType for MaybeUninit<T> {
72
72
/// particular, this is never safe to have on the stack by itself.
73
73
///
74
74
/// Additionally, the instance variable described by `T` must be available on
75
- /// the specific instance, and be of the exact same type.
75
+ /// the specific instance, and be of the exact same type. When declaring the
76
+ /// object yourself, you can ensure this using
77
+ /// [`ClassBuilder::add_static_ivar`].
76
78
///
77
79
/// Finally, two ivars with the same name must not be used on the same object.
78
80
///
81
+ /// [`ClassBuilder::add_static_ivar`]: crate::declare::ClassBuilder::add_static_ivar
82
+ ///
79
83
///
80
84
/// # Examples
81
85
///
@@ -106,7 +110,7 @@ unsafe impl<T: IvarType> IvarType for MaybeUninit<T> {
106
110
/// # use objc2::declare::ClassBuilder;
107
111
/// # let mut builder = ClassBuilder::new("MyObject", class!(NSObject)).unwrap();
108
112
/// // Declare the class and add the instance variable to it
109
- /// builder.add_ivar ::<< MyCustomIvar as IvarType>::Type>(MyCustomIvar::NAME );
113
+ /// builder.add_static_ivar ::<MyCustomIvar>( );
110
114
/// # let _cls = builder.register();
111
115
///
112
116
/// let obj: MyObject;
Original file line number Diff line number Diff line change @@ -495,8 +495,7 @@ macro_rules! declare_class {
495
495
unsafe $v struct $name<>: $inherits, $( $inheritance_rest, ) * $crate:: runtime:: Object {
496
496
// SAFETY:
497
497
// - The ivars are in a type used as an Objective-C object.
498
- // - The instance variable is defined in the exact same manner
499
- // in `class` below.
498
+ // - The ivar is added to the class below.
500
499
// - Rust prevents having two fields with the same name.
501
500
// - Caller upholds that the ivars are properly initialized.
502
501
$( $ivar_v $ivar: $crate:: declare:: Ivar <$ivar>, ) *
@@ -527,10 +526,9 @@ macro_rules! declare_class {
527
526
) ;
528
527
let mut builder = $crate:: declare:: ClassBuilder :: new( stringify!( $name) , superclass) . expect( err_str) ;
529
528
529
+ // Ivars
530
530
$(
531
- builder. add_ivar:: <<$ivar as $crate:: declare:: IvarType >:: Type >(
532
- <$ivar as $crate:: declare:: IvarType >:: NAME
533
- ) ;
531
+ builder. add_static_ivar:: <$ivar>( ) ;
534
532
) *
535
533
536
534
$(
You can’t perform that action at this time.
0 commit comments