Skip to content

Commit a0e2b8b

Browse files
committed
Remove bloat from VaArg, VaList and VaArgGet
1 parent 8b800d7 commit a0e2b8b

1 file changed

Lines changed: 60 additions & 137 deletions

File tree

libcc2rs/src/va_args.rs

Lines changed: 60 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,38 @@ pub enum VaArg {
1313
Ptr(AnyPtr),
1414
}
1515

16-
macro_rules! impl_from {
17-
($ty:ty, $variant:ident) => {
16+
macro_rules! impl_va_arg_from {
17+
(direct: $($ty:ty => $variant:ident),*) => {$(
1818
impl From<$ty> for VaArg {
19-
fn from(v: $ty) -> Self {
20-
VaArg::$variant(v)
21-
}
19+
fn from(v: $ty) -> Self { VaArg::$variant(v) }
2220
}
23-
};
24-
($ty:ty => $variant:ident as $cast:ty) => {
21+
)*};
22+
(promote: $($ty:ty => $variant:ident as $cast:ty),*) => {$(
2523
impl From<$ty> for VaArg {
26-
fn from(v: $ty) -> Self {
27-
VaArg::$variant(v as $cast)
28-
}
24+
fn from(v: $ty) -> Self { VaArg::$variant(v as $cast) }
25+
}
26+
)*};
27+
(ptr: $($ty:ty),*) => {$(
28+
impl From<*mut $ty> for VaArg {
29+
fn from(v: *mut $ty) -> Self { VaArg::RawPtr(v as *mut c_void) }
2930
}
30-
};
31+
impl From<*const $ty> for VaArg {
32+
fn from(v: *const $ty) -> Self { VaArg::RawPtr(v as *mut c_void) }
33+
}
34+
)*};
3135
}
3236

33-
impl_from!(i32, Int);
34-
impl_from!(u32, UInt);
35-
impl_from!(i64, Long);
36-
impl_from!(u64, ULong);
37-
impl_from!(f64, Double);
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);
45-
impl_from!(AnyPtr, Ptr);
37+
impl_va_arg_from!(direct: i32 => Int, u32 => UInt, i64 => Long, u64 => ULong, f64 => Double, AnyPtr => Ptr);
38+
impl_va_arg_from!(promote: i8 => Int as i32, i16 => Int as i32, u8 => UInt as u32, u16 => UInt as u32, f32 => Double as f64);
39+
impl_va_arg_from!(ptr: c_void, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize);
4640

4741
impl<T: Clone + crate::reinterpret::ByteRepr + 'static> From<crate::rc::Ptr<T>> for VaArg {
4842
fn from(v: crate::rc::Ptr<T>) -> Self {
4943
VaArg::Ptr(v.to_any())
5044
}
5145
}
5246

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-
47+
#[derive(Clone, Copy, Default)]
6448
pub struct VaList<'a> {
6549
args: &'a [VaArg],
6650
pos: usize,
@@ -78,120 +62,59 @@ impl<'a> VaList<'a> {
7862
}
7963
}
8064

81-
impl Default for VaList<'_> {
82-
fn default() -> Self {
83-
VaList { args: &[], pos: 0 }
84-
}
85-
}
86-
87-
impl Clone for VaList<'_> {
88-
fn clone(&self) -> Self {
89-
VaList {
90-
args: self.args,
91-
pos: self.pos,
92-
}
93-
}
94-
}
95-
96-
fn get_int(v: &VaArg) -> i64 {
97-
match v {
98-
VaArg::Int(n) => *n as i64,
99-
VaArg::UInt(n) => *n as i64,
100-
VaArg::Long(n) => *n,
101-
VaArg::ULong(n) => *n as i64,
102-
_ => panic!("VaList::arg: expected integer, got different variant"),
103-
}
104-
}
105-
106-
fn get_uint(v: &VaArg) -> u64 {
107-
match v {
108-
VaArg::Int(n) => *n as u64,
109-
VaArg::UInt(n) => *n as u64,
110-
VaArg::Long(n) => *n as u64,
111-
VaArg::ULong(n) => *n,
112-
_ => panic!("VaList::arg: expected unsigned integer, got different variant"),
113-
}
114-
}
115-
116-
fn get_float(v: &VaArg) -> f64 {
117-
match v {
118-
VaArg::Double(n) => *n,
119-
VaArg::Int(n) => *n as f64,
120-
VaArg::Long(n) => *n as f64,
121-
_ => panic!("VaList::arg: expected float, got different variant"),
122-
}
123-
}
124-
125-
fn get_ptr(v: &VaArg) -> *mut c_void {
126-
match v {
127-
VaArg::RawPtr(p) => *p,
128-
_ => panic!("VaList::arg: expected pointer, got different variant"),
129-
}
130-
}
131-
13265
pub trait VaArgGet {
13366
fn get(v: &VaArg) -> Self;
13467
}
13568

