Skip to content

Commit f53d6e1

Browse files
committed
Make Id::as_ptr an associated method
1 parent 3fe8b19 commit f53d6e1

File tree

8 files changed

+20
-23
lines changed

8 files changed

+20
-23
lines changed

objc2-foundation/src/attributed_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ mod tests {
153153
let s2 = s1.copy();
154154
// NSAttributedString performs this optimization in GNUStep's runtime,
155155
// but not in Apple's; so we don't test for it!
156-
// assert_eq!(s1.as_ptr(), s2.as_ptr());
156+
// assert_eq!(Id::as_ptr(&s1), Id::as_ptr(&s2));
157157
assert!(s2.is_kind_of(NSAttributedString::class()));
158158

159159
let s3 = s1.mutable_copy();
160-
assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSAttributedString);
160+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *mut NSAttributedString);
161161
assert!(s3.is_kind_of(NSMutableAttributedString::class()));
162162
}
163163
}

objc2-foundation/src/mutable_attributed_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ mod tests {
9090
fn test_copy() {
9191
let s1 = NSMutableAttributedString::from_nsstring(&NSString::from_str("abc"));
9292
let s2 = s1.copy();
93-
assert_ne!(s1.as_ptr() as *const NSAttributedString, s2.as_ptr());
93+
assert_ne!(Id::as_ptr(&s1) as *mut NSAttributedString, Id::as_ptr(&s2));
9494
assert!(s2.is_kind_of(NSAttributedString::class()));
9595

9696
let s3 = s1.mutable_copy();
97-
assert_ne!(s1.as_ptr(), s3.as_ptr());
97+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3));
9898
assert!(s3.is_kind_of(NSMutableAttributedString::class()));
9999
}
100100
}

objc2-foundation/src/mutable_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ mod tests {
200200
fn test_copy() {
201201
let s1 = NSMutableString::from_str("abc");
202202
let s2 = s1.copy();
203-
assert_ne!(s1.as_ptr(), s2.as_ptr() as *mut NSMutableString);
203+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s2) as *mut NSMutableString);
204204
assert!(s2.is_kind_of(NSString::class()));
205205

206206
let s3 = s1.mutable_copy();
207-
assert_ne!(s1.as_ptr(), s3.as_ptr());
207+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3));
208208
assert!(s3.is_kind_of(NSMutableString::class()));
209209
}
210210
}

objc2-foundation/src/string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,11 @@ mod tests {
346346
let s1 = NSString::from_str("abc");
347347
let s2 = s1.copy();
348348
// An optimization that NSString makes, since it is immutable
349-
assert_eq!(s1.as_ptr(), s2.as_ptr());
349+
assert_eq!(Id::as_ptr(&s1), Id::as_ptr(&s2));
350350
assert!(s2.is_kind_of(NSString::class()));
351351

352352
let s3 = s1.mutable_copy();
353-
assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSString);
353+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *mut NSString);
354354
assert!(s3.is_kind_of(NSMutableString::class()));
355355
}
356356

objc2/src/message/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,28 +256,28 @@ unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut T {
256256
unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Id<T, O> {
257257
#[inline]
258258
fn as_raw_receiver(self) -> *mut Object {
259-
self.as_ptr() as *mut Object
259+
Id::as_ptr(self) as *mut Object
260260
}
261261
}
262262

263263
unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut Id<T, Owned> {
264264
#[inline]
265265
fn as_raw_receiver(self) -> *mut Object {
266-
self.as_ptr() as *mut Object
266+
Id::as_ptr(self) as *mut Object
267267
}
268268
}
269269

270270
unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a ManuallyDrop<Id<T, O>> {
271271
#[inline]
272272
fn as_raw_receiver(self) -> *mut Object {
273-
(**self).as_ptr() as *mut Object
273+
Id::as_ptr(&**self) as *mut Object
274274
}
275275
}
276276

277277
unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut ManuallyDrop<Id<T, Owned>> {
278278
#[inline]
279279
fn as_raw_receiver(self) -> *mut Object {
280-
(**self).as_ptr() as *mut Object
280+
Id::as_ptr(&mut **self) as *mut Object
281281
}
282282
}
283283

objc2/src/rc/id.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,11 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
191191
/// Returns a raw pointer to the object.
192192
///
193193
/// The pointer is valid for at least as long as the `Id` is held.
194+
///
195+
/// This is an associated method, and must be called as `Id::as_ptr(obj)`.
194196
#[inline]
195-
pub fn as_ptr(&self) -> *mut T {
196-
// Note: This is not an associated function, which breaks the
197-
// guideline that smart pointers shouldn't add inherent methods!
198-
//
199-
// However, this method is quite useful when migrating old codebases,
200-
// so I think we'll let it be here for now.
201-
self.ptr.as_ptr()
197+
pub fn as_ptr(this: &Id<T, O>) -> *mut T {
198+
this.ptr.as_ptr()
202199
}
203200
}
204201

@@ -382,7 +379,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
382379
// retained objects it is hard to imagine a case where the inner type
383380
// has a method with the same name.
384381

385-
let ptr = ManuallyDrop::new(self).as_ptr() as *mut ffi::objc_object;
382+
let ptr = ManuallyDrop::new(self).ptr.as_ptr() as *mut ffi::objc_object;
386383
// SAFETY: The `ptr` is guaranteed to be valid and have at least one
387384
// retain count.
388385
// And because of the ManuallyDrop, we don't call the Drop

objc2/src/rc/weak_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<T: Message> WeakId<T> {
4343
// allow loading an `Id<T, Shared>` later on.
4444

4545
// SAFETY: `obj` is valid
46-
unsafe { Self::new_inner(obj.as_ptr()) }
46+
unsafe { Self::new_inner(Id::as_ptr(obj)) }
4747
}
4848

4949
/// # Safety

tests/src/test_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ fn test_object() {
179179
assert!(obj.var3().is_null());
180180
assert!(obj.var3_ivar().is_null());
181181

182-
let obj2 = ManuallyDrop::new(NSObject::new()).as_ptr().cast::<Object>();
182+
let obj2 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
183183
obj.set_var3(obj2);
184184
assert_eq!(obj.var3(), obj2);
185185
assert_eq!(*obj.var3_ivar(), obj2);
186186

187-
let obj3 = ManuallyDrop::new(NSObject::new()).as_ptr().cast::<Object>();
187+
let obj3 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
188188
*obj.var3_ivar_mut() = obj3;
189189
assert_ne!(obj.var3(), obj2);
190190
assert_ne!(*obj.var3_ivar(), obj2);

0 commit comments

Comments
 (0)