Skip to content

Commit

Permalink
Remove hack to support CustomObject test utility
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 20, 2023
1 parent 247b933 commit b8bc138
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 60 deletions.
11 changes: 0 additions & 11 deletions crates/objc2/src/runtime/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomObject> {}
}

mod message_args_private {
pub trait Sealed {}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ mod tests {
let object = test_utils::custom_object();
assert_eq!(
format!("{:?}", &*object),
format!("<CustomObject: {:p}>", &*object)
format!("CustomObject(<CustomObject: {:p}>)", &*object)
);
}

Expand Down
81 changes: 33 additions & 48 deletions crates/objc2/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,54 @@
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<CustomObject> {
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
}
}

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
}
}

Expand Down Expand Up @@ -105,8 +86,10 @@ pub(crate) fn custom_class() -> &'static AnyClass {
builder.add_ivar::<u32>("_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) {
Expand Down Expand Up @@ -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<CustomObject> {
let ptr: *const AnyClass = custom_class();
unsafe { Id::new(ffi::class_createInstance(ptr.cast(), 0).cast()) }.unwrap()
}

pub(crate) fn custom_subclass() -> &'static AnyClass {
Expand Down Expand Up @@ -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<CustomObject> {
let ptr: *const AnyClass = custom_subclass();
unsafe { Id::new(ffi::class_createInstance(ptr.cast(), 0).cast()) }.unwrap()
}

0 comments on commit b8bc138

Please sign in to comment.