Skip to content

Commit 854b252

Browse files
committed
Add Id::as_mut_ptr
1 parent fbd395c commit 854b252

File tree

9 files changed

+36
-13
lines changed

9 files changed

+36
-13
lines changed

objc2-foundation/src/attributed_string.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ mod tests {
157157
assert!(s2.is_kind_of(NSAttributedString::class()));
158158

159159
let s3 = s1.mutable_copy();
160-
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *mut NSAttributedString);
160+
assert_ne!(
161+
Id::as_ptr(&s1),
162+
Id::as_ptr(&s3) as *const NSAttributedString
163+
);
161164
assert!(s3.is_kind_of(NSMutableAttributedString::class()));
162165
}
163166
}

objc2-foundation/src/mutable_attributed_string.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ mod tests {
9090
fn test_copy() {
9191
let s1 = NSMutableAttributedString::from_nsstring(&NSString::from_str("abc"));
9292
let s2 = s1.copy();
93-
assert_ne!(Id::as_ptr(&s1) as *mut NSAttributedString, Id::as_ptr(&s2));
93+
assert_ne!(
94+
Id::as_ptr(&s1) as *const NSAttributedString,
95+
Id::as_ptr(&s2)
96+
);
9497
assert!(s2.is_kind_of(NSAttributedString::class()));
9598

9699
let s3 = s1.mutable_copy();

objc2-foundation/src/mutable_string.rs

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

206206
let s3 = s1.mutable_copy();

objc2-foundation/src/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ mod tests {
350350
assert!(s2.is_kind_of(NSString::class()));
351351

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

objc2/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1111
upgrading easier.
1212
* Allow using `From`/`TryFrom` to convert between `rc::Id` and `rc::WeakId`.
1313
* Added `Bool::as_bool` (more descriptive name than `Bool::is_true`).
14-
* Added convenience method `Id::as_ptr`.
14+
* Added convenience method `Id::as_ptr` and `Id::as_mut_ptr`.
1515
* The `objc2-encode` dependency is now exposed as `objc2::encode`.
1616
* Added `Id::retain_autoreleased` to allow following Cocoas memory management
1717
rules more efficiently.

objc2/src/message/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Id<T,
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-
Id::as_ptr(self) as *mut Object
266+
Id::as_mut_ptr(self) as *mut Object
267267
}
268268
}
269269

@@ -277,7 +277,7 @@ unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Manua
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-
Id::as_ptr(&mut **self) as *mut Object
280+
Id::as_mut_ptr(&mut **self) as *mut Object
281281
}
282282
}
283283

objc2/src/rc/id.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,26 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
192192
///
193193
/// The pointer is valid for at least as long as the `Id` is held.
194194
///
195+
/// See [`Id::as_mut_ptr`] for the mutable equivalent.
196+
///
195197
/// This is an associated method, and must be called as `Id::as_ptr(obj)`.
196198
#[inline]
197-
pub fn as_ptr(this: &Id<T, O>) -> *mut T {
199+
pub fn as_ptr(this: &Id<T, O>) -> *const T {
200+
this.ptr.as_ptr()
201+
}
202+
}
203+
204+
impl<T: Message + ?Sized> Id<T, Owned> {
205+
/// Returns a raw mutable pointer to the object.
206+
///
207+
/// The pointer is valid for at least as long as the `Id` is held.
208+
///
209+
/// See [`Id::as_ptr`] for the immutable equivalent.
210+
///
211+
/// This is an associated method, and must be called as
212+
/// `Id::as_mut_ptr(obj)`.
213+
#[inline]
214+
pub fn as_mut_ptr(this: &mut Id<T, Owned>) -> *mut T {
198215
this.ptr.as_ptr()
199216
}
200217
}

objc2/src/rc/weak_id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ impl<T: Message> WeakId<T> {
4949
/// # Safety
5050
///
5151
/// The object must be valid or null.
52-
unsafe fn new_inner(obj: *mut T) -> Self {
52+
unsafe fn new_inner(obj: *const T) -> Self {
5353
let inner = Box::new(UnsafeCell::new(ptr::null_mut()));
5454
// SAFETY: `ptr` will never move, and the caller verifies `obj`
55-
let _ = unsafe { ffi::objc_initWeak(inner.get(), obj as *mut ffi::objc_object) };
55+
let _ = unsafe { ffi::objc_initWeak(inner.get(), obj as *mut T as *mut ffi::objc_object) };
5656
Self {
5757
inner,
5858
item: PhantomData,
@@ -108,7 +108,7 @@ impl<T: Message> Default for WeakId<T> {
108108
#[inline]
109109
fn default() -> Self {
110110
// SAFETY: The pointer is null
111-
unsafe { Self::new_inner(ptr::null_mut()) }
111+
unsafe { Self::new_inner(ptr::null()) }
112112
}
113113
}
114114

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 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
182+
let obj2 = Id::as_mut_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 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
187+
let obj3 = Id::as_mut_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)