llvm_lib/
lib.rs

1#![deny(clippy::nursery, clippy::pedantic)]
2#![allow(
3    clippy::module_name_repetitions,
4    clippy::new_without_default,
5    clippy::doc_lazy_continuation
6)]
7pub mod basic_block;
8pub mod builder;
9pub mod core;
10
11#[macro_use]
12mod macros;
13
14use libc::{c_char, c_double, c_int, c_uint, size_t};
15use std::ops::{Deref, DerefMut};
16
17/// Get raw references trait
18pub trait GetRef {
19    /// Raw LLVM reference type
20    type RawRef;
21    /// Get LLVM raw reference
22    fn get_ref(&self) -> Self::RawRef;
23}
24
25/// `c_uint` wrapper (from C-type)
26#[derive(Debug, Copy, Clone, PartialEq, Eq)]
27pub struct CUint(c_uint);
28
29impl From<u32> for CUint {
30    fn from(value: u32) -> Self {
31        // Force to unwrap c_uint
32        Self(c_uint::try_from(value).expect("c_unit casting fail from u32"))
33    }
34}
35
36impl From<CUint> for u32 {
37    fn from(value: CUint) -> Self {
38        value.0
39    }
40}
41
42impl From<usize> for CUint {
43    fn from(value: usize) -> Self {
44        // Force to unwrap c_uint
45        Self(c_uint::try_from(value).expect("c_uint casting fail from usize"))
46    }
47}
48
49impl Deref for CUint {
50    type Target = c_uint;
51    fn deref(&self) -> &Self::Target {
52        &self.0
53    }
54}
55
56impl DerefMut for CUint {
57    fn deref_mut(&mut self) -> &mut Self::Target {
58        &mut self.0
59    }
60}
61
62/// `c_double` wrapper (from C-type)
63#[derive(Debug, Copy, Clone, PartialEq)]
64pub struct CDouble(c_double);
65
66impl From<f64> for CDouble {
67    fn from(value: f64) -> Self {
68        // Force to unwrap
69        Self(c_double::try_from(value).expect("c_double casting fail from u32"))
70    }
71}
72
73impl From<CDouble> for f64 {
74    fn from(value: CDouble) -> Self {
75        value.0
76    }
77}
78
79impl Deref for CDouble {
80    type Target = c_double;
81    fn deref(&self) -> &Self::Target {
82        &self.0
83    }
84}
85
86/// `c_int` wrapper (from C-type)
87#[derive(Debug, Copy, Clone, PartialEq, Eq)]
88pub struct CInt(c_int);
89
90impl From<i32> for CInt {
91    fn from(value: i32) -> Self {
92        // Force to unwrap c_int
93        Self(c_int::try_from(value).expect("c_int casting fail from i32"))
94    }
95}
96
97impl From<bool> for CInt {
98    fn from(value: bool) -> Self {
99        Self(c_int::from(value))
100    }
101}
102
103impl Deref for CInt {
104    type Target = c_int;
105    fn deref(&self) -> &Self::Target {
106        &self.0
107    }
108}
109
110/// `size_t` wrapper (from C-type)
111#[derive(Debug, Copy, Clone, PartialEq, Eq)]
112pub struct SizeT(size_t);
113
114impl From<usize> for SizeT {
115    fn from(value: usize) -> Self {
116        // Force to unwrap size_t
117        Self(size_t::try_from(value).expect("size_t casting fail from usize"))
118    }
119}
120
121impl Deref for SizeT {
122    type Target = size_t;
123    fn deref(&self) -> &Self::Target {
124        &self.0
125    }
126}
127
128impl DerefMut for SizeT {
129    fn deref_mut(&mut self) -> &mut Self::Target {
130        &mut self.0
131    }
132}
133
134/// `CString` wrapper
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub struct CString(std::ffi::CString);
137
138impl From<&str> for CString {
139    fn from(value: &str) -> Self {
140        // Force to unwrap `CString`
141        Self(std::ffi::CString::new(value).expect("CString casting fail from str"))
142    }
143}
144
145impl Deref for CString {
146    type Target = std::ffi::CString;
147    fn deref(&self) -> &Self::Target {
148        &self.0
149    }
150}
151
152/// `CStr` wrapper
153#[derive(Debug, Clone, PartialEq, Eq)]
154pub struct CStr<'a>(&'a std::ffi::CStr);
155
156impl<'a> CStr<'a> {
157    /// Initialize wrapped `CStr`
158    /// ## Safety
159    /// NOTE: Safety considerations same as for `std::ffi::CStr::from_ptr`.
160    #[must_use]
161    pub const unsafe fn new(value: *const c_char) -> Self {
162        unsafe { Self(std::ffi::CStr::from_ptr(value)) }
163    }
164}
165
166impl<'a> Deref for CStr<'a> {
167    type Target = std::ffi::CStr;
168    fn deref(&self) -> &Self::Target {
169        self.0
170    }
171}
172
173#[allow(clippy::to_string_trait_impl)]
174impl<'a> ToString for CStr<'a> {
175    fn to_string(&self) -> String {
176        self.0
177            .to_str()
178            .map(ToString::to_string)
179            .expect("Failed to convert CStr to String")
180    }
181}
182
183/// Wrapping for `*mut c_void`
184#[derive(Debug, Copy, Clone)]
185pub struct UnsafeMutVoidPtr(*mut std::ffi::c_void);
186
187impl Deref for UnsafeMutVoidPtr {
188    type Target = *mut std::ffi::c_void;
189    fn deref(&self) -> &Self::Target {
190        &self.0
191    }
192}