#![deny(clippy::nursery, clippy::pedantic)]
#![allow(
clippy::module_name_repetitions,
clippy::new_without_default,
clippy::doc_lazy_continuation
)]
pub mod basic_block;
pub mod builder;
pub mod core;
use libc::{c_char, c_double, c_int, c_uint, size_t};
use std::ops::{Deref, DerefMut};
pub trait GetRef {
type RawRef;
fn get_ref(&self) -> Self::RawRef;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct CUint(c_uint);
impl From<u32> for CUint {
fn from(value: u32) -> Self {
Self(c_uint::try_from(value).expect("c_unit casting fail from u32"))
}
}
impl From<CUint> for u32 {
fn from(value: CUint) -> Self {
value.0
}
}
impl From<usize> for CUint {
fn from(value: usize) -> Self {
Self(c_uint::try_from(value).expect("c_uint casting fail from usize"))
}
}
impl Deref for CUint {
type Target = c_uint;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for CUint {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct CDouble(c_double);
impl From<f64> for CDouble {
fn from(value: f64) -> Self {
Self(c_double::try_from(value).expect("c_double casting fail from u32"))
}
}
impl From<CDouble> for f64 {
fn from(value: CDouble) -> Self {
value.0
}
}
impl Deref for CDouble {
type Target = c_double;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct CInt(c_int);
impl From<i32> for CInt {
fn from(value: i32) -> Self {
Self(c_int::try_from(value).expect("c_int casting fail from i32"))
}
}
impl From<bool> for CInt {
fn from(value: bool) -> Self {
Self(c_int::from(value))
}
}
impl Deref for CInt {
type Target = c_int;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SizeT(size_t);
impl From<usize> for SizeT {
fn from(value: usize) -> Self {
Self(size_t::try_from(value).expect("size_t casting fail from usize"))
}
}
impl Deref for SizeT {
type Target = size_t;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for SizeT {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CString(std::ffi::CString);
impl From<&str> for CString {
fn from(value: &str) -> Self {
Self(std::ffi::CString::new(value).expect("CString casting fail from str"))
}
}
impl Deref for CString {
type Target = std::ffi::CString;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CStr<'a>(&'a std::ffi::CStr);
impl<'a> CStr<'a> {
#[must_use]
pub const unsafe fn new(value: *const c_char) -> Self {
unsafe { Self(std::ffi::CStr::from_ptr(value)) }
}
}
impl<'a> Deref for CStr<'a> {
type Target = std::ffi::CStr;
fn deref(&self) -> &Self::Target {
self.0
}
}
#[allow(clippy::to_string_trait_impl)]
impl<'a> ToString for CStr<'a> {
fn to_string(&self) -> String {
self.0
.to_str()
.map(ToString::to_string)
.expect("Failed to convert CStr to String")
}
}
#[derive(Debug, Copy, Clone)]
pub struct UnsafeMutVoidPtr(*mut std::ffi::c_void);
impl Deref for UnsafeMutVoidPtr {
type Target = *mut std::ffi::c_void;
fn deref(&self) -> &Self::Target {
&self.0
}
}