Skip to content

Commit e2e7caf

Browse files
committed
Add support for int promotion and pointers
1 parent 3a1a848 commit e2e7caf

1 file changed

Lines changed: 47 additions & 14 deletions

File tree

libcc2rs/src/va_args.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,32 @@ impl_from!(u32, UInt);
3535
impl_from!(i64, Long);
3636
impl_from!(u64, ULong);
3737
impl_from!(f64, Double);
38-
impl_from!(*mut c_void, RawPtr);
39-
impl_from!(*const c_void => RawPtr as *mut c_void);
40-
impl_from!(*mut i8 => RawPtr as *mut c_void);
41-
impl_from!(*const i8 => RawPtr as *mut c_void);
42-
impl_from!(*mut u8 => RawPtr as *mut c_void);
43-
impl_from!(*const u8 => RawPtr as *mut c_void);
38+
39+
// C promotion: char/short -> int, float -> double
40+
impl_from!(i8 => Int as i32);
41+
impl_from!(i16 => Int as i32);
42+
impl_from!(u8 => UInt as u32);
43+
impl_from!(u16 => UInt as u32);
44+
impl_from!(f32 => Double as f64);
4445
impl_from!(AnyPtr, Ptr);
4546

47+
impl<T: Clone + crate::reinterpret::ByteRepr + 'static> From<crate::rc::Ptr<T>> for VaArg {
48+
fn from(v: crate::rc::Ptr<T>) -> Self {
49+
VaArg::Ptr(v.to_any())
50+
}
51+
}
52+
53+
macro_rules! impl_from_ptr {
54+
($($ty:ty),*) => {
55+
$(
56+
impl_from!(*mut $ty => RawPtr as *mut c_void);
57+
impl_from!(*const $ty => RawPtr as *mut c_void);
58+
)*
59+
};
60+
}
61+
62+
impl_from_ptr!(c_void, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize);
63+
4664
pub struct VaList<'a> {
4765
args: &'a [VaArg],
4866
pos: usize,
@@ -154,11 +172,26 @@ impl VaArgGet for f64 {
154172
}
155173
}
156174

157-
impl_get_ptr!(
158-
*mut c_void,
159-
*const c_void,
160-
*mut i8,
161-
*const i8,
162-
*mut u8,
163-
*const u8
164-
);
175+
macro_rules! impl_get_ptr {
176+
($($ty:ty),*) => {
177+
$(
178+
impl VaArgGet for *mut $ty {
179+
fn get(v: &VaArg) -> Self { get_ptr(v) as *mut $ty }
180+
}
181+
impl VaArgGet for *const $ty {
182+
fn get(v: &VaArg) -> Self { get_ptr(v) as *const $ty }
183+
}
184+
)*
185+
};
186+
}
187+
188+
impl_get_ptr!(c_void, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize);
189+
190+
impl<T: 'static> VaArgGet for crate::rc::Ptr<T> {
191+
fn get(v: &VaArg) -> Self {
192+
match v {
193+
VaArg::Ptr(any) => any.cast::<T>().expect("VaList::arg: Ptr type mismatch"),
194+
_ => panic!("VaList::arg: expected Ptr, got different variant"),
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)