Skip to content

Commit

Permalink
Rename "declare class" to "define class"
Browse files Browse the repository at this point in the history
- declare_class! -> define_class!
- DeclaredClass -> DefinedClass
- A bunch of internal helpers

Fixes #532
  • Loading branch information
madsmtm committed Nov 26, 2024
1 parent 180b930 commit 89efc9e
Show file tree
Hide file tree
Showing 77 changed files with 1,015 additions and 966 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions crates/objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Implement `Message` for `AnyClass` and `AnyProtocol`.
* Allow `AnyClass` and `AnyProtocol` to be converted to `AnyObject` (both of
these can act as objects).
* Classes created using `declare_class!` now implement `Send` and `Sync` when
* Classes created using `define_class!` now implement `Send` and `Sync` when
subclassing `NSObject`.

### Changed
* **BREAKING**: Changed the syntax of `declare_class!` to be more succinct:
* **BREAKING**: Renamed `declare_class!` to `define_class!`, and changed the
syntax to be more succinct:

```rust
// Before
use objc2::mutability::InteriorMutable;
use objc2::runtime::NSObject;
use objc2::{declare_class, ClassType, DeclaredClass};

struct MyIvars;

declare_class!(
struct MyObject;

Expand All @@ -54,7 +61,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
);

// After
declare_class!(
use objc2::runtime::NSObject;
use objc2::define_class;

struct MyIvars;

define_class!(
#[unsafe(super(NSObject))]
#[name = "MyObject"]
#[ivars = MyIvars]
Expand All @@ -69,6 +81,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* **BREAKING**: Changed the syntax of `extern_class!` to be more succinct:
```rust
// Before
use objc2::mutability::MainThreadOnly;
use objc2::runtime::NSObject;
use objc2::{extern_class, ClassType};

extern_class!(
#[derive(PartialEq, Eq, Hash, Debug)]
struct MyClass;
Expand All @@ -81,6 +97,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
);

// After
use objc2::runtime::NSObject;
use objc2::{extern_class, MainThreadOnly};

extern_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
Expand Down Expand Up @@ -126,6 +145,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Deprecated `NSObjectProtocol::is_kind_of`, use `isKindOfClass` or the new
`AnyObject::downcast_ref` method instead.
* Deprecated `Retained::cast`, this has been renamed to `Retained::cast_unchecked`.
* Renamed `DeclaredClass` to `DefinedClass`.

