From 1158e4cfe1eebf351bb3a413b22bc2ed8f6ef164 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 6 Jun 2022 17:43:02 +0200 Subject: [PATCH] Make `Id::as_ptr` an associated method --- objc2-foundation/src/attributed_string.rs | 4 ++-- objc2-foundation/src/mutable_attributed_string.rs | 4 ++-- objc2-foundation/src/mutable_string.rs | 4 ++-- objc2-foundation/src/string.rs | 4 ++-- objc2/benches/autorelease.rs | 4 ++-- objc2/src/message/mod.rs | 8 ++++---- objc2/src/rc/id.rs | 13 +++++-------- objc2/src/rc/weak_id.rs | 2 +- tests/src/test_object.rs | 4 ++-- 9 files changed, 22 insertions(+), 25 deletions(-) diff --git a/objc2-foundation/src/attributed_string.rs b/objc2-foundation/src/attributed_string.rs index 6df466507..f5dd4d9bb 100644 --- a/objc2-foundation/src/attributed_string.rs +++ b/objc2-foundation/src/attributed_string.rs @@ -153,11 +153,11 @@ mod tests { let s2 = s1.copy(); // NSAttributedString performs this optimization in GNUStep's runtime, // but not in Apple's; so we don't test for it! - // assert_eq!(s1.as_ptr(), s2.as_ptr()); + // assert_eq!(Id::as_ptr(&s1), Id::as_ptr(&s2)); assert!(s2.is_kind_of(NSAttributedString::class())); let s3 = s1.mutable_copy(); - assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSAttributedString); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *mut NSAttributedString); assert!(s3.is_kind_of(NSMutableAttributedString::class())); } } diff --git a/objc2-foundation/src/mutable_attributed_string.rs b/objc2-foundation/src/mutable_attributed_string.rs index 733e75830..8d407115a 100644 --- a/objc2-foundation/src/mutable_attributed_string.rs +++ b/objc2-foundation/src/mutable_attributed_string.rs @@ -90,11 +90,11 @@ mod tests { fn test_copy() { let s1 = NSMutableAttributedString::from_nsstring(&NSString::from_str("abc")); let s2 = s1.copy(); - assert_ne!(s1.as_ptr() as *const NSAttributedString, s2.as_ptr()); + assert_ne!(Id::as_ptr(&s1) as *mut NSAttributedString, Id::as_ptr(&s2)); assert!(s2.is_kind_of(NSAttributedString::class())); let s3 = s1.mutable_copy(); - assert_ne!(s1.as_ptr(), s3.as_ptr()); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3)); assert!(s3.is_kind_of(NSMutableAttributedString::class())); } } diff --git a/objc2-foundation/src/mutable_string.rs b/objc2-foundation/src/mutable_string.rs index f804b2398..46a7973ea 100644 --- a/objc2-foundation/src/mutable_string.rs +++ b/objc2-foundation/src/mutable_string.rs @@ -200,11 +200,11 @@ mod tests { fn test_copy() { let s1 = NSMutableString::from_str("abc"); let s2 = s1.copy(); - assert_ne!(s1.as_ptr(), s2.as_ptr() as *mut NSMutableString); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s2) as *mut NSMutableString); assert!(s2.is_kind_of(NSString::class())); let s3 = s1.mutable_copy(); - assert_ne!(s1.as_ptr(), s3.as_ptr()); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3)); assert!(s3.is_kind_of(NSMutableString::class())); } } diff --git a/objc2-foundation/src/string.rs b/objc2-foundation/src/string.rs index d06a06c3d..e9459c67c 100644 --- a/objc2-foundation/src/string.rs +++ b/objc2-foundation/src/string.rs @@ -346,11 +346,11 @@ mod tests { let s1 = NSString::from_str("abc"); let s2 = s1.copy(); // An optimization that NSString makes, since it is immutable - assert_eq!(s1.as_ptr(), s2.as_ptr()); + assert_eq!(Id::as_ptr(&s1), Id::as_ptr(&s2)); assert!(s2.is_kind_of(NSString::class())); let s3 = s1.mutable_copy(); - assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSString); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *mut NSString); assert!(s3.is_kind_of(NSMutableString::class())); } diff --git a/objc2/benches/autorelease.rs b/objc2/benches/autorelease.rs index a0bc82c81..5e0ae9ef3 100644 --- a/objc2/benches/autorelease.rs +++ b/objc2/benches/autorelease.rs @@ -62,7 +62,7 @@ fn new_nsdata() -> Id { } fn new_leaked_nsdata() -> *mut Object { - ManuallyDrop::new(new_nsdata()).as_ptr() + Id::as_ptr(&*ManuallyDrop::new(new_nsdata())) } fn autoreleased_nsdata() -> *mut Object { @@ -84,7 +84,7 @@ fn new_nsstring() -> Id { } fn new_leaked_nsstring() -> *mut Object { - ManuallyDrop::new(new_nsstring()).as_ptr() + Id::as_ptr(&*ManuallyDrop::new(new_nsstring())) } fn autoreleased_nsstring() -> *mut Object { diff --git a/objc2/src/message/mod.rs b/objc2/src/message/mod.rs index a76dd2b81..6a6a073e0 100644 --- a/objc2/src/message/mod.rs +++ b/objc2/src/message/mod.rs @@ -256,28 +256,28 @@ unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut T { unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Id { #[inline] fn as_raw_receiver(self) -> *mut Object { - self.as_ptr() as *mut Object + Id::as_ptr(self) as *mut Object } } unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut Id { #[inline] fn as_raw_receiver(self) -> *mut Object { - self.as_ptr() as *mut Object + Id::as_ptr(self) as *mut Object } } unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a ManuallyDrop> { #[inline] fn as_raw_receiver(self) -> *mut Object { - (**self).as_ptr() as *mut Object + Id::as_ptr(&**self) as *mut Object } } unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut ManuallyDrop> { #[inline] fn as_raw_receiver(self) -> *mut Object { - (**self).as_ptr() as *mut Object + Id::as_ptr(&mut **self) as *mut Object } } diff --git a/objc2/src/rc/id.rs b/objc2/src/rc/id.rs index ab9b95e7b..397b517b5 100644 --- a/objc2/src/rc/id.rs +++ b/objc2/src/rc/id.rs @@ -191,14 +191,11 @@ impl Id { /// Returns a raw pointer to the object. /// /// The pointer is valid for at least as long as the `Id` is held. + /// + /// This is an associated method, and must be called as `Id::as_ptr(obj)`. #[inline] - pub fn as_ptr(&self) -> *mut T { - // Note: This is not an associated function, which breaks the - // guideline that smart pointers shouldn't add inherent methods! - // - // However, this method is quite useful when migrating old codebases, - // so I think we'll let it be here for now. - self.ptr.as_ptr() + pub fn as_ptr(this: &Id) -> *mut T { + this.ptr.as_ptr() } } @@ -382,7 +379,7 @@ impl Id { // retained objects it is hard to imagine a case where the inner type // has a method with the same name. - let ptr = ManuallyDrop::new(self).as_ptr() as *mut ffi::objc_object; + let ptr = ManuallyDrop::new(self).ptr.as_ptr() as *mut ffi::objc_object; // SAFETY: The `ptr` is guaranteed to be valid and have at least one // retain count. // And because of the ManuallyDrop, we don't call the Drop diff --git a/objc2/src/rc/weak_id.rs b/objc2/src/rc/weak_id.rs index 3e19550da..973b9cd69 100644 --- a/objc2/src/rc/weak_id.rs +++ b/objc2/src/rc/weak_id.rs @@ -43,7 +43,7 @@ impl WeakId { // allow loading an `Id` later on. // SAFETY: `obj` is valid - unsafe { Self::new_inner(obj.as_ptr()) } + unsafe { Self::new_inner(Id::as_ptr(obj)) } } /// # Safety diff --git a/tests/src/test_object.rs b/tests/src/test_object.rs index ab9c182d3..4e9c598a6 100644 --- a/tests/src/test_object.rs +++ b/tests/src/test_object.rs @@ -179,12 +179,12 @@ fn test_object() { assert!(obj.var3().is_null()); assert!(obj.var3_ivar().is_null()); - let obj2 = ManuallyDrop::new(NSObject::new()).as_ptr().cast::(); + let obj2 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::(); obj.set_var3(obj2); assert_eq!(obj.var3(), obj2); assert_eq!(*obj.var3_ivar(), obj2); - let obj3 = ManuallyDrop::new(NSObject::new()).as_ptr().cast::(); + let obj3 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::(); *obj.var3_ivar_mut() = obj3; assert_ne!(obj.var3(), obj2); assert_ne!(*obj.var3_ivar(), obj2);