#[doc(alias = "@interface")]
#[macro_export]
macro_rules! extern_class {
(
$(#[$m:meta])*
$v:vis struct $name:ident;
$(#[$impl_m:meta])*
unsafe impl ClassType for $for:ty {
$(#[inherits($($inheritance_rest:ty),+)])?
type Super = $superclass:ty;
type Mutability = $mutability:ty;
$(const NAME: &'static str = $name_const:expr;)?
}
) => {
$crate::extern_class!(
$(#[$m])*
$v struct $name {}
$(#[$impl_m])*
unsafe impl ClassType for $for {
$(#[inherits($($inheritance_rest),+)])?
type Super = $superclass;
type Mutability = $mutability;
$(const NAME: &'static str = $name_const;)?
}
);
};
(
$(#[$m:meta])*
$v:vis struct $name:ident {
$($field_vis:vis $field:ident: $field_ty:ty,)*
}
$(#[$impl_m:meta])*
unsafe impl ClassType for $for:ty {
$(#[inherits($($inheritance_rest:ty),+)])?
type Super = $superclass:ty;
type Mutability = $mutability:ty;
$(const NAME: &'static str = $name_const:expr;)?
}
) => {
$crate::__inner_extern_class!(
$(#[$m])*
$v struct $name<> {
__superclass: $superclass,
$($field_vis $field: $field_ty,)*
}
$(#[$impl_m])*
unsafe impl<> ClassType for $for {
$(#[inherits($($inheritance_rest),+)])?
type Super = $superclass;
type Mutability = $mutability;
fn as_super(&self) -> &Self::Super {
&self.__superclass
}
fn as_super_mut(&mut self) -> &mut Self::Super {
&mut self.__superclass
}
$(const NAME: &'static str = $name_const;)?
}
);
$(#[$impl_m])*
const _: () = {
if $crate::__macro_helpers::size_of::<$name>() != 0 {
$crate::__macro_helpers::panic!($crate::__macro_helpers::concat!(
"the struct ",
$crate::__macro_helpers::stringify!($name),
" is not zero-sized!",
))
}
};
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_as_ref_borrow {
{
impl ($($t:tt)*) for $for:ty {
fn as_ref($($self:tt)*) $ref:block
fn as_mut($($self_mut:tt)*) $mut:block
}
()
} => {};
{
impl ($($t:tt)*) for $for:ty {
fn as_ref($($self:tt)*) $ref:block
fn as_mut($($self_mut:tt)*) $mut:block
}
($item:ty, $($tail:ty,)*)
} => {
impl<$($t)*> $crate::__macro_helpers::AsRef<$item> for $for {
#[inline]
fn as_ref($($self)*) -> &$item $ref
}
impl<$($t)*> $crate::__macro_helpers::AsMut<$item> for $for {
#[inline]
fn as_mut($($self_mut)*) -> &mut $item $mut
}
impl<$($t)*> $crate::__macro_helpers::Borrow<$item> for $for {
#[inline]
fn borrow($($self)*) -> &$item $ref
}
impl<$($t)*> $crate::__macro_helpers::BorrowMut<$item> for $for {
#[inline]
fn borrow_mut($($self_mut)*) -> &mut $item $mut
}
$crate::__impl_as_ref_borrow! {
impl ($($t)*) for $for {
fn as_ref($($self)*) $ref
fn as_mut($($self_mut)*) $mut
}
($($tail,)*)
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __inner_extern_class {
(
$(#[$m:meta])*
$v:vis struct $name:ident<$($t_struct:ident $(: $(?$b_sized_struct:ident)? $($b_struct:ident)? $(= $default:ty)?)?),* $(,)?> {
$superclass_field:ident: $superclass_field_ty:ty,
$($fields:tt)*
}
$(#[$impl_m:meta])*
unsafe impl<$($t_for:ident $(: $(?$b_sized_for:ident +)? $b_for:ident)?),* $(,)?> ClassType for $for:ty {
$(#[inherits($($inheritance_rest:ty),+ $(,)?)])?
type Super = $superclass:ty;
type Mutability = $mutability:ty;
fn as_super(&$as_super_self:ident) -> &Self::Super $as_super:block
fn as_super_mut(&mut $as_super_mut_self:ident) -> &mut Self::Super $as_super_mut:block
$(const NAME: &'static str = $name_const:expr;)?
}
) => {
$(#[$m])*
#[repr(C)]
$v struct $name<$($t_struct $(: $(?$b_sized_struct)? $($b_struct)? $(= $default)?)?),*> {
$superclass_field: $superclass_field_ty,
$($fields)*
}
$crate::__extern_class_impl_traits! {
$(#[$impl_m])*
unsafe impl ($($t_for $(: $(?$b_sized_for +)? $b_for)?),*) for $for {
INHERITS = [$superclass, $($($inheritance_rest,)+)? $crate::runtime::AnyObject];
fn as_super(&$as_super_self) $as_super
fn as_super_mut(&mut $as_super_mut_self) $as_super_mut
}
}
$(#[$impl_m])*
unsafe impl<$($t_for $(: $(?$b_sized_for +)? $b_for)?),*> ClassType for $for {
type Super = $superclass;
type Mutability = $mutability;
const NAME: &'static $crate::__macro_helpers::str = $crate::__select_name!($name; $($name_const)?);
#[inline]
fn class() -> &'static $crate::runtime::AnyClass {
$crate::__macro_helpers::assert_mutability_matches_superclass_mutability::<Self>();
$crate::__class_inner!(
$crate::__select_name!($name; $($name_const)?),
$crate::__hash_idents!($name)
)
}
#[inline]
fn as_super(&$as_super_self) -> &Self::Super $as_super
#[inline]
fn as_super_mut(&mut $as_super_mut_self) -> &mut Self::Super $as_super_mut
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __extern_class_impl_traits {
(
$(#[$impl_m:meta])*
unsafe impl ($($t:tt)*) for $for:ty {
INHERITS = [$superclass:ty $(, $inheritance_rest:ty)*];
fn as_super(&$as_super_self:ident) $as_super:block
fn as_super_mut(&mut $as_super_mut_self:ident) $as_super_mut:block
}
) => {
$(#[$impl_m])*
unsafe impl<$($t)*> $crate::RefEncode for $for {
const ENCODING_REF: $crate::Encoding
= <$superclass as $crate::RefEncode>::ENCODING_REF;
}
$(#[$impl_m])*
unsafe impl<$($t)*> $crate::Message for $for {}
$(#[$impl_m])*
impl<$($t)*> $crate::__macro_helpers::Deref for $for {
type Target = $superclass;
#[inline]
fn deref(&$as_super_self) -> &Self::Target $as_super
}
$(#[$impl_m])*
impl<$($t)*> $crate::__macro_helpers::DerefMut for $for {
#[inline]
fn deref_mut(&mut $as_super_mut_self) -> &mut Self::Target $as_super_mut
}
$(#[$impl_m])*
impl<$($t)*> $crate::__macro_helpers::AsRef<Self> for $for {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
$(#[$impl_m])*
impl<$($t)*> $crate::__macro_helpers::AsMut<Self> for $for {
#[inline]
fn as_mut(&mut self) -> &mut Self {
self
}
}
$(#[$impl_m])*
$crate::__impl_as_ref_borrow! {
impl ($($t)*) for $for {
fn as_ref(&self) {
&*self
}
fn as_mut(&mut self) {
&mut *self
}
}
($superclass, $($inheritance_rest,)*)
}
};
}