1use std::ffi::CStr;
2use std::fmt;
3
4use crate::support::{LLVMString, LLVMStringOrRaw};
5
6#[derive(Eq)]
7pub struct DataLayout {
8 pub(crate) data_layout: LLVMStringOrRaw,
9}
10
11impl DataLayout {
12 pub(crate) unsafe fn new_owned(data_layout: *const ::libc::c_char) -> DataLayout {
13 debug_assert!(!data_layout.is_null());
14
15 DataLayout {
16 data_layout: LLVMStringOrRaw::Owned(LLVMString::new(data_layout)),
17 }
18 }
19
20 pub(crate) unsafe fn new_borrowed(data_layout: *const ::libc::c_char) -> DataLayout {
21 debug_assert!(!data_layout.is_null());
22
23 DataLayout {
24 data_layout: LLVMStringOrRaw::Borrowed(data_layout),
25 }
26 }
27
28 pub fn as_str(&self) -> &CStr {
29 self.data_layout.as_str()
30 }
31
32 pub fn as_ptr(&self) -> *const ::libc::c_char {
33 match self.data_layout {
34 LLVMStringOrRaw::Owned(ref llvm_string) => llvm_string.ptr,
35 LLVMStringOrRaw::Borrowed(ptr) => ptr,
36 }
37 }
38}
39
40impl PartialEq for DataLayout {
41 fn eq(&self, other: &DataLayout) -> bool {
42 self.as_str() == other.as_str()
43 }
44}
45
46impl fmt::Debug for DataLayout {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 f.debug_struct("DataLayout")
49 .field("address", &self.as_ptr())
50 .field("repr", &self.as_str())
51 .finish()
52 }
53}