From b8f9b883d6299c5fddd751c9ea79803e6d781ede Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 12 Jun 2022 10:06:00 +0200 Subject: [PATCH] Don't use `as` for casting pointers (only for `*const T -> *mut T`) Easy to accidentally convert immutable pointers to a mutable ones, as is done in `declare` (now fixed). --- objc2-foundation/src/data.rs | 17 +++--- objc2-foundation/src/enumerator.rs | 8 +-- objc2-foundation/src/string.rs | 4 +- objc2-foundation/src/value.rs | 5 +- objc2/examples/talk_to_me.rs | 3 +- objc2/src/cache.rs | 10 ++-- objc2/src/declare.rs | 45 ++++++++-------- objc2/src/exception.rs | 10 ++-- objc2/src/message/apple/mod.rs | 11 ++-- objc2/src/message/gnustep.rs | 11 ++-- objc2/src/message/mod.rs | 19 ++++--- objc2/src/runtime.rs | 76 ++++++++++++++++----------- objc2/src/test_utils.rs | 8 +-- objc2/tests/id_retain_autoreleased.rs | 2 +- 14 files changed, 129 insertions(+), 100 deletions(-) diff --git a/objc2-foundation/src/data.rs b/objc2-foundation/src/data.rs index 17f7e9f5b..0ecb7fd86 100644 --- a/objc2-foundation/src/data.rs +++ b/objc2-foundation/src/data.rs @@ -53,11 +53,12 @@ impl NSData { pub fn bytes(&self) -> &[u8] { let ptr: *const c_void = unsafe { msg_send![self, bytes] }; + let ptr: *const u8 = ptr.cast(); // The bytes pointer may be null for length zero if ptr.is_null() { &[] } else { - unsafe { slice::from_raw_parts(ptr as *const u8, self.len()) } + unsafe { slice::from_raw_parts(ptr, self.len()) } } } @@ -172,11 +173,12 @@ impl NSMutableData { #[doc(alias = "mutableBytes")] pub fn bytes_mut(&mut self) -> &mut [u8] { let ptr = self.raw_bytes_mut(); + let ptr: *mut u8 = ptr.cast(); // The bytes pointer may be null for length zero if ptr.is_null() { &mut [] } else { - unsafe { slice::from_raw_parts_mut(ptr as *mut u8, self.len()) } + unsafe { slice::from_raw_parts_mut(ptr, self.len()) } } } @@ -188,7 +190,7 @@ impl NSMutableData { #[doc(alias = "appendBytes:length:")] pub fn extend_from_slice(&mut self, bytes: &[u8]) { - let bytes_ptr = bytes.as_ptr() as *const c_void; + let bytes_ptr: *const c_void = bytes.as_ptr().cast(); unsafe { msg_send![self, appendBytes: bytes_ptr, length: bytes.len()] } } @@ -201,7 +203,7 @@ impl NSMutableData { let range = NSRange::from(range); // No need to verify the length of the range here, // `replaceBytesInRange:` just zero-fills if out of bounds. - let ptr = bytes.as_ptr() as *const c_void; + let ptr: *const c_void = bytes.as_ptr().cast(); unsafe { msg_send![ self, @@ -312,7 +314,7 @@ impl DefaultId for NSMutableData { } unsafe fn data_with_bytes(cls: &Class, bytes: &[u8]) -> *mut Object { - let bytes_ptr = bytes.as_ptr() as *const c_void; + let bytes_ptr: *const c_void = bytes.as_ptr().cast(); unsafe { let obj: *mut Object = msg_send![cls, alloc]; msg_send![ @@ -333,18 +335,19 @@ unsafe fn data_from_vec(cls: &Class, bytes: Vec) -> *mut Object { let dealloc = ConcreteBlock::new(move |bytes: *mut c_void, len: usize| unsafe { // Recreate the Vec and let it drop - let _ = Vec::from_raw_parts(bytes as *mut u8, len, capacity); + let _ = Vec::::from_raw_parts(bytes.cast(), len, capacity); }); let dealloc = dealloc.copy(); let dealloc: &Block<(*mut c_void, usize), ()> = &dealloc; let mut bytes = ManuallyDrop::new(bytes); + let bytes_ptr: *mut c_void = bytes.as_mut_ptr().cast(); unsafe { let obj: *mut Object = msg_send![cls, alloc]; msg_send![ obj, - initWithBytesNoCopy: bytes.as_mut_ptr() as *mut c_void, + initWithBytesNoCopy: bytes_ptr, length: bytes.len(), deallocator: dealloc, ] diff --git a/objc2-foundation/src/enumerator.rs b/objc2-foundation/src/enumerator.rs index 35f2b3776..23177d849 100644 --- a/objc2-foundation/src/enumerator.rs +++ b/objc2-foundation/src/enumerator.rs @@ -172,25 +172,25 @@ mod tests { #[test] fn test_enumerator() { - let vec = (0u32..4).map(NSValue::new).collect(); + let vec = (0usize..4).map(NSValue::new).collect(); let array = NSArray::from_vec(vec); let enumerator = array.iter(); assert_eq!(enumerator.count(), 4); let enumerator = array.iter(); - assert!(enumerator.enumerate().all(|(i, obj)| obj.get() == i as u32)); + assert!(enumerator.enumerate().all(|(i, obj)| obj.get() == i)); } #[test] fn test_fast_enumerator() { - let vec = (0u32..4).map(NSValue::new).collect(); + let vec = (0usize..4).map(NSValue::new).collect(); let array = NSArray::from_vec(vec); let enumerator = array.iter_fast(); assert_eq!(enumerator.count(), 4); let enumerator = array.iter_fast(); - assert!(enumerator.enumerate().all(|(i, obj)| obj.get() == i as u32)); + assert!(enumerator.enumerate().all(|(i, obj)| obj.get() == i)); } } diff --git a/objc2-foundation/src/string.rs b/objc2-foundation/src/string.rs index 53f063826..fbe153483 100644 --- a/objc2-foundation/src/string.rs +++ b/objc2-foundation/src/string.rs @@ -140,7 +140,7 @@ impl NSString { // // https://developer.apple.com/documentation/foundation/nsstring/1411189-utf8string?language=objc let bytes: *const c_char = unsafe { msg_send![self, UTF8String] }; - let bytes = bytes as *const u8; + let bytes: *const u8 = bytes.cast(); let len = self.len(); // SAFETY: @@ -199,7 +199,7 @@ impl NSString { } pub(crate) fn from_str(cls: &Class, string: &str) -> *mut Object { - let bytes = string.as_ptr() as *const c_void; + let bytes: *const c_void = string.as_ptr().cast(); unsafe { let obj: *mut Object = msg_send![cls, alloc]; msg_send![ diff --git a/objc2-foundation/src/value.rs b/objc2-foundation/src/value.rs index 3d4e173d5..71b44f9f5 100644 --- a/objc2-foundation/src/value.rs +++ b/objc2-foundation/src/value.rs @@ -52,7 +52,7 @@ impl NSValue { /// The user must ensure that the inner value is properly initialized. pub unsafe fn get_unchecked(&self) -> T { let mut value = MaybeUninit::::uninit(); - let ptr = value.as_mut_ptr() as *mut c_void; + let ptr: *mut c_void = value.as_mut_ptr().cast(); let _: () = unsafe { msg_send![self, getValue: ptr] }; unsafe { value.assume_init() } } @@ -64,7 +64,8 @@ impl NSValue { pub fn new(value: T) -> Id { let cls = Self::class(); - let bytes = &value as *const T as *const c_void; + let bytes: *const T = &value; + let bytes: *const c_void = bytes.cast(); let encoding = CString::new(T::ENCODING.to_string()).unwrap(); unsafe { let obj: *mut Self = msg_send![cls, alloc]; diff --git a/objc2/examples/talk_to_me.rs b/objc2/examples/talk_to_me.rs index 1ab776884..a29eb193b 100644 --- a/objc2/examples/talk_to_me.rs +++ b/objc2/examples/talk_to_me.rs @@ -14,10 +14,11 @@ fn main() { const UTF8_ENCODING: NSUInteger = 4; let string: *const Object = unsafe { msg_send![class!(NSString), alloc] }; + let text_ptr: *const c_void = text.as_ptr().cast(); let string = unsafe { msg_send![ string, - initWithBytes: text.as_ptr() as *const c_void, + initWithBytes: text_ptr, length: text.len(), encoding: UTF8_ENCODING, ] diff --git a/objc2/src/cache.rs b/objc2/src/cache.rs index 0fd2cb950..1858f493d 100644 --- a/objc2/src/cache.rs +++ b/objc2/src/cache.rs @@ -27,9 +27,9 @@ impl CachedSel { // `Relaxed` should be fine since `sel_registerName` is thread-safe. let ptr = self.ptr.load(Ordering::Relaxed); if ptr.is_null() { - let ptr = unsafe { ffi::sel_registerName(name.as_ptr() as *const _) }; - self.ptr.store(ptr as *mut _, Ordering::Relaxed); - unsafe { Sel::from_ptr(ptr as *const _) } + let ptr: *const c_void = unsafe { ffi::sel_registerName(name.as_ptr().cast()).cast() }; + self.ptr.store(ptr as *mut c_void, Ordering::Relaxed); + unsafe { Sel::from_ptr(ptr) } } else { unsafe { Sel::from_ptr(ptr) } } @@ -58,8 +58,8 @@ impl CachedClass { // `Relaxed` should be fine since `objc_getClass` is thread-safe. let ptr = self.ptr.load(Ordering::Relaxed); if ptr.is_null() { - let cls = unsafe { ffi::objc_getClass(name.as_ptr() as *const _) } as *const Class; - self.ptr.store(cls as *mut _, Ordering::Relaxed); + let cls: *const Class = unsafe { ffi::objc_getClass(name.as_ptr().cast()) }.cast(); + self.ptr.store(cls as *mut Class, Ordering::Relaxed); unsafe { cls.as_ref() } } else { Some(unsafe { &*ptr }) diff --git a/objc2/src/declare.rs b/objc2/src/declare.rs index 17bb58db3..6239c2541 100644 --- a/objc2/src/declare.rs +++ b/objc2/src/declare.rs @@ -186,13 +186,13 @@ unsafe impl Send for ClassBuilder {} unsafe impl Sync for ClassBuilder {} impl ClassBuilder { - fn as_ptr(&self) -> *mut ffi::objc_class { + fn as_mut_ptr(&mut self) -> *mut ffi::objc_class { self.cls.as_ptr().cast() } fn with_superclass(name: &str, superclass: Option<&Class>) -> Option { let name = CString::new(name).unwrap(); - let super_ptr = superclass.map_or(ptr::null(), |c| c) as _; + let super_ptr = superclass.map_or(ptr::null(), |c| c).cast(); let cls = unsafe { ffi::objc_allocateClassPair(super_ptr, name.as_ptr(), 0) }; NonNull::new(cls.cast()).map(|cls| Self { cls }) } @@ -255,8 +255,8 @@ impl ClassBuilder { let types = method_type_encoding(&F::Ret::ENCODING, encs); let success = Bool::from_raw(unsafe { ffi::class_addMethod( - self.as_ptr(), - sel.as_ptr() as _, + self.as_mut_ptr(), + sel.as_ptr().cast(), Some(func.__imp()), types.as_ptr(), ) @@ -264,6 +264,10 @@ impl ClassBuilder { assert!(success.as_bool(), "Failed to add method {:?}", sel); } + fn metaclass_mut(&mut self) -> *mut ffi::objc_class { + unsafe { ffi::object_getClass(self.as_mut_ptr().cast()) as *mut ffi::objc_class } + } + /// Adds a class method with the given name and implementation. /// /// # Panics @@ -290,11 +294,10 @@ impl ClassBuilder { ); let types = method_type_encoding(&F::Ret::ENCODING, encs); - let metaclass = unsafe { self.cls.as_ref() }.metaclass() as *const _ as *mut _; let success = Bool::from_raw(unsafe { ffi::class_addMethod( - metaclass, - sel.as_ptr() as _, + self.metaclass_mut(), + sel.as_ptr().cast(), Some(func.__imp()), types.as_ptr(), ) @@ -314,7 +317,7 @@ impl ClassBuilder { let align = log2_align_of::(); let success = Bool::from_raw(unsafe { ffi::class_addIvar( - self.as_ptr(), + self.as_mut_ptr(), c_name.as_ptr(), size, align, @@ -330,7 +333,7 @@ impl ClassBuilder { /// /// If the protocol wasn't successfully added. pub fn add_protocol(&mut self, proto: &Protocol) { - let success = unsafe { ffi::class_addProtocol(self.as_ptr(), proto.as_ptr()) }; + let success = unsafe { ffi::class_addProtocol(self.as_mut_ptr(), proto.as_ptr()) }; let success = Bool::from_raw(success).as_bool(); assert!(success, "Failed to add protocol {:?}", proto); } @@ -341,15 +344,15 @@ impl ClassBuilder { /// to the newly registered [`Class`]. pub fn register(self) -> &'static Class { // Forget self, otherwise the class will be disposed in drop - let cls = ManuallyDrop::new(self).cls; - unsafe { ffi::objc_registerClassPair(cls.as_ptr().cast()) }; - unsafe { cls.as_ref() } + let mut this = ManuallyDrop::new(self); + unsafe { ffi::objc_registerClassPair(this.as_mut_ptr()) }; + unsafe { this.cls.as_ref() } } } impl Drop for ClassBuilder { fn drop(&mut self) { - unsafe { ffi::objc_disposeClassPair(self.as_ptr()) } + unsafe { ffi::objc_disposeClassPair(self.as_mut_ptr()) } } } @@ -369,7 +372,7 @@ unsafe impl Send for ProtocolBuilder {} unsafe impl Sync for ProtocolBuilder {} impl ProtocolBuilder { - fn as_ptr(&self) -> *mut ffi::objc_protocol { + fn as_mut_ptr(&mut self) -> *mut ffi::objc_protocol { self.proto.as_ptr().cast() } @@ -378,8 +381,8 @@ impl ProtocolBuilder { /// Returns [`None`] if the protocol couldn't be allocated. pub fn new(name: &str) -> Option { let c_name = CString::new(name).unwrap(); - let proto = unsafe { ffi::objc_allocateProtocol(c_name.as_ptr()) } as *mut Protocol; - NonNull::new(proto).map(|proto| Self { proto }) + let proto = unsafe { ffi::objc_allocateProtocol(c_name.as_ptr()) }; + NonNull::new(proto.cast()).map(|proto| Self { proto }) } fn add_method_description_common( @@ -403,8 +406,8 @@ impl ProtocolBuilder { let types = method_type_encoding(&Ret::ENCODING, encs); unsafe { ffi::protocol_addMethodDescription( - self.as_ptr(), - sel.as_ptr() as _, + self.as_mut_ptr(), + sel.as_ptr().cast(), types.as_ptr(), Bool::new(is_required).as_raw(), Bool::new(is_instance_method).as_raw(), @@ -433,15 +436,15 @@ impl ProtocolBuilder { /// Adds a requirement on another protocol. pub fn add_protocol(&mut self, proto: &Protocol) { unsafe { - ffi::protocol_addProtocol(self.as_ptr(), proto.as_ptr()); + ffi::protocol_addProtocol(self.as_mut_ptr(), proto.as_ptr()); } } /// Registers the [`ProtocolBuilder`], consuming it and returning a reference /// to the newly registered [`Protocol`]. - pub fn register(self) -> &'static Protocol { + pub fn register(mut self) -> &'static Protocol { unsafe { - ffi::objc_registerProtocol(self.as_ptr()); + ffi::objc_registerProtocol(self.as_mut_ptr()); self.proto.as_ref() } } diff --git a/objc2/src/exception.rs b/objc2/src/exception.rs index ef4414c7a..bc65fe516 100644 --- a/objc2/src/exception.rs +++ b/objc2/src/exception.rs @@ -60,7 +60,8 @@ unsafe fn try_no_ret(closure: F) -> Result<(), Option = &mut closure; + let context = context.cast(); let mut exception = ptr::null_mut(); let success = unsafe { ffi::rust_objc_sys_0_2_try_catch_exception(f, context, &mut exception) }; @@ -76,7 +77,7 @@ unsafe fn try_no_ret(closure: F) -> Result<(), Option = unsafe { Id::new(msg_send![class!(NSObject), new]).unwrap() }; let result = unsafe { catch(|| throw(Some(&obj))) }; - let e = result.unwrap_err().unwrap(); - // Compare pointers - assert_eq!(&*e as *const Object, &*obj as *const Object); + let exception = result.unwrap_err().unwrap(); + assert!(ptr::eq(&*exception, &*obj)); } } diff --git a/objc2/src/message/apple/mod.rs b/objc2/src/message/apple/mod.rs index a5dfd5650..37345bb56 100644 --- a/objc2/src/message/apple/mod.rs +++ b/objc2/src/message/apple/mod.rs @@ -48,11 +48,14 @@ where A: MessageArguments, R: Encode, { - let sup = ffi::objc_super { - receiver: receiver as *mut _, - super_class: superclass as *const Class as *const _, + let superclass: *const Class = superclass; + let mut sup = ffi::objc_super { + receiver: receiver.cast(), + super_class: superclass.cast(), }; - let receiver = &sup as *const ffi::objc_super as *mut Object; + let receiver: *mut ffi::objc_super = &mut sup; + let receiver = receiver.cast(); + let msg_send_fn = R::MSG_SEND_SUPER; unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) } } diff --git a/objc2/src/message/gnustep.rs b/objc2/src/message/gnustep.rs index 650e121e7..39ee5932e 100644 --- a/objc2/src/message/gnustep.rs +++ b/objc2/src/message/gnustep.rs @@ -23,8 +23,8 @@ where return unsafe { mem::zeroed() }; } - let sel_ptr = sel.as_ptr() as *const _; - let msg_send_fn = unsafe { ffi::objc_msg_lookup(receiver as *mut _, sel_ptr) }; + let sel_ptr = sel.as_ptr().cast(); + let msg_send_fn = unsafe { ffi::objc_msg_lookup(receiver.cast(), sel_ptr) }; let msg_send_fn = msg_send_fn.expect("Null IMP"); unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) } } @@ -44,11 +44,12 @@ where return unsafe { mem::zeroed() }; } + let superclass: *const Class = superclass; let sup = ffi::objc_super { - receiver: receiver as *mut _, - super_class: superclass as *const Class as *const _, + receiver: receiver.cast(), + super_class: superclass.cast(), }; - let sel_ptr = sel.as_ptr() as *const _; + let sel_ptr = sel.as_ptr().cast(); let msg_send_fn = unsafe { ffi::objc_msg_lookup_super(&sup, sel_ptr) }; let msg_send_fn = msg_send_fn.expect("Null IMP"); unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) } diff --git a/objc2/src/message/mod.rs b/objc2/src/message/mod.rs index fff0fc9cf..8f5fc60c5 100644 --- a/objc2/src/message/mod.rs +++ b/objc2/src/message/mod.rs @@ -229,49 +229,51 @@ unsafe impl MessageReceiver for *const T { unsafe impl MessageReceiver for *mut T { #[inline] fn __as_raw_receiver(self) -> *mut Object { - self as *mut Object + self.cast() } } unsafe impl MessageReceiver for NonNull { #[inline] fn __as_raw_receiver(self) -> *mut Object { - self.as_ptr() as *mut Object + self.as_ptr().cast() } } unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a T { #[inline] fn __as_raw_receiver(self) -> *mut Object { - self as *const T as *mut T as *mut Object + let ptr: *const T = self; + ptr as *mut T as *mut Object } } unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut T { #[inline] fn __as_raw_receiver(self) -> *mut Object { - self as *mut T as *mut Object + let ptr: *mut T = self; + ptr.cast() } } unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Id { #[inline] fn __as_raw_receiver(self) -> *mut Object { - Id::as_ptr(self) as *mut Object + Id::as_ptr(self) as *mut T as *mut Object } } unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut Id { #[inline] fn __as_raw_receiver(self) -> *mut Object { - Id::as_mut_ptr(self) as *mut Object + Id::as_mut_ptr(self).cast() } } unsafe impl MessageReceiver for ManuallyDrop> { #[inline] fn __as_raw_receiver(self) -> *mut Object { - Id::consume_as_ptr(self) as *mut Object + Id::consume_as_ptr(self).cast() } } @@ -285,7 +287,8 @@ unsafe impl MessageReceiver for *const Class { unsafe impl<'a> MessageReceiver for &'a Class { #[inline] fn __as_raw_receiver(self) -> *mut Object { - self as *const Class as *mut Class as *mut Object + let ptr: *const Class = self; + ptr as *mut Class as *mut Object } } diff --git a/objc2/src/runtime.rs b/objc2/src/runtime.rs index 7bf2d5ade..9afec40d0 100644 --- a/objc2/src/runtime.rs +++ b/objc2/src/runtime.rs @@ -12,6 +12,7 @@ use core::str; #[cfg(feature = "malloc")] use malloc_buf::Malloc; use std::ffi::{CStr, CString}; +use std::os::raw::c_char; #[cfg(feature = "malloc")] use std::os::raw::c_uint; @@ -118,15 +119,13 @@ impl Sel { /// This is almost never what you want; use [`Sel::register`] instead. #[inline] pub unsafe fn from_ptr(ptr: *const c_void) -> Self { - Self { - ptr: ptr as *const _, - } + Self { ptr: ptr.cast() } } /// Returns a pointer to the raw selector. #[inline] pub fn as_ptr(&self) -> *const c_void { - self.ptr as *const _ + self.ptr.cast() } } @@ -159,7 +158,8 @@ impl fmt::Pointer for Sel { impl Ivar { pub(crate) fn as_ptr(&self) -> *const ffi::objc_ivar { - self as *const Self as *const _ + let ptr: *const Self = self; + ptr.cast() } /// Returns the name of self. @@ -188,7 +188,8 @@ impl RefUnwindSafe for Ivar {} impl Method { pub(crate) fn as_ptr(&self) -> *const ffi::objc_method { - self as *const Self as *const _ + let ptr: *const Self = self; + ptr.cast() } /// Returns the name of self. @@ -243,7 +244,8 @@ impl RefUnwindSafe for Method {} impl Class { pub(crate) fn as_ptr(&self) -> *const ffi::objc_class { - self as *const Self as *const _ + let ptr: *const Self = self; + ptr.cast() } /// Returns the class definition of a specified class, or [`None`] if the @@ -262,8 +264,8 @@ impl Class { pub fn classes() -> Malloc<[&'static Self]> { unsafe { let mut count: c_uint = 0; - let classes = ffi::objc_copyClassList(&mut count); - Malloc::from_array(classes as *mut _, count as usize) + let classes: *mut &Self = ffi::objc_copyClassList(&mut count).cast(); + Malloc::from_array(classes, count as usize) } } @@ -288,7 +290,7 @@ impl Class { /// Returns the metaclass of self. pub fn metaclass(&self) -> &Self { - unsafe { &*(ffi::object_getClass(self.as_ptr() as *const _) as *const Self) } + unsafe { &*(ffi::object_getClass(self.as_ptr().cast()).cast()) } } // objc_getMetaClass -> Same as `Class::get(name).metaclass()` @@ -327,11 +329,11 @@ impl Class { #[allow(unused)] fn instance_variable_layout(&self) -> Option<&[u8]> { - let layout = unsafe { ffi::class_getIvarLayout(self.as_ptr()) }; + let layout: *const c_char = unsafe { ffi::class_getIvarLayout(self.as_ptr()).cast() }; if layout.is_null() { None } else { - Some(unsafe { CStr::from_ptr(layout as *const _) }.to_bytes()) + Some(unsafe { CStr::from_ptr(layout) }.to_bytes()) } } @@ -348,8 +350,8 @@ impl Class { pub fn instance_methods(&self) -> Malloc<[&Method]> { unsafe { let mut count: c_uint = 0; - let methods = ffi::class_copyMethodList(self.as_ptr(), &mut count); - Malloc::from_array(methods as *mut _, count as usize) + let methods: *mut &Method = ffi::class_copyMethodList(self.as_ptr(), &mut count).cast(); + Malloc::from_array(methods, count as usize) } } @@ -365,8 +367,9 @@ impl Class { pub fn adopted_protocols(&self) -> Malloc<[&Protocol]> { unsafe { let mut count: c_uint = 0; - let protos = ffi::class_copyProtocolList(self.as_ptr(), &mut count); - Malloc::from_array(protos as *mut _, count as usize) + let protos: *mut &Protocol = + ffi::class_copyProtocolList(self.as_ptr(), &mut count).cast(); + Malloc::from_array(protos, count as usize) } } @@ -375,8 +378,8 @@ impl Class { pub fn instance_variables(&self) -> Malloc<[&Ivar]> { unsafe { let mut count: c_uint = 0; - let ivars = ffi::class_copyIvarList(self.as_ptr(), &mut count); - Malloc::from_array(ivars as *mut _, count as usize) + let ivars: *mut &Ivar = ffi::class_copyIvarList(self.as_ptr(), &mut count).cast(); + Malloc::from_array(ivars, count as usize) } } @@ -412,7 +415,8 @@ impl fmt::Debug for Class { impl Protocol { pub(crate) fn as_ptr(&self) -> *const ffi::objc_protocol { - self as *const Self as *const _ + let ptr: *const Self = self; + ptr.cast() } /// Returns the protocol definition of a specified protocol, or [`None`] @@ -430,8 +434,8 @@ impl Protocol { pub fn protocols() -> Malloc<[&'static Protocol]> { unsafe { let mut count: c_uint = 0; - let protocols = ffi::objc_copyProtocolList(&mut count); - Malloc::from_array(protocols as *mut _, count as usize) + let protocols: *mut &Protocol = ffi::objc_copyProtocolList(&mut count).cast(); + Malloc::from_array(protocols, count as usize) } } @@ -440,8 +444,9 @@ impl Protocol { pub fn adopted_protocols(&self) -> Malloc<[&Protocol]> { unsafe { let mut count: c_uint = 0; - let protocols = ffi::protocol_copyProtocolList(self.as_ptr(), &mut count); - Malloc::from_array(protocols as *mut _, count as usize) + let protocols: *mut &Protocol = + ffi::protocol_copyProtocolList(self.as_ptr(), &mut count).cast(); + Malloc::from_array(protocols, count as usize) } } @@ -503,12 +508,13 @@ fn ivar_offset(cls: &Class, name: &str) -> isize { impl Object { pub(crate) fn as_ptr(&self) -> *const ffi::objc_object { - self as *const Self as *const _ + let ptr: *const Self = self; + ptr.cast() } /// Dynamically find the class of this object. pub fn class(&self) -> &Class { - unsafe { &*(ffi::object_getClass(self.as_ptr()) as *const Class) } + unsafe { &*(ffi::object_getClass(self.as_ptr()).cast()) } } /// Returns a shared reference to the ivar with the given name. @@ -525,9 +531,13 @@ impl Object { /// Library implementors should expose a safe interface to the ivar. pub unsafe fn ivar(&self, name: &str) -> &T { let offset = ivar_offset::(self.class(), name); - // `offset` is given in bytes, so we convert to `u8` - let ptr = self as *const Self as *const u8; - let ptr = unsafe { ptr.offset(offset) } as *const T; + let ptr: *const Self = self; + + // `offset` is given in bytes, so we convert to `u8` and back to `T` + let ptr: *const u8 = ptr.cast(); + let ptr = unsafe { ptr.offset(offset) }; + let ptr: *const T = ptr.cast(); + unsafe { &*ptr } } @@ -556,9 +566,13 @@ impl Object { /// Library implementors should expose a safe interface to the ivar. pub unsafe fn ivar_mut(&mut self, name: &str) -> &mut T { let offset = ivar_offset::(self.class(), name); - // `offset` is given in bytes, so we convert to `u8` - let ptr = self as *mut Self as *mut u8; - let ptr = unsafe { ptr.offset(offset) } as *mut T; + let ptr: *mut Self = self; + + // `offset` is given in bytes, so we convert to `u8` and back to `T` + let ptr: *mut u8 = ptr.cast(); + let ptr = unsafe { ptr.offset(offset) }; + let ptr: *mut T = ptr.cast(); + unsafe { &mut *ptr } } diff --git a/objc2/src/test_utils.rs b/objc2/src/test_utils.rs index e9f8ae354..ee38b3bcc 100644 --- a/objc2/src/test_utils.rs +++ b/objc2/src/test_utils.rs @@ -14,9 +14,9 @@ pub(crate) struct CustomObject { impl CustomObject { fn new(class: &Class) -> Self { - let ptr = class as *const Class as _; - let obj = unsafe { ffi::class_createInstance(ptr, 0) }; - CustomObject { obj: obj as _ } + let ptr: *const Class = class; + let obj = unsafe { ffi::class_createInstance(ptr.cast(), 0) }.cast(); + CustomObject { obj } } } @@ -64,7 +64,7 @@ impl Drop for CustomObject { fn drop(&mut self) { unsafe { #[allow(deprecated)] - ffi::object_dispose(self.obj as _); + ffi::object_dispose(self.obj.cast()); } } } diff --git a/objc2/tests/id_retain_autoreleased.rs b/objc2/tests/id_retain_autoreleased.rs index 6188ffada..2640a4a73 100644 --- a/objc2/tests/id_retain_autoreleased.rs +++ b/objc2/tests/id_retain_autoreleased.rs @@ -9,7 +9,7 @@ fn retain_count(obj: &Object) -> usize { } fn create_data(bytes: &[u8]) -> Id { - let bytes_ptr = bytes.as_ptr() as *const c_void; + let bytes_ptr: *const c_void = bytes.as_ptr().cast(); unsafe { // let obj: *mut Object = msg_send![ // class!(NSMutableData),