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
use ::macroix::{
repr::{BaseKind, Int, PrimitiveType, Repr},
visit_fields,
AttrValue,
};
use ::proc_macro2::{Span, TokenStream};
use ::quote::quote;
use ::syn::{parse2, parse_quote, Data, DeriveInput, Error, Path};
pub fn derive(mut input: DeriveInput) -> Result<TokenStream, Error> {
let mut repr = None;
let mut rel_core = None;
for attr in input.attrs.iter() {
if attr.path.is_ident("repr") {
Repr::merge_attr(&mut repr, attr.tokens.clone())?;
} else if attr.path.is_ident("rel_core") {
rel_core =
Some(parse2::<AttrValue<Path>>(attr.tokens.clone())?.value);
}
}
let rel_core = rel_core.unwrap_or_else(|| parse_quote! { ::rel_core });
let repr = repr.ok_or_else(|| {
Error::new(
Span::call_site(),
"`Portable` types require an explicit `repr` attribute",
)
})?;
let repr_base = repr.base.ok_or_else(|| {
Error::new(
Span::call_site(),
"`Portable` types require a base `repr` kind",
)
})?;
match input.data {
Data::Struct(_) => {
if !matches!(repr_base.kind, BaseKind::C | BaseKind::Transparent) {
return Err(Error::new_spanned(
repr_base.kind_token,
"`Portable` structs must be `repr(C)` or \
`repr(transparent)`",
));
}
}
Data::Enum(_) => match repr_base.kind {
BaseKind::Primitive(Int::I8 | Int::U8) => (),
BaseKind::C => match repr.primitive_type {
Some(PrimitiveType {
int: Int::I8 | Int::U8,
..
}) => (),
Some(PrimitiveType { int_token, .. }) => {
return Err(Error::new_spanned(
int_token,
"`Portable` enums that are `repr(C)` must have a \
primitive type of `i8` or `u8`",
))
}
None => {
return Err(Error::new_spanned(
repr_base.kind_token,
"`Portable` enums that are `repr(C)` must specify \
a primitive type with `repr(C, i8)` or \
`repr(C, u8)`",
))
}
},
_ => {
return Err(Error::new_spanned(
repr_base.kind_token,
"`Portable` enums must be `repr(i8)`, `repr(u8)`, \
`repr(C, i8)`, or `repr(C, u8)`",
));
}
},
Data::Union(_) => {
if !matches!(repr_base.kind, BaseKind::C | BaseKind::Transparent) {
return Err(Error::new_spanned(
repr_base.kind_token,
"`Portable` unions must be `repr(C)` or \
`repr(transparent)`",
));
}
}
}
let where_clause = input.generics.make_where_clause();
visit_fields(&input.data, |f| {
let ty = &f.ty;
where_clause
.predicates
.push(parse_quote! { #ty: #rel_core::Portable });
});
let (impl_generics, ty_generics, where_clause) =
input.generics.split_for_impl();
let ty_name = &input.ident;
Ok(quote! {
unsafe impl #impl_generics #rel_core::Portable
for #ty_name #ty_generics #where_clause {}
})
}