136-
macro_rules! impl_get_int {
137-
($($ty:ty),*) => {
138-
$(impl VaArgGet for $ty {
139-
fn get(v: &VaArg) -> Self { get_int(v) as $ty }
140-
})*
141-
};
142-
}
143-
144-
macro_rules! impl_get_uint {
145-
($($ty:ty),*) => {
146-
$(impl VaArgGet for $ty {
147-
fn get(v: &VaArg) -> Self { get_uint(v) as $ty }
148-
})*
149-
};
150-
}
151-
152-
macro_rules! impl_get_ptr {
153-
($($ty:ty),*) => {
154-
$(impl VaArgGet for $ty {
155-
fn get(v: &VaArg) -> Self { get_ptr(v) as $ty }
156-
})*
157-
};
158-
}
159-
160-
impl_get_int!(i8, i16, i32, i64);
161-
impl_get_uint!(u8, u16, u32, u64);
162-
163-
impl VaArgGet for f32 {
164-
fn get(v: &VaArg) -> Self {
165-
get_float(v) as f32
166-
}
167-
}
168-
169-
impl VaArgGet for f64 {
170-
fn get(v: &VaArg) -> Self {
171-
get_float(v)
172-
}
173-
}
174-
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 }
69+
macro_rules! impl_va_arg_get {
70+
(int: $($ty:ty),*) => {$(
71+
impl VaArgGet for $ty {
72+
fn get(v: &VaArg) -> Self {
73+
match v {
74+
VaArg::Int(n) => *n as Self,
75+
VaArg::UInt(n) => *n as Self,
76+
VaArg::Long(n) => *n as Self,
77+
VaArg::ULong(n) => *n as Self,
78+
_ => panic!("VaArgGet: expected integer"),
79+
}
18080
}
181-
impl VaArgGet for *const $ty {
182-
fn get(v: &VaArg) -> Self { get_ptr(v) as *const $ty }
81+
}
82+
)*};
83+
(float: $($ty:ty),*) => {$(
84+
impl VaArgGet for $ty {
85+
fn get(v: &VaArg) -> Self {
86+
match v {
87+
VaArg::Double(n) => *n as Self,
88+
VaArg::Int(n) => *n as Self,
89+
VaArg::Long(n) => *n as Self,
90+
_ => panic!("VaArgGet: expected float"),
91+
}
92+
}
93+
}
94+
)*};
95+
(ptr: $($ty:ty),*) => {$(
96+
impl VaArgGet for *mut $ty {
97+
fn get(v: &VaArg) -> Self {
98+
match v { VaArg::RawPtr(p) => *p as Self, _ => panic!("VaArgGet: expected pointer") }
18399
}
184-
)*
185-
};
100+
}
101+
impl VaArgGet for *const $ty {
102+
fn get(v: &VaArg) -> Self {
103+
match v { VaArg::RawPtr(p) => *p as Self, _ => panic!("VaArgGet: expected pointer") }
104+
}
105+
}
106+
)*};
186107
}
187108

188-
impl_get_ptr!(c_void, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize);
109+
impl_va_arg_get!(int: i8, i16, i32, i64, u8, u16, u32, u64);
110+
impl_va_arg_get!(float: f32, f64);
111+
impl_va_arg_get!(ptr: c_void, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize);
189112

190113
impl<T: 'static> VaArgGet for crate::rc::Ptr<T> {
191114
fn get(v: &VaArg) -> Self {
192115
match v {
193-
VaArg::Ptr(any) => any.cast::<T>().expect("VaList::arg: Ptr type mismatch"),
194-
_ => panic!("VaList::arg: expected Ptr, got different variant"),
116+
VaArg::Ptr(any) => any.cast::<T>().expect("VaArgGet: Ptr type mismatch"),
117+
_ => panic!("VaArgGet: expected Ptr"),
195118
}
196119
}
197120
}

0 commit comments

Comments
 (0)