1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#![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};

/// Get raw references trait
pub trait GetRef {
    /// Raw LLVM reference type
    type RawRef;
    /// Get LLVM raw reference
    fn get_ref(&self) -> Self::RawRef;
}

/// `c_uint` wrapper (from C-type)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct CUint(c_uint);

impl From<u32> for CUint {
    fn from(value: u32) -> Self {
        // Force to unwrap c_uint
        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 {
        // Force to unwrap c_uint
        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
    }
}

/// `c_double` wrapper (from C-type)
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct CDouble(c_double);

impl From<f64> for CDouble {
    fn from(value: f64) -> Self {
        // Force to unwrap
        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
    }
}

/// `c_int` wrapper (from C-type)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct CInt(c_int);

impl From<i32> for CInt {
    fn from(value: i32) -> Self {
        // Force to unwrap c_int
        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
    }
}

/// `size_t` wrapper (from C-type)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SizeT(size_t);

impl From<usize> for SizeT {
    fn from(value: usize) -> Self {
        // Force to unwrap size_t
        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
    }
}

/// `CString` wrapper
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CString(std::ffi::CString);

impl From<&str> for CString {
    fn from(value: &str) -> Self {
        // Force to unwrap `CString`
        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
    }
}

/// `CStr` wrapper
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CStr<'a>(&'a std::ffi::CStr);

impl<'a> CStr<'a> {
    /// Initialize wrapped `CStr`
    /// ## Safety
    /// NOTE: Safety considerations same as for `std::ffi::CStr::from_ptr`.
    #[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")
    }
}

/// Wrapping for `*mut c_void`
#[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
    }
}