Skip to content
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

Add support for Objective-C generics and NSArray #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 61 additions & 0 deletions src/core/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// This macro is intentionally undocumented to ensure it is not publicly
// exported.

#![feature(log_syntax)]

macro_rules! subclass {
(
$(#[$meta:meta])+
Expand Down Expand Up @@ -59,6 +62,64 @@ macro_rules! subclass {
}
}
};
(
$(#[$meta:meta])+
$vis:vis class $a:ident <$lifetime:lifetime, $genty:ident> : $b:ty ;
) => {
$(#[$meta])+
#[repr(C)]
$vis struct $a <$lifetime, $genty> ($b, std::marker::PhantomData<&$lifetime $genty>);

impl <$lifetime, $genty> $crate::core::ObjectType for $a <$lifetime, $genty> {
#[inline]
fn retain(obj: &Self) -> $crate::core::Arc<Self> {
let obj = $crate::core::Arc::retain(&obj.0);
unsafe { $crate::core::Arc::cast_unchecked(obj) }
}

#[inline]
unsafe fn release(obj: std::ptr::NonNull<Self>) {
<$b>::release(obj.cast());
}
}

impl <$lifetime, $genty> std::ops::Deref for $a <$lifetime, $genty> {
type Target = $b;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl <$lifetime, $genty> AsRef<$a <$lifetime, $genty>> for $a <$lifetime, $genty> {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}

impl <$lifetime, $genty> AsMut<$a <$lifetime, $genty>> for $a <$lifetime, $genty> {
#[inline]
fn as_mut(&mut self) -> &mut Self {
self
}
}

impl <$lifetime, $genty, _T> AsRef<_T> for $a <$lifetime, $genty> where $b: AsRef<_T> {
#[inline]
fn as_ref(&self) -> &_T {
self.0.as_ref()
}
}

impl <$lifetime, $genty, _T> AsMut<_T> for $a <$lifetime, $genty> where $b: AsMut<_T> {
#[inline]
fn as_mut(&mut self) -> &mut _T {
self.0.as_mut()
}
}
};
}

// This macro is intentionally undocumented to ensure it is not publicly
Expand Down
2 changes: 2 additions & 0 deletions src/foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod error_codes;

mod cmp;
mod geometry;
mod ns_array;
mod ns_error;
mod ns_exception;
mod ns_null;
Expand All @@ -26,6 +27,7 @@ mod ns_value;

pub use cmp::*;
pub use geometry::*;
pub use ns_array::*;
pub use ns_error::*;
pub use ns_exception::*;
pub use ns_null::*;
Expand Down
6 changes: 6 additions & 0 deletions src/foundation/ns_array/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[macro_export]
macro_rules! ns_array {
($s: expr) => {{

}}
}
22 changes: 22 additions & 0 deletions src/foundation/ns_array/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::core::Arc;
use crate::core::ObjectType;
use crate::objc::{NSObject, NSUInteger};

objc_subclass! {
/// A static ordered collection of objects.
///
// See [documentation](https://developer.apple.com/documentation/foundation/nsarray?language=objc)
pub class NSArray<'data, T>: NSObject<'data>;
}

impl<'a, T: ObjectType> NSArray<'a, T> {
/// The number of objects in the array.
pub fn count(&self) -> NSUInteger {
unsafe { _msg_send_any![self, count] }
}

/// Returns the object located at the specified index.
pub fn object_at_index(&self, index: NSUInteger) -> Arc<T> {
unsafe { _msg_send_any![self, object_at_index: index] }
}
}
33 changes: 33 additions & 0 deletions src/objc/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
macro_rules! objc_class_type {
($obj:ident <$lifetime:lifetime, $genty:ident>) => {
objc_class_type!($obj <$lifetime, $genty>, stringify!($obj));
};
($obj:ident <$lifetime:lifetime, $genty:ident>, $class:expr) => {
objc_class_type!(@processed_generic
$obj <$lifetime, $genty>,
$class,
concat!("OBJC_CLASS_$_", $class)
);
};
(@processed_generic $obj:ident <$lifetime:lifetime, $genty:ident>, $class:expr, $class_symbol:expr) => {
impl<$lifetime, $genty> $crate::objc::ClassType<$lifetime> for $obj<$lifetime, $genty>
where $genty: $crate::objc::ObjectType<$lifetime> {
#[inline]
fn class() -> &'static $crate::objc::Class {
$crate::_objc_class!(@ $class_symbol)
}
}
};
($obj:ident $(<$lifetime:lifetime>)?) => {
objc_class_type!($obj $(<$lifetime>)?, stringify!($obj));
};
Expand Down Expand Up @@ -58,6 +77,20 @@ macro_rules! objc_subclass {

objc_class_type!($a <$lifetime>);
};
(
$(#[$meta:meta])+
$vis:vis class $a:ident <$lifetime:lifetime, $genty:ident> : $b:ty ;
) => {
subclass! {
$(#[$meta])+
$vis class $a <$lifetime, $genty> : $b ;
}

impl<$lifetime, $genty> $crate::objc::ObjectType<$lifetime> for $a<$lifetime, $genty>
where $genty: $crate::objc::ObjectType<$lifetime> {}

objc_class_type!($a <$lifetime, $genty>);
};
}

// This macro is intentionally undocumented to ensure it is not publicly
Expand Down