Skip to content

Commit 09f6049

Browse files
committed
Remove special-case for using () when there are no ivars
This means that users have to write the slightly more verbose `.set_ivars(Ivars::<Self> {})` instead of `.set_ivars(())`. This is probably fine, we should instead find better ways to avoid such situations in the first place, such as by using derive macros, see #267.
1 parent 78d1cf6 commit 09f6049

File tree

10 files changed

+22
-41
lines changed

10 files changed

+22
-41
lines changed

crates/objc2/src/__macros/define_class/ivars.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ mod tests {
516516
impl ImplsDrop {
517517
#[unsafe(method_id(init))]
518518
fn init(this: Allocated<Self>) -> Option<Retained<Self>> {
519-
unsafe { msg_send![super(this.set_ivars(())), init] }
519+
unsafe { msg_send![super(this.set_ivars(Ivars::<Self> {})), init] }
520520
}
521521
}
522522
);
@@ -533,7 +533,7 @@ mod tests {
533533
let _ = unsafe { init_only_superclasses(ImplsDrop::alloc()) };
534534
check([]);
535535

536-
let _ = unsafe { init_no_finalize(ImplsDrop::alloc(), ()) };
536+
let _ = unsafe { init_no_finalize(ImplsDrop::alloc(), Ivars::<ImplsDrop> {}) };
537537
check([]);
538538

539539
let _ = unsafe { init(ImplsDrop::alloc()) };
@@ -877,7 +877,7 @@ mod tests {
877877
}
878878
}
879879

880-
let obj = DropPanics::alloc().set_ivars(());
880+
let obj = DropPanics::alloc().set_ivars(Ivars::<DropPanics> {});
881881
let obj: Retained<DropPanics> = unsafe { msg_send![super(obj), init] };
882882
drop(obj);
883883
}
@@ -937,7 +937,7 @@ mod tests {
937937
}
938938
}
939939

940-
let obj = DropRetainsAndLeaksSelf::alloc().set_ivars(());
940+
let obj = DropRetainsAndLeaksSelf::alloc().set_ivars(Ivars::<DropRetainsAndLeaksSelf> {});
941941
let obj: Retained<DropRetainsAndLeaksSelf> = unsafe { msg_send![super(obj), init] };
942942
drop(obj);
943943

crates/objc2/src/__macros/define_class/mod.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,9 @@ macro_rules! __define_class_inner {
726726
$crate::__define_class_ivar_accessors!(($class) $($ivars)*);
727727

728728
impl $crate::DefinedClass for $class {
729-
type Ivars = $crate::__define_class_ivars_type!(
730-
(__Objc2IvarsContainer)
731-
$($ivars)*
732-
);
729+
// NOTE: We could have used `()` here when there are no ivars,
730+
// but it's more consistent to always use the container.
731+
type Ivars = __Objc2IvarsContainer;
733732

734733
#[inline]
735734
fn __ivars_offset() -> $crate::__macros::isize {
@@ -1016,17 +1015,3 @@ macro_rules! __define_class_ivar_accessors {
10161015
));
10171016
}
10181017
}
1019-
1020-
#[doc(hidden)]
1021-
#[macro_export]
1022-
macro_rules! __define_class_ivars_type {
1023-
(($ivars_ty:ident)) => {
1024-
// Default to `Ivars = ()` when no ivars are specified.
1025-
//
1026-
// We might remove this in the future, but let's keep it for now.
1027-
()
1028-
};
1029-
(($ivars_ty:ident) $($t:tt)*) => {
1030-
$ivars_ty
1031-
};
1032-
}