### Removed
* **BREAKING**: Removed the `ffi::SEL` and `ffi::objc_selector` types. Use
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/examples/class_with_lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Note: We can't use the `declare_class!` macro for this, it doesn't support
//! such use-cases (yet). Instead, we'll declare the class manually.
//! Note: We can't use the `define_class!` macro for this, it doesn't support
//! such use-cases (yet). Instead, we'll create it manually.
#![deny(unsafe_op_in_unsafe_fn)]
use std::cell::Cell;
use std::marker::PhantomData;
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/__macro_helpers/common_selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ fn cxx_construct_sel() -> Sel {
/// undocumented in clang - but since the selector is emitted into the final
/// binary, it is fine to rely on it being used.
///
/// Unfortunately though, this only works if the class has been declared
/// Unfortunately though, this only works if the class has been defined
/// statically, since in that case a flag is set to inform the runtime that it
/// needs to run destructors. So unfortunately we can't use this on
/// dynamically declared classes.
/// dynamically defined classes.
///
///
/// # ABI
Expand Down
24 changes: 12 additions & 12 deletions crates/objc2/src/__macro_helpers/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait ConvertArgument: argument_private::Sealed {
type __StoredBeforeMessage: Sized;

#[doc(hidden)]
fn __from_declared_param(inner: Self::__Inner) -> Self;
fn __from_defined_param(inner: Self::__Inner) -> Self;

#[doc(hidden)]
fn __into_argument(self) -> (Self::__Inner, Self::__StoredBeforeMessage);
Expand All @@ -48,7 +48,7 @@ impl<T: EncodeArgument> ConvertArgument for T {
type __StoredBeforeMessage = ();

#[inline]
fn __from_declared_param(inner: Self::__Inner) -> Self {
fn __from_defined_param(inner: Self::__Inner) -> Self {
inner
}

Expand All @@ -65,7 +65,7 @@ impl ConvertArgument for bool {
type __StoredBeforeMessage = ();

#[inline]
fn __from_declared_param(inner: Self::__Inner) -> Self {
fn __from_defined_param(inner: Self::__Inner) -> Self {
inner.as_bool()
}

Expand All @@ -86,7 +86,7 @@ pub trait ConvertReturn: return_private::Sealed {
type __Inner: EncodeReturn;

#[doc(hidden)]
fn __into_declared_return(self) -> Self::__Inner;
fn __into_defined_return(self) -> Self::__Inner;

#[doc(hidden)]
fn __from_return(inner: Self::__Inner) -> Self;
Expand All @@ -97,7 +97,7 @@ impl<T: EncodeReturn> ConvertReturn for T {
type __Inner = Self;

#[inline]
fn __into_declared_return(self) -> Self::__Inner {
fn __into_defined_return(self) -> Self::__Inner {
self
}

Expand All @@ -112,7 +112,7 @@ impl ConvertReturn for bool {
type __Inner = Bool;

#[inline]
fn __into_declared_return(self) -> Self::__Inner {
fn __into_defined_return(self) -> Self::__Inner {
Bool::new(self)
}

Expand Down Expand Up @@ -295,7 +295,7 @@ mod tests {
TypeId::of::<<i32 as ConvertArgument>::__Inner>(),
TypeId::of::<i32>()
);
assert_eq!(<i32 as ConvertArgument>::__from_declared_param(42), 42);
assert_eq!(<i32 as ConvertArgument>::__from_defined_param(42), 42);
assert_eq!(ConvertArgument::__into_argument(42i32).0, 42);
}

Expand All @@ -305,21 +305,21 @@ mod tests {
TypeId::of::<<i8 as ConvertArgument>::__Inner>(),
TypeId::of::<i8>()
);
assert_eq!(<i8 as ConvertArgument>::__from_declared_param(-3), -3);
assert_eq!(<i8 as ConvertArgument>::__from_defined_param(-3), -3);
assert_eq!(ConvertArgument::__into_argument(-3i32).0, -3);
}

#[test]
fn convert_bool() {
assert!(!<bool as ConvertArgument>::__from_declared_param(Bool::NO));
assert!(<bool as ConvertArgument>::__from_declared_param(Bool::YES));
assert!(!<bool as ConvertArgument>::__from_defined_param(Bool::NO));
assert!(<bool as ConvertArgument>::__from_defined_param(Bool::YES));
assert!(!<bool as ConvertReturn>::__from_return(Bool::NO));
assert!(<bool as ConvertReturn>::__from_return(Bool::YES));

assert!(!ConvertArgument::__into_argument(false).0.as_bool());
assert!(ConvertArgument::__into_argument(true).0.as_bool());
assert!(!ConvertReturn::__into_declared_return(false).as_bool());
assert!(ConvertReturn::__into_declared_return(true).as_bool());
assert!(!ConvertReturn::__into_defined_return(false).as_bool());
assert!(ConvertReturn::__into_defined_return(true).as_bool());

#[cfg(all(target_vendor = "apple", target_os = "macos", target_arch = "x86_64"))]
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use crate::runtime::{
};
#[cfg(debug_assertions)]
use crate::runtime::{AnyProtocol, MethodDescription};
use crate::{AllocAnyThread, ClassType, DeclaredClass, Message, ProtocolType};
use crate::{AllocAnyThread, ClassType, DefinedClass, Message, ProtocolType};

use super::declared_ivars::{register_with_ivars, setup_dealloc};
use super::defined_ivars::{register_with_ivars, setup_dealloc};
use super::{Copy, Init, MaybeUnwrap, MutableCopy, New, Other};

/// Helper for determining auto traits of declared classes.
/// Helper for determining auto traits of defined classes.
///
/// This will contain either `dyn AllocAnyThread` or `dyn MainThreadOnly`, so
/// it will have no auto traits by default.
Expand Down Expand Up @@ -64,7 +64,7 @@ unsafe impl Encode for RetainedReturnValue {
}

// One could imagine a different design where we had a method like
// `fn convert_receiver()`, but that won't work in `declare_class!` since we
// `fn convert_receiver()`, but that won't work in `define_class!` since we
// can't actually modify the `self` argument (e.g. `let self = foo(self)` is
// not allowed).
//
Expand Down Expand Up @@ -143,7 +143,7 @@ where

/// Helper trait for specifying an `Retained<T>` or an `Option<Retained<T>>`.
///
/// (Both of those are valid return types from declare_class! `#[method_id]`).
/// (Both of those are valid return types from define_class! `#[method_id]`).
pub trait MaybeOptionRetained: MaybeUnwrap {
fn consumed_return(self) -> RetainedReturnValue;
fn autorelease_return(self) -> RetainedReturnValue;
Expand Down Expand Up @@ -195,7 +195,7 @@ fn create_builder(name: &str, superclass: &AnyClass) -> ClassBuilder {
}
}

impl<T: DeclaredClass> ClassBuilderHelper<T> {
impl<T: DefinedClass> ClassBuilderHelper<T> {
#[inline]
#[track_caller]
#[allow(clippy::new_without_default)]
Expand Down Expand Up @@ -302,7 +302,7 @@ pub struct ClassProtocolMethodsBuilder<'a, T: ?Sized> {
registered_class_methods: HashSet<Sel>,
}

impl<T: DeclaredClass> ClassProtocolMethodsBuilder<'_, T> {
impl<T: DefinedClass> ClassProtocolMethodsBuilder<'_, T> {
// Addition: This restricts to callee `T`
#[inline]
pub unsafe fn add_method<F>(&mut self, sel: Sel, func: F)
Expand Down
Loading

0 comments on commit 89efc9e

Please sign in to comment.