Skip to content

Commit 8c38606

Browse files
committed
Create references from pointers using as_ref instead of &*
Hard to see difference between unsafe `&*` and safe Deref, and hard to know whether ptr nullability is handled.
1 parent 41dd355 commit 8c38606

File tree

12 files changed

+34
-27
lines changed

12 files changed

+34
-27
lines changed

block2/src/global.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ where
8383
type Target = Block<A, R>;
8484

8585
fn deref(&self) -> &Self::Target {
86+
let ptr: *const Self = self;
87+
let ptr: *const Block<A, R> = ptr.cast();
8688
// TODO: SAFETY
87-
unsafe { &*(self as *const Self as *const Block<A, R>) }
89+
unsafe { ptr.as_ref().unwrap_unchecked() }
8890
}
8991
}
9092

block2/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,10 @@ impl<A, R, F> Deref for ConcreteBlock<A, R, F> {
468468
type Target = Block<A, R>;
469469

470470
fn deref(&self) -> &Self::Target {
471-
unsafe { &*(self as *const Self as *const Block<A, R>) }
471+
let ptr: *const Self = self;
472+
let ptr: *const Block<A, R> = ptr.cast();
473+
// TODO: SAFETY
474+
unsafe { ptr.as_ref().unwrap_unchecked() }
472475
}
473476
}
474477

objc2-foundation/src/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
310310
// Bring back a reference to the closure.
311311
// Guaranteed to be unique, we gave `sortUsingFunction` unique is
312312
// ownership, and that method only runs one function at a time.
313-
let closure: &mut F = unsafe { &mut *(context as *mut F) };
313+
let closure: &mut F = unsafe { (context as *mut F).as_mut().unwrap_unchecked() };
314314

315315
NSComparisonResult::from((*closure)(obj1, obj2))
316316
}

objc2-foundation/src/enumerator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'a, C: NSFastEnumeration + ?Sized> Iterator for NSFastEnumerator<'a, C> {
159159
unsafe {
160160
let obj = *self.ptr;
161161
self.ptr = self.ptr.offset(1);
162-
Some(&*obj)
162+
Some(obj.as_ref().unwrap_unchecked())
163163
}
164164
}
165165
}

objc2-foundation/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ macro_rules! object {
178178
impl<$($t: ::core::cmp::PartialEq $(+ $b)?),*> ::core::cmp::PartialEq for $name<$($t),*> {
179179
#[inline]
180180
fn eq(&self, other: &Self) -> bool {
181-
self.is_equal(&*other)
181+
self.is_equal(other.as_ref())
182182
}
183183
}
184184

objc2-foundation/src/string.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ impl fmt::Display for NSString {
276276
mod tests {
277277
use super::*;
278278
use alloc::format;
279+
use core::ptr;
279280

280281
#[cfg(feature = "gnustep-1-7")]
281282
#[test]
@@ -365,11 +366,10 @@ mod tests {
365366
fn test_copy_nsstring_is_same() {
366367
let string1 = NSString::from_str("Hello, world!");
367368
let string2 = string1.copy();
368-
369-
let s1: *const NSString = &*string1;
370-
let s2: *const NSString = &*string2;
371-
372-
assert_eq!(s1, s2, "Cloned NSString didn't have the same address");
369+
assert!(
370+
ptr::eq(&*string1, &*string2),
371+
"Cloned NSString didn't have the same address"
372+
);
373373
}
374374

375375
#[test]

objc2/src/cache.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ impl CachedClass {
5757
pub unsafe fn get(&self, name: &str) -> Option<&'static Class> {
5858
// `Relaxed` should be fine since `objc_getClass` is thread-safe.
5959
let ptr = self.ptr.load(Ordering::Relaxed);
60-
if ptr.is_null() {
61-
let cls: *const Class = unsafe { ffi::objc_getClass(name.as_ptr().cast()) }.cast();
62-
self.ptr.store(cls as *mut Class, Ordering::Relaxed);
63-
unsafe { cls.as_ref() }
60+
if let Some(cls) = unsafe { ptr.as_ref() } {
61+
Some(cls)
6462
} else {
65-
Some(unsafe { &*ptr })
63+
let ptr: *const Class = unsafe { ffi::objc_getClass(name.as_ptr().cast()) }.cast();
64+
self.ptr.store(ptr as *mut Class, Ordering::Relaxed);
65+
unsafe { ptr.as_ref() }
6666
}
6767
}
6868
}

objc2/src/message/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub unsafe trait MessageReceiver: private::Sealed + Sized {
211211
A: EncodeArguments,
212212
R: Encode,
213213
{
214-
let obj = unsafe { &*self.__as_raw_receiver() };
214+
let obj = unsafe { self.__as_raw_receiver().as_ref().unwrap() };
215215
verify_message_signature::<A, R>(obj.class(), sel).map_err(MessageError::from)
216216
}
217217
}

objc2/src/rc/autorelease.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl AutoreleasePool {
8888
pub unsafe fn ptr_as_ref<'p, T: ?Sized>(&'p self, ptr: *const T) -> &'p T {
8989
self.__verify_is_inner();
9090
// SAFETY: Checked by the caller
91-
unsafe { &*ptr }
91+
unsafe { ptr.as_ref().unwrap_unchecked() }
9292
}
9393

9494
/// Returns a unique reference to the given autoreleased pointer object.
@@ -109,7 +109,7 @@ impl AutoreleasePool {
109109
pub unsafe fn ptr_as_mut<'p, T: ?Sized>(&'p self, ptr: *mut T) -> &'p mut T {
110110
self.__verify_is_inner();
111111
// SAFETY: Checked by the caller
112-
unsafe { &mut *ptr }
112+
unsafe { ptr.as_mut().unwrap_unchecked() }
113113
}
114114
}
115115

objc2/src/rc/id_traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ impl<T: Message + ?Sized, O: Ownership> SliceId for [Id<T, O>] {
2929
let ptr = self as *const Self as *const [&T];
3030
// SAFETY: Id<T, O> and &T have the same memory layout. Further safety
3131
// follows from `Deref` impl.
32-
unsafe { &*ptr }
32+
unsafe { ptr.as_ref().unwrap_unchecked() }
3333
}
3434

3535
fn as_slice_mut(&mut self) -> &mut [&T] {
3636
let ptr = self as *mut Self as *mut [&T];
3737
// SAFETY: Id<T, O> and &T have the same memory layout. Further safety
3838
// follows from `Deref` impl.
39-
unsafe { &mut *ptr }
39+
unsafe { ptr.as_mut().unwrap_unchecked() }
4040
}
4141
}
4242

@@ -46,7 +46,7 @@ impl<T: Message + ?Sized> SliceIdMut for [Id<T, Owned>] {
4646
// SAFETY: Id<T, O> and &mut T have the same memory layout, and the
4747
// `Id` is `Owned` so we're allowed to hand out mutable references.
4848
// Further safety follows from `DerefMut` impl.
49-
unsafe { &mut *ptr }
49+
unsafe { ptr.as_mut().unwrap_unchecked() }
5050
}
5151
}
5252

0 commit comments

Comments
 (0)