use std::ptr::NonNull;
use libperl_sys::COP;
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Cop(NonNull<COP>);
impl Cop {
#[inline]
pub unsafe fn from_raw_unchecked(p: *const COP) -> Self {
debug_assert!(!p.is_null(), "Cop::from_raw_unchecked received a null pointer");
Cop(unsafe { NonNull::new_unchecked(p as *mut COP) })
}
#[inline]
pub fn from_raw(p: *const COP) -> Option<Self> {
NonNull::new(p as *mut COP).map(Cop)
}
#[inline]
pub fn as_ptr(&self) -> *mut COP {
self.0.as_ptr()
}
#[inline]
pub fn line(&self) -> u32 {
unsafe { libperl_sys::CopLINE(self.0.as_ptr()) }
}
pub fn file(&self) -> Option<String> {
let p = unsafe { libperl_sys::CopFILE(self.0.as_ptr()) };
if p.is_null() {
None
} else {
Some(
unsafe { std::ffi::CStr::from_ptr(p) }
.to_string_lossy()
.into_owned(),
)
}
}
}