Skip to content

Commit f8458d4

Browse files
committed
Add macro to types module for auto-implementing Sub
1 parent 2669dc6 commit f8458d4

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/ty.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ use std::marker::PhantomData;
1010
use std::iter::Iterator;
1111
use std::ops::Deref;
1212

13+
macro_rules! sub {
14+
($ty:ty, $kind:ident) => (
15+
unsafe impl Sub<::Type> for $ty {
16+
fn is(ty: &Type) -> bool {
17+
unsafe {
18+
let kind = core::LLVMGetTypeKind(ty.into());
19+
kind as c_uint == LLVMTypeKind::$kind as c_uint
20+
}
21+
}
22+
}
23+
deref!{$ty, Type}
24+
)
25+
}
26+
1327
/// Defines how a value should be laid out in memory.
1428
pub struct Type(PhantomData<[u8]>);
1529
native_ref!(&Type = LLVMTypeRef);
@@ -35,10 +49,6 @@ impl Type {
3549
pub fn new_pointer<'a>(elem: &'a Type) -> &'a Type {
3650
unsafe { core::LLVMPointerType(elem.into(), 0 as c_uint) }.into()
3751
}
38-
/// Make a new structure type with the given types.
39-
pub fn new_struct<'a>(context:&'a Context, elems: &[&'a Type], packed: bool) -> &'a Type {
40-
unsafe { core::LLVMStructTypeInContext(context.into(), elems.as_ptr() as *mut LLVMTypeRef, elems.len() as c_uint, packed as c_int) }.into()
41-
}
4252
/// Returns true if the size of the type is known at compile-time.
4353
///
4454
/// This is equivalent to the type implementing `Sized` in Rust
@@ -92,6 +102,7 @@ to_str!(Type, LLVMPrintTypeToString);
92102
/// A structure type, such as a tuple or struct.
93103
pub struct StructType;
94104
native_ref!(&StructType = LLVMTypeRef);
105+
sub!{StructType, LLVMStructTypeKind}
95106
impl StructType {
96107
/// Make a new struct with the given fields and packed representation.
97108
pub fn new<'a>(context: &'a Context, fields: &[&'a Type], packed: bool) -> &'a StructType {
@@ -115,15 +126,6 @@ impl StructType {
115126
}
116127
}
117128
}
118-
unsafe impl Sub<Type> for StructType {
119-
fn is(ty: &Type) -> bool {
120-
unsafe {
121-
let kind = core::LLVMGetTypeKind(ty.into());
122-
kind as c_uint == LLVMTypeKind::LLVMStructTypeKind as c_uint
123-
}
124-
}
125-
}
126-
deref!(StructType, Type);
127129
get_context!(StructType, LLVMGetTypeContext);
128130
to_str!(StructType, LLVMPrintTypeToString);
129131

0 commit comments

Comments
 (0)