Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Feb 4, 2023
1 parent 7908848 commit 71e04b1
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 26 deletions.
42 changes: 41 additions & 1 deletion crates/objc2/src/class_type.rs → crates/objc2/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@ use crate::rc::{Allocated, Id, Ownership};
use crate::runtime::Class;
use crate::Message;

mod private {
pub trait Sealed {}
}

pub trait ClassKind: private::Sealed {}

// Fallback for everything that hasn't been specified
pub struct Unknown;
impl private::Sealed for Unknown {}
impl ClassKind for Unknown {}

/// `Id::retain` is safe
pub struct Immutable;
impl private::Sealed for Immutable {}
impl ClassKind for Immutable {}

/// Immutable, but disallows `Id::retain`
pub struct ImmutableWithMutableSubclass;
impl private::Sealed for ImmutableWithMutableSubclass {}
impl ClassKind for ImmutableWithMutableSubclass {}

/// Mutable, mutable methods use `&mut self`. `Id::retain` is not safe
pub struct Mutable;
impl private::Sealed for Mutable {}
impl ClassKind for Mutable {}

/// Mutable, mutable methods use `&self`. Not Send + Sync. `Id::retain` is safe
pub struct InteriorMutable;
impl private::Sealed for InteriorMutable {}
impl ClassKind for InteriorMutable {}

/// Same as InteriorMutable, but only on the main thread. `Id::retain` is safe
pub struct MainThreadOnly;
impl private::Sealed for MainThreadOnly {}
impl ClassKind for MainThreadOnly {}