crates/objc2/src/__macros/msg_send/retained.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ mod tests {
320320

321321
use crate::rc::{autoreleasepool, Allocated, PartialInit, RcTestObject, ThreadTestData};
322322
use crate::runtime::{AnyObject, NSObject, NSObjectProtocol, NSZone};
323-
use crate::{class, define_class, extern_methods, msg_send, AnyThread};
323+
use crate::{class, define_class, extern_methods, msg_send, AnyThread, Ivars};
324324

325325
#[test]
326326
fn test_send_message_manuallydrop() {
@@ -616,7 +616,7 @@ mod tests {
616616
expected.drop += 1;
617617
expected.assert_current();
618618

619-
let obj = RcTestObject::alloc().set_ivars(());
619+
let obj = RcTestObject::alloc().set_ivars(Ivars::<RcTestObject> {});
620620
let _: Retained<RcTestObject> = unsafe { msg_send![super(obj), init] };
621621
expected.alloc += 1;
622622
expected.release += 1;
@@ -658,7 +658,7 @@ mod tests {
658658
ignore = "SIGSEGVs with the old runtime for some reason?"
659659
)]
660660
fn test_super_init_not_initialized() {
661-
let obj = RcTestObject::alloc().set_ivars(());
661+
let obj = RcTestObject::alloc().set_ivars(Ivars::<RcTestObject> {});
662662
let _: Retained<RcTestObject> =
663663
unsafe { msg_send![super(obj, RcTestObject::class()), init] };
664664
}

crates/objc2/src/rc/allocated_partial_init.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ mod tests {
361361
use super::*;
362362
use crate::rc::RcTestObject;
363363
use crate::runtime::NSObject;
364+
use crate::Ivars;
364365

365366
#[test]
366367
fn auto_traits() {
@@ -396,7 +397,7 @@ mod tests {
396397
fn test_set_ivars_null() {
397398
// SAFETY: The pointer is NULL
398399
let obj: Allocated<RcTestObject> = unsafe { Allocated::new(ptr::null_mut()) };
399-
let _ = obj.set_ivars(());
400+
let _ = obj.set_ivars(Ivars::<RcTestObject> {});
400401
}
401402

402403
#[test]

crates/objc2/src/rc/test_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::ptr;
44

55
use crate::rc::{Allocated, DefaultRetained, Retained};
66
use crate::runtime::{NSObject, NSObjectProtocol, NSZone};
7-
use crate::{define_class, msg_send, ClassType};
7+
use crate::{define_class, msg_send, ClassType, Ivars};
88

99
// TODO: Put tests that use this in another crate
1010
#[derive(Debug, Clone, Default, PartialEq, Eq)]
@@ -108,7 +108,7 @@ define_class!(
108108
#[unsafe(method_id(init))]
109109
unsafe fn init(this: Allocated<Self>) -> Retained<Self> {
110110
TEST_DATA.with(|data| data.borrow_mut().init += 1);
111-
let this = this.set_ivars(());
111+
let this = this.set_ivars(Ivars::<RcTestObject> {});
112112
unsafe { msg_send![super(this), init] }
113113
}
114114

crates/objc2/src/topics/run_loop.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ define_class!(
5656
#[derive(Debug)]
5757
struct AppDelegate {
5858
// Whatever state you want to store in your delegate.
59-
state: i32,
6059
}
6160
6261
impl AppDelegate {
@@ -66,7 +65,6 @@ define_class!(
6665
fn init(this: Allocated<Self>) -> Retained<Self> {
6766
let this = this.set_ivars(Ivars::<Self> {
6867
// Initialize state.
69-
state: 42,
7068
});
7169
unsafe { msg_send![super(this), init] }
7270
}

crates/objc2/tests/track_caller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::Mutex;
1010
use objc2::encode::Encode;
1111
use objc2::rc::{self, Allocated, Retained};
1212
use objc2::runtime::{self, NSObject};
13-
use objc2::{class, define_class, msg_send, AnyThread, ClassType};
13+
use objc2::{class, define_class, msg_send, AnyThread, ClassType, Ivars};
1414

1515
#[path = "../src/rc/test_object.rs"]
1616
#[allow(dead_code)]

crates/test-ui/ui/implement_protocol_missing_super.stderr

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/app/default_xcode_app/view_controller.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use objc2::rc::Retained;
22
use objc2::runtime::AnyObject;
3-
use objc2::{define_class, msg_send, MainThreadMarker, MainThreadOnly};
3+
use objc2::{define_class, msg_send, Ivars, MainThreadMarker, MainThreadOnly};
44
use objc2_app_kit::{NSResponder, NSViewController};
55
use objc2_foundation::{NSObject, NSObjectProtocol};
66

@@ -40,7 +40,7 @@ impl ViewController {
4040
// FIXME: Make it possible to avoid this boilerplate.
4141
pub fn new(mtm: MainThreadMarker) -> Retained<Self> {
4242
let this = Self::alloc(mtm);
43-
let this = this.set_ivars(());
43+
let this = this.set_ivars(Ivars::<Self> {});
4444
// SAFETY: `ViewController` is safe to initialize.
4545
unsafe { msg_send![super(this), init] }
4646
}

framework-crates/objc2-ui-kit/src/application.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,13 @@ impl UIApplication {
4444
/// #[derive(Debug)]
4545
/// struct AppDelegate {
4646
/// // Whatever state you want to store in your delegate.
47-
/// state: i32,
4847
/// }
4948
///
5049
/// impl AppDelegate {
5150
/// // Called by `UIApplication::main`.
5251
/// #[unsafe(method_id(init))]
5352
/// fn init(this: Allocated<Self>) -> Retained<Self> {
54-
/// let this = this.set_ivars(Ivars::<Self> {
55-
/// state: 42,
56-
/// });
53+
/// let this = this.set_ivars(Ivars::<Self> {});
5754
/// unsafe { msg_send![super(this), init] }
5855
/// }
5956
/// }

0 commit comments

Comments
 (0)