From b8bc1389c72f783c03b329be8ebd9b979dd2f1ca Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 20 Sep 2023 04:05:39 +0200 Subject: [PATCH] Remove hack to support CustomObject test utility --- crates/objc2/src/runtime/message.rs | 11 ---- crates/objc2/src/runtime/mod.rs | 2 +- crates/objc2/src/test_utils.rs | 81 ++++++++++++----------------- 3 files changed, 34 insertions(+), 60 deletions(-) diff --git a/crates/objc2/src/runtime/message.rs b/crates/objc2/src/runtime/message.rs index 27e36d3f7..6f7f9690b 100644 --- a/crates/objc2/src/runtime/message.rs +++ b/crates/objc2/src/runtime/message.rs @@ -635,17 +635,6 @@ unsafe impl<'a> MessageReceiver for &'a AnyClass { } } -#[cfg(test)] -mod test_utils_hack { - use super::*; - use crate::test_utils::CustomObject; - - // TODO: Remove the need for this hack - impl private::Sealed for &CustomObject {} - impl private::Sealed for &mut CustomObject {} - impl private::Sealed for ManuallyDrop {} -} - mod message_args_private { pub trait Sealed {} } diff --git a/crates/objc2/src/runtime/mod.rs b/crates/objc2/src/runtime/mod.rs index 3723032fd..235f49af4 100644 --- a/crates/objc2/src/runtime/mod.rs +++ b/crates/objc2/src/runtime/mod.rs @@ -1544,7 +1544,7 @@ mod tests { let object = test_utils::custom_object(); assert_eq!( format!("{:?}", &*object), - format!("", &*object) + format!("CustomObject()", &*object) ); } diff --git a/crates/objc2/src/test_utils.rs b/crates/objc2/src/test_utils.rs index 2dc1fadfd..f8ca1d897 100644 --- a/crates/objc2/src/test_utils.rs +++ b/crates/objc2/src/test_utils.rs @@ -1,50 +1,40 @@ -use core::mem::ManuallyDrop; use core::ops::{Deref, DerefMut}; use std::os::raw::c_char; use std::sync::Once; use crate::declare::{ClassBuilder, ProtocolBuilder}; -use crate::runtime::{AnyClass, AnyObject, AnyProtocol, MessageReceiver, Sel}; -use crate::{ffi, Encode, Encoding}; -use crate::{msg_send, sel}; +use crate::encode::{Encode, Encoding, RefEncode}; +use crate::rc::Id; +use crate::runtime::{AnyClass, AnyObject, AnyProtocol, Sel}; +use crate::{ffi, msg_send, mutability, sel, ClassType, Message}; #[derive(Debug)] -pub(crate) struct CustomObject { - obj: *mut AnyObject, -} +#[repr(C)] +pub(crate) struct CustomObject(AnyObject); -impl CustomObject { - fn new(class: &AnyClass) -> Self { - let ptr: *const AnyClass = class; - let obj = unsafe { ffi::class_createInstance(ptr.cast(), 0) }.cast(); - CustomObject { obj } - } +unsafe impl RefEncode for CustomObject { + const ENCODING_REF: Encoding = Encoding::Object; } -unsafe impl MessageReceiver for &CustomObject { - type __Inner = AnyObject; +unsafe impl Message for CustomObject {} - #[inline] - fn __as_raw_receiver(self) -> *mut AnyObject { - self.obj - } -} +unsafe impl ClassType for CustomObject { + type Super = AnyObject; + + type Mutability = mutability::Mutable; -unsafe impl MessageReceiver for &mut CustomObject { - type __Inner = AnyObject; + const NAME: &'static str = "CustomObject"; - #[inline] - fn __as_raw_receiver(self) -> *mut AnyObject { - self.obj + fn class() -> &'static AnyClass { + custom_class() } -} -unsafe impl MessageReceiver for ManuallyDrop { - type __Inner = AnyObject; + fn as_super(&self) -> &Self::Super { + &self.0 + } - #[inline] - fn __as_raw_receiver(self) -> *mut AnyObject { - self.obj + fn as_super_mut(&mut self) -> &mut Self::Super { + &mut self.0 } } @@ -52,22 +42,13 @@ impl Deref for CustomObject { type Target = AnyObject; fn deref(&self) -> &AnyObject { - unsafe { self.obj.as_ref().unwrap_unchecked() } + &self.0 } } impl DerefMut for CustomObject { fn deref_mut(&mut self) -> &mut AnyObject { - unsafe { self.obj.as_mut().unwrap_unchecked() } - } -} - -impl Drop for CustomObject { - fn drop(&mut self) { - unsafe { - #[allow(deprecated)] - ffi::object_dispose(self.obj.cast()); - } + &mut self.0 } } @@ -105,8 +86,10 @@ pub(crate) fn custom_class() -> &'static AnyClass { builder.add_ivar::("_foo"); unsafe extern "C" fn custom_obj_release(this: *mut AnyObject, _cmd: Sel) { - // Drop the value - let _ = CustomObject { obj: this }; + unsafe { + #[allow(deprecated)] + ffi::object_dispose(this.cast()); + } } extern "C" fn custom_obj_set_foo(this: &mut AnyObject, _cmd: Sel, foo: u32) { @@ -237,8 +220,9 @@ pub(crate) fn custom_subprotocol() -> &'static AnyProtocol { AnyProtocol::get("CustomSubProtocol").unwrap() } -pub(crate) fn custom_object() -> CustomObject { - CustomObject::new(custom_class()) +pub(crate) fn custom_object() -> Id { + let ptr: *const AnyClass = custom_class(); + unsafe { Id::new(ffi::class_createInstance(ptr.cast(), 0).cast()) }.unwrap() } pub(crate) fn custom_subclass() -> &'static AnyClass { @@ -270,6 +254,7 @@ pub(crate) fn custom_subclass() -> &'static AnyClass { AnyClass::get("CustomSubclassObject").unwrap() } -pub(crate) fn custom_subclass_object() -> CustomObject { - CustomObject::new(custom_subclass()) +pub(crate) fn custom_subclass_object() -> Id { + let ptr: *const AnyClass = custom_subclass(); + unsafe { Id::new(ffi::class_createInstance(ptr.cast(), 0).cast()) }.unwrap() }