use crate::objc::Sel;
use std::{ffi::CStr, fmt, marker::PhantomData, os::raw::c_char, ptr::NonNull};
#[repr(C)]
#[derive(Copy, Clone)]
pub struct MethodDescription<'a> {
name: Sel,
type_encoding: NonNull<c_char>,
_marker: PhantomData<&'a CStr>,
}
unsafe impl Send for MethodDescription<'_> {}
unsafe impl Sync for MethodDescription<'_> {}
impl fmt::Debug for MethodDescription<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MethodDescription")
.field("name", &self.name())
.field("type_encoding", &self.type_encoding())
.finish()
}
}
impl<'a> MethodDescription<'a> {
#[inline]
pub const fn new(name: Sel, types: &'a CStr) -> Self {
unsafe { Self::from_raw_parts(name, types.as_ptr()) }
}
#[inline]
pub const unsafe fn from_raw_parts(name: Sel, type_encoding: *const c_char) -> Self {
Self {
name,
type_encoding: NonNull::new_unchecked(type_encoding as *mut c_char),
_marker: PhantomData,
}
}
#[inline]
pub fn name(&self) -> Sel {
self.name
}
#[inline]
pub fn type_encoding(&self) -> &'a CStr {
unsafe { CStr::from_ptr(self.type_encoding.as_ptr()) }
}
}