macro_rules! __impl_as_ref_borrow {
($name:ident<$($t:ident $(: $b:ident)?),*>,) => {};
($name:ident<$($t:ident $(: $b:ident)?),*>, $item:ty, $($tail:ty,)*) => {
impl<$($t $(: $b)?),*> AsRef<$item> for $name<$($t),*> {
#[inline]
fn as_ref(&self) -> &$item {
&*self
}
}
impl<$($t $(: $b)?),*> AsMut<$item> for $name<$($t),*> {
#[inline]
fn as_mut(&mut self) -> &mut $item {
&mut *self
}
}
impl<$($t $(: $b)?),*> ::core::borrow::Borrow<$item> for $name<$($t),*> {
#[inline]
fn borrow(&self) -> &$item {
&*self
}
}
impl<$($t $(: $b)?),*> ::core::borrow::BorrowMut<$item> for $name<$($t),*> {
#[inline]
fn borrow_mut(&mut self) -> &mut $item {
&mut *self
}
}
__impl_as_ref_borrow!($name<$($t $(: $b)?),*>, $($tail,)*);
};
}
macro_rules! object {
(
$(#[$m:meta])*
unsafe $v:vis struct $name:ident: $($inheritance_chain:ty),+ $(;)?
) => {
object! {
@__inner
$(#[$m])*
unsafe $v struct $name<>: $($inheritance_chain,)+ ::objc2::runtime::Object {}
}
};
(
$(#[$m:meta])*
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $($inheritance_chain:ty),+ {
$($p:ident: $pty:ty,)*
}
) => {
object! {
@__inner
$(#[$m])*
unsafe $v struct $name<$($t $(: $b)?),*>: $($inheritance_chain,)+ ::objc2::runtime::Object {
$($p: $pty,)*
}
}
};
(
@__inner
$(#[$m:meta])*
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $inherits:ty $(, $inheritance_rest:ty)* {
$($p:ident: $pty:ty,)*
}
) => {
$(#[$m])*
#[repr(C)]
$v struct $name<$($t $(: $b)?),*> {
inner: $inherits,
$($p: $pty),*
}
unsafe impl<$($t $(: $b)?),*> ::objc2::Message for $name<$($t),*> { }
unsafe impl<$($t $(: $b)?),*> ::objc2::RefEncode for $name<$($t),*> {
const ENCODING_REF: ::objc2::Encoding<'static> = ::objc2::Encoding::Object;
}
impl<$($t $(: $b)?),*> $name<$($t),*> {
pub fn class() -> &'static ::objc2::runtime::Class {
::objc2::class!($name)
}
}
impl<$($t $(: $b)?),*> ::core::ops::Deref for $name<$($t),*> {
type Target = $inherits;
#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<$($t $(: $b)?),*> ::core::ops::DerefMut for $name<$($t),*> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<$($t $(: $b)?),*> AsRef<Self> for $name<$($t),*> {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl<$($t $(: $b)?),*> AsMut<Self> for $name<$($t),*> {
#[inline]
fn as_mut(&mut self) -> &mut Self {
self
}
}
__impl_as_ref_borrow!($name<$($t $(: $b)?),*>, $inherits, $($inheritance_rest,)*);
impl<$($t: ::core::cmp::PartialEq $(+ $b)?),*> ::core::cmp::PartialEq for $name<$($t),*> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.is_equal(other.as_ref())
}
}
impl<$($t: ::core::cmp::Eq $(+ $b)?),*> ::core::cmp::Eq for $name<$($t),*> {}
impl<$($t: ::core::hash::Hash $(+ $b)?),*> ::core::hash::Hash for $name<$($t),*> {
#[inline]
fn hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
self.hash_code().hash(state);
}
}
impl<$($t $(: $b)?),*> ::core::fmt::Debug for $name<$($t),*> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
use ::alloc::borrow::ToOwned;
use $crate::NSObject;
let this: &NSObject = self.as_ref();
let s = ::objc2::rc::autoreleasepool(|pool| {
this.description().as_str(pool).to_owned()
});
::core::fmt::Debug::fmt(&s, f)
}
}
};
}
macro_rules! unsafe_def_fn {
(
$(#[$m:meta])*
$v:vis fn new -> $o:ty $(;)?
) => {
$(#[$m])*
$v fn new() -> Id<Self, $o> {
let cls = Self::class();
unsafe { Id::new(msg_send![cls, new]).unwrap() }
}
};
}