From d22b755310833c0a85dc7a9fed2f370e8162aaf4 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 1 Jun 2021 16:43:53 +0200 Subject: [PATCH 1/2] Use a struct to represent Exception instead of an uninhabited enum Having pointers to uninhabited enums is maybe undefined behaviour, and dereferencing them definitely is. --- objc_exception/src/lib.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/objc_exception/src/lib.rs b/objc_exception/src/lib.rs index 80098a24f..df924e3b9 100644 --- a/objc_exception/src/lib.rs +++ b/objc_exception/src/lib.rs @@ -24,7 +24,18 @@ extern "C" { } /// An opaque type representing any Objective-C object thrown as an exception. -pub enum Exception {} +/// +/// In the future this will be an [`extern type`][rfc-1861], if that gets +/// stabilized. +/// +/// [rfc-1861]: https://rust-lang.github.io/rfcs/1861-extern-types.html +#[repr(C)] +pub struct Exception { + /// See the [Nomicon] for details on representing opaque structs. + /// + /// [Nomicon]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs + _priv: [u8; 0], +} /// Throws an Objective-C exception. /// The argument must be a pointer to an Objective-C object. From cc1430af276a80c03a11bbe916fdee303ebdd037 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 31 Aug 2021 18:07:29 +0200 Subject: [PATCH 2/2] Fix opaque struct usage in objc_foundation --- examples/foundation_custom_class.rs | 10 +++++++++- objc_foundation/src/macros.rs | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/foundation_custom_class.rs b/examples/foundation_custom_class.rs index 22bef447b..30dee30bd 100644 --- a/examples/foundation_custom_class.rs +++ b/examples/foundation_custom_class.rs @@ -6,7 +6,15 @@ use objc::runtime::{Class, Object, Sel}; use objc::Message; use objc_foundation::{INSObject, NSObject}; -pub enum MYObject {} +/// In the future this should be an `extern type`, if that gets stabilized, +/// see [RFC-1861](https://rust-lang.github.io/rfcs/1861-extern-types.html). +#[repr(C)] +pub struct MYObject { + /// See the [Nomicon] for details on representing opaque structs. + /// + /// [Nomicon]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs + _priv: [u8; 0], +} impl MYObject { fn number(&self) -> u32 { diff --git a/objc_foundation/src/macros.rs b/objc_foundation/src/macros.rs index 9b45930af..4c2e39a5d 100644 --- a/objc_foundation/src/macros.rs +++ b/objc_foundation/src/macros.rs @@ -1,8 +1,10 @@ #[macro_export] macro_rules! object_struct { ($name:ident) => { + // TODO: `extern type` + #[repr(C)] pub struct $name { - _private: (), + _private: [u8; 0], } unsafe impl ::objc::Message for $name {}