Skip to content

Use structs to represent objects instead of uninhabited enums #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/foundation_custom_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion objc_exception/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion objc_foundation/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand Down