1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use core::fmt;
use {Encoding, Encodings};
use encoding::Primitive;
#[derive(Debug)]
pub enum Descriptor<'a,
T: 'a + ?Sized,
I: 'a + ?Sized,
F: 'a + ?Sized,
M: 'a + ?Sized> {
Primitive(Primitive),
Pointer(&'a T),
Array(u32, &'a I),
Struct(&'a str, &'a F),
Union(&'a str, &'a M),
}
impl<'a,
T: 'a + ?Sized,
I: 'a + ?Sized,
F: 'a + ?Sized,
M: 'a + ?Sized>
Copy for Descriptor<'a, T, I, F, M> { }
impl<'a,
T: 'a + ?Sized,
I: 'a + ?Sized,
F: 'a + ?Sized,
M: 'a + ?Sized>
Clone for Descriptor<'a, T, I, F, M> {
fn clone(&self) -> Descriptor<'a, T, I, F, M> {
*self
}
}
pub fn encodings_eq<T: ?Sized, U: ?Sized>(e1: &T, e2: &U) -> bool
where T: Encoding, U: Encoding {
use Descriptor::*;
match (e1.descriptor(), e2.descriptor()) {
(Primitive(p1), Primitive(p2)) => primitives_eq(p1, p2),
(Pointer(t1), Pointer(t2)) => t1.eq_encoding(t2),
(Array(l1, i1), Array(l2, i2)) =>
l1 == l2 && i1.eq_encoding(i2),
(Struct(n1, f1), Struct(n2, f2)) =>
n1 == n2 && f1.eq_encodings(f2),
(Union(n1, m1), Union(n2, m2)) =>
n1 == n2 && m1.eq_encodings(m2),
_ => false,
}
}
fn primitives_eq(p1: Primitive, p2: Primitive) -> bool {
use encoding::Primitive::*;
match (p1, p2) {
(Char , Char ) => true,
(Short , Short ) => true,
(Int , Int ) => true,
(Long , Long ) => true,
(LongLong , LongLong ) => true,
(UChar , UChar ) => true,
(UShort , UShort ) => true,
(UInt , UInt ) => true,
(ULong , ULong ) => true,
(ULongLong, ULongLong) => true,
(Float , Float ) => true,
(Double , Double ) => true,
(Bool , Bool ) => true,
(Void , Void ) => true,
(String , String ) => true,
(Object , Object ) => true,
(Block , Block ) => true,
(Class , Class ) => true,
(Sel , Sel ) => true,
(Unknown , Unknown ) => true,
(BitField(b1), BitField(b2)) => b1 == b2,
_ => false,
}
}
pub fn write_encoding<W, T: ?Sized>(writer: &mut W, encoding: &T) -> fmt::Result
where W: fmt::Write, T: Encoding {
use Descriptor::*;
match encoding.descriptor() {
Primitive(p) => write_primitive(writer, p),
Pointer(t) => {
try!(writer.write_char('^'));
t.write(writer)
}
Array(len, item) => {
try!(write!(writer, "[{}", len));
try!(item.write(writer));
writer.write_char(']')
}
Struct(name, fields) => {
try!(write!(writer, "{{{}=", name));
try!(fields.write_all(writer));
writer.write_char('}')
}
Union(name, members) => {
try!(write!(writer, "({}=", name));
try!(members.write_all(writer));
writer.write_char(')')
}
}
}
fn write_primitive<W: fmt::Write>(writer: &mut W, p: Primitive) -> fmt::Result {
use encoding::Primitive::*;
let code = match p {
Char => "c",
Short => "s",
Int => "i",
Long => "l",
LongLong => "q",
UChar => "C",
UShort => "S",
UInt => "I",
ULong => "L",
ULongLong => "Q",
Float => "f",
Double => "d",
Bool => "B",
Void => "v",
String => "*",
Object => "@",
Block => "@?",
Class => "#",
Sel => ":",
Unknown => "?",
BitField(b) => {
return write!(writer, "b{}", b);
}
};
writer.write_str(code)
}