macro_rules! dispatch_object {
(
$(#[$m:meta])*
pub struct $type:ident;
) => {
$(#[$m])*
#[repr(C)]
pub struct $type {
inner: [u8; 0],
_p: $crate::OpaqueData,
}
unsafe impl $crate::DispatchObject for $type {}
impl core::convert::AsRef<Self> for $type {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl PartialEq for $type {
#[inline]
fn eq(&self, other: &Self) -> bool {
core::ptr::eq(self, other)
}
}
impl Eq for $type {}
impl core::hash::Hash for $type {
#[inline]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
let ptr: *const Self = self;
ptr.hash(state);
}
}
impl core::fmt::Debug for $type {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let ptr: *const Self = self;
f.debug_struct(core::stringify!($type))
.field("ptr", &ptr)
.finish_non_exhaustive()
}
}
#[cfg(feature = "objc2")]
unsafe impl objc2::encode::RefEncode for $type {
const ENCODING_REF: objc2::encode::Encoding
= objc2::encode::Encoding::Object;
}
#[cfg(feature = "objc2")]
unsafe impl objc2::Message for $type {}
#[cfg(feature = "objc2")]
impl core::convert::AsRef<objc2::runtime::AnyObject> for $type {
#[inline]
fn as_ref(&self) -> &objc2::runtime::AnyObject {
let ptr: *const Self = self;
let ptr: *const objc2::runtime::AnyObject = ptr.cast();
unsafe { &*ptr }
}
}
};
}
macro_rules! dispatch_object_not_data {
(unsafe $type:ident) => {
unsafe impl Send for $type {}
unsafe impl Sync for $type {}
};
}
macro_rules! enum_with_val {
($(#[$meta:meta])* $vis:vis struct $ident:ident($innervis:vis $ty:ty) {
$($(#[$varmeta:meta])* $variant:ident = $num:expr),* $(,)*
}) => {
$(#[$meta])*
#[repr(transparent)]
$vis struct $ident($innervis $ty);
#[allow(non_upper_case_globals)]
impl $ident {
$($(#[$varmeta])* $vis const $variant: Self = Self($num);)*
}
impl ::core::fmt::Debug for $ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self {
$(&$ident::$variant => write!(f, "{}::{}", stringify!($ident), stringify!($variant)),)*
&$ident(v) => write!(f, "UNKNOWN({})", v),
}
}
}
#[cfg(feature = "objc2")]
unsafe impl objc2::encode::Encode for $ident {
const ENCODING: objc2::encode::Encoding = <$ty as objc2::encode::Encode>::ENCODING;
}
#[cfg(feature = "objc2")]
unsafe impl objc2::encode::RefEncode for $ident {
const ENCODING_REF: objc2::encode::Encoding = objc2::encode::Encoding::Pointer(&<Self as objc2::encode::Encode>::ENCODING);
}
}
}