Skip to content

Commit

Permalink
Make Id::as_ptr an associated method
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 6, 2022
1 parent 0925705 commit 1158e4c
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions objc2-foundation/src/attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
4 changes: 2 additions & 2 deletions objc2-foundation/src/mutable_attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
4 changes: 2 additions & 2 deletions objc2-foundation/src/mutable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
4 changes: 2 additions & 2 deletions objc2-foundation/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
4 changes: 2 additions & 2 deletions objc2/benches/autorelease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn new_nsdata() -> Id<Object, Shared> {
}

fn new_leaked_nsdata() -> *mut Object {
ManuallyDrop::new(new_nsdata()).as_ptr()
Id::as_ptr(&*ManuallyDrop::new(new_nsdata()))
}

fn autoreleased_nsdata() -> *mut Object {
Expand All @@ -84,7 +84,7 @@ fn new_nsstring() -> Id<Object, Shared> {
}

fn new_leaked_nsstring() -> *mut Object {
ManuallyDrop::new(new_nsstring()).as_ptr()
Id::as_ptr(&*ManuallyDrop::new(new_nsstring()))
}

fn autoreleased_nsstring() -> *mut Object {
Expand Down
8 changes: 4 additions & 4 deletions objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, O> {
#[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<T, Owned> {
#[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<Id<T, O>> {
#[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<Id<T, Owned>> {
#[inline]
fn as_raw_receiver(self) -> *mut Object {
(**self).as_ptr() as *mut Object
Id::as_ptr(&mut **self) as *mut Object
}
}

Expand Down
13 changes: 5 additions & 8 deletions objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,11 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
/// 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<T, O>) -> *mut T {
this.ptr.as_ptr()
}
}

Expand Down Expand Up @@ -382,7 +379,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
// 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
Expand Down
2 changes: 1 addition & 1 deletion objc2/src/rc/weak_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<T: Message> WeakId<T> {
// allow loading an `Id<T, Shared>` later on.

// SAFETY: `obj` is valid
unsafe { Self::new_inner(obj.as_ptr()) }
unsafe { Self::new_inner(Id::as_ptr(obj)) }
}

/// # Safety
Expand Down
4 changes: 2 additions & 2 deletions tests/src/test_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Object>();
let obj2 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
obj.set_var3(obj2);
assert_eq!(obj.var3(), obj2);
assert_eq!(*obj.var3_ivar(), obj2);

let obj3 = ManuallyDrop::new(NSObject::new()).as_ptr().cast::<Object>();
let obj3 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
*obj.var3_ivar_mut() = obj3;
assert_ne!(obj.var3(), obj2);
assert_ne!(*obj.var3_ivar(), obj2);
Expand Down

0 comments on commit 1158e4c

Please sign in to comment.