/// Marks types that represent specific classes.
///
/// Usually it is enough to generically know that a type is messageable, e.g.
Expand Down Expand Up @@ -44,13 +80,15 @@ use crate::Message;
/// trait for a type.
///
/// ```ignore
/// use objc2::{extern_class, ClassType};
/// use objc2::class::{ClassType, Unknown};
/// use objc2::extern_class;
///
/// extern_class!(
/// struct MyClass;
///
/// unsafe impl ClassType for MyClass {
/// type Super = NSObject;
/// type Kind = Unknown;
/// }
/// );
///
Expand All @@ -69,6 +107,8 @@ pub unsafe trait ClassType: Message {
/// [`runtime::Object`]: crate::runtime::Object
type Super: Message;

type Kind: ClassKind;

/// The name of the Objective-C class that this type represents.
const NAME: &'static str;

Expand Down
12 changes: 11 additions & 1 deletion crates/objc2/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,12 @@ impl ProtocolBuilder {
#[cfg(test)]
mod tests {
use super::*;
use crate::class::{ClassType, Immutable};
use crate::protocol::ProtocolType;
use crate::rc::{Id, Shared};
use crate::runtime::{NSObject, NSZone};
use crate::test_utils;
use crate::{declare_class, extern_protocol, msg_send, ClassType, ProtocolType};
use crate::{declare_class, extern_protocol, msg_send};

extern_protocol!(
#[allow(clippy::missing_safety_doc)]
Expand Down Expand Up @@ -848,6 +850,7 @@ mod tests {

unsafe impl ClassType for Custom1 {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestDeclareClassDuplicate";
}
);
Expand All @@ -857,6 +860,7 @@ mod tests {

unsafe impl ClassType for Custom2 {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestDeclareClassDuplicate";
}
);
Expand All @@ -873,6 +877,7 @@ mod tests {

unsafe impl ClassType for Custom {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestDeclareClassProtocolNotFound";
}

Expand All @@ -899,6 +904,7 @@ mod tests {

unsafe impl ClassType for Custom {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestDeclareClassInvalidMethod";
}

Expand All @@ -923,6 +929,7 @@ mod tests {

unsafe impl ClassType for Custom {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestDeclareClassMissingProtocolMethod";
}

Expand All @@ -942,6 +949,7 @@ mod tests {

unsafe impl ClassType for Custom {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestDeclareClassInvalidProtocolMethod";
}

Expand All @@ -968,6 +976,7 @@ mod tests {

unsafe impl ClassType for Custom {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestDeclareClassExtraProtocolMethod";
}

Expand Down Expand Up @@ -998,6 +1007,7 @@ mod tests {

unsafe impl<T> ClassType for GenericDeclareClass<T> {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "GenericDeclareClass";

#[inline]
Expand Down
12 changes: 11 additions & 1 deletion crates/objc2/src/declare/declare_class_tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![deny(deprecated, unreachable_code)]
use core::ptr;

use crate::class::{ClassType, Immutable, Mutable};
use crate::declare::IvarEncode;
use crate::rc::{Id, Owned, Shared};
use crate::runtime::NSObject;
use crate::{declare_class, extern_methods, sel, ClassType};
use crate::{declare_class, extern_methods, sel};

// Test that adding the `deprecated` attribute does not mean that warnings
// when using the method internally are output.
Expand All @@ -14,6 +15,7 @@ declare_class!(

unsafe impl ClassType for DeclareClassDepreactedMethod {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "DeclareClassDepreactedMethod";
}

Expand Down Expand Up @@ -44,6 +46,7 @@ declare_class!(

unsafe impl ClassType for DeclareClassCfg {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "DeclareClassCfg";
}

Expand Down Expand Up @@ -185,6 +188,7 @@ declare_class!(

unsafe impl ClassType for TestMultipleColonSelector {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "TestMultipleColonSelector";
}

Expand Down Expand Up @@ -257,6 +261,7 @@ declare_class!(

unsafe impl ClassType for DeclareClassAllTheBool {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "DeclareClassAllTheBool";
}

Expand Down Expand Up @@ -319,6 +324,7 @@ declare_class!(

unsafe impl ClassType for DeclareClassUnreachable {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "DeclareClassUnreachable";
}

Expand Down Expand Up @@ -374,6 +380,7 @@ fn test_duplicate_ivar() {

unsafe impl ClassType for DeclareClassDuplicateIvar {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "DeclareClassDuplicateIvar";
}
);
Expand All @@ -393,6 +400,7 @@ fn test_subclass_duplicate_ivar() {

unsafe impl ClassType for Cls {
type Super = NSObject;
type Kind = Mutable;
const NAME: &'static str = "DeclareClassDuplicateIvarSuperclass";
}
);
Expand All @@ -406,6 +414,7 @@ fn test_subclass_duplicate_ivar() {

unsafe impl ClassType for SubCls {
type Super = Cls;
type Kind = Mutable;
const NAME: &'static str = "DeclareClassDuplicateIvarSubclass";
}
);
Expand Down Expand Up @@ -449,6 +458,7 @@ declare_class!(

unsafe impl ClassType for OutParam {
type Super = NSObject;
type Kind = Immutable;
const NAME: &'static str = "OutParam";
}

Expand Down
4 changes: 3 additions & 1 deletion crates/objc2/src/declare/ivar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,11 @@ mod tests {
use std::sync::atomic::{AtomicBool, Ordering};

use super::*;
use crate::class::{ClassType, Mutable};
use crate::declare::{IvarBool, IvarEncode};
use crate::rc::{Id, Owned};
use crate::runtime::NSObject;
use crate::{declare_class, msg_send, msg_send_id, test_utils, ClassType, MessageReceiver};
use crate::{declare_class, msg_send, msg_send_id, test_utils, MessageReceiver};

struct TestIvar;

Expand Down Expand Up @@ -374,6 +375,7 @@ mod tests {

unsafe impl ClassType for CustomDrop {
type Super = NSObject;
type Kind = Mutable;
const NAME: &'static str = "CustomDrop";
}
);
Expand Down
5 changes: 4 additions & 1 deletion crates/objc2/src/declare/ivar_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ unsafe fn box_unreachable() -> ! {
#[cfg(test)]
mod tests {
use super::*;
use crate::class::{ClassType, Mutable};
use crate::declare::{Ivar, IvarType};
use crate::rc::{Allocated, Owned, Shared, __RcTestObject, __ThreadTestData};
use crate::runtime::NSObject;
use crate::runtime::Object;
use crate::{declare_class, msg_send, msg_send_id, ClassType};
use crate::{declare_class, msg_send, msg_send_id};

struct TestIvar1;
unsafe impl IvarType for TestIvar1 {
Expand Down Expand Up @@ -246,6 +247,7 @@ mod tests {

unsafe impl ClassType for IvarTester {
type Super = NSObject;
type Kind = Mutable;
const NAME: &'static str = "IvarTester";
}

Expand Down Expand Up @@ -282,6 +284,7 @@ mod tests {

unsafe impl ClassType for IvarTesterSubclass {
type Super = IvarTester;
type Kind = Mutable;
const NAME: &'static str = "IvarTesterSubclass";
}

Expand Down
8 changes: 5 additions & 3 deletions crates/objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ extern "C" {}
#[doc(no_inline)]
pub use objc_sys as ffi;

pub use self::class_type::ClassType;
#[doc(no_inline)]
pub use self::class::ClassType;
#[doc(no_inline)]
pub use self::encode::{Encode, Encoding, RefEncode};
pub use self::message::{Message, MessageArguments, MessageReceiver};
#[doc(no_inline)]
pub use self::protocol::{ImplementedBy, ProtocolObject, ProtocolType};
pub use self::verify::VerificationError;

Expand All @@ -211,13 +213,13 @@ macro_rules! __hash_idents {
#[doc(hidden)]
pub mod __macro_helpers;
mod cache;
mod class_type;
pub mod class;
pub mod declare;
pub mod encode;
pub mod exception;
mod macros;
mod message;
mod protocol;
pub mod protocol;
pub mod rc;
pub mod runtime;
#[cfg(test)]
Expand Down
5 changes: 4 additions & 1 deletion crates/objc2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,14 +887,17 @@ macro_rules! __class_inner {
///
/// ```no_run
/// use objc2::msg_send;
/// #
/// # use objc2::class::{ClassType, Unknown};
/// # use objc2::runtime::NSObject;
/// # use objc2::{declare_class, ClassType};
/// # use objc2::declare_class;
/// #
/// # declare_class!(
/// # struct MyObject;
/// #
/// # unsafe impl ClassType for MyObject {
/// # type Super = NSObject;
/// # type Kind = Unknown;
/// # const NAME: &'static str = "MyObject";
/// # }
/// # );
Expand Down
Loading

0 comments on commit 71e04b1

Please sign in to comment.