use crate::objc::{sys, Sel};
use std::{ffi::CStr, fmt};
mod description;
pub use description::*;
pub type Imp = unsafe extern "C" fn();
#[repr(C)]
pub struct Method {
_data: std::cell::UnsafeCell<[u8; 0]>,
}
unsafe impl Send for Method {}
unsafe impl Sync for Method {}
impl std::panic::RefUnwindSafe for Method {}
impl fmt::Debug for Method {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MethodDescription")
.field("name", &self.name())
.field("type_encoding", &self.type_encoding())
.field("implementation", &self.implementation())
.finish()
}
}
impl Method {
#[inline]
#[doc(alias = "method_getNumberOfArguments")]
pub fn num_args(&self) -> u32 {
unsafe { sys::method_getNumberOfArguments(self) }
}
#[inline]
#[doc(alias = "method_getName")]
pub fn name(&self) -> Sel {
unsafe { sys::method_getName(self) }
}
#[inline]
pub fn name_c_str(&self) -> &'static CStr {
self.name().name()
}
#[inline]
#[doc(alias = "method_getTypeEncoding")]
pub fn type_encoding(&self) -> Option<&CStr> {
unsafe {
let encoding = sys::method_getTypeEncoding(self);
if encoding.is_null() {
None
} else {
Some(CStr::from_ptr(encoding))
}
}
}
#[inline]
#[doc(alias = "method_getDescription")]
pub fn description(&self) -> &MethodDescription {
unsafe { self.description_mut() }
}
#[inline]
#[doc(alias = "method_getDescription")]
pub unsafe fn description_mut(&self) -> &mut MethodDescription<'_> {
&mut *sys::method_getDescription(self)
}
#[inline]
#[doc(alias = "method_getImplementation")]
pub fn implementation(&self) -> Imp {
unsafe { sys::method_getImplementation(self) }
}
#[inline]
#[doc(alias = "method_setImplementation")]
pub unsafe fn set_implementation(&self, imp: Imp) -> Imp {
sys::method_setImplementation(self, imp)
}
#[inline]
#[doc(alias = "method_exchangeImplementations")]
pub unsafe fn exchange_implementation(&self, other: &Self) {
sys::method_exchangeImplementations(self, other);
}
}