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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#[macro_use]
extern crate quote;
extern crate proc_macro;
use self::proc_macro::TokenStream;
use syn::{Attribute, Data, DeriveInput, Fields, Ident, Type};
#[proc_macro_derive(SocketWrapper, attributes(sink, stream))]
pub fn socket_derive(input: TokenStream) -> TokenStream {
let input: DeriveInput = syn::parse(input).unwrap();
if !only_has_inner_socket(&input.data) {
panic!("Expected to derive for struct with inner: Socket");
}
let name = input.ident;
let from_sock = quote! {
impl From<Socket> for #name {
fn from(inner: Socket) -> Self {
#name {
inner,
}
}
}
};
let from_parts = quote! {
impl From<RawSocket> for #name {
fn from(inner: RawSocket) -> Self {
#name {
inner: inner.into()
}
}
}
};
let pair = if format!("{}", name).to_lowercase() == "pair" {
quote! {
impl async_zmq_types::Pair for #name {}
}
} else {
quote! {
impl async_zmq_types::UnPair for #name {}
}
};
let sub = if format!("{}", name).to_lowercase() == "sub" {
quote! {
impl async_zmq_types::Sub for #name {}
}
} else {
quote!{}
};
let kind = Ident::new(&format!("{}", name).to_uppercase(), name.span());
let as_socket = quote! {
impl crate::prelude::IntoInnerSocket for #name {
type Socket = Socket;
fn socket(self) -> Self::Socket {
self.inner
}
fn kind() -> SocketType {
#kind
}
}
};
let stream = if has_attr(&input.attrs, "stream") {
quote!{
impl crate::prelude::StreamSocket for #name {}
}
} else {
quote!{}
};
let sink = if has_attr(&input.attrs, "sink") {
quote!{
impl crate::prelude::SinkSocket for #name {}
}
} else {
quote!{}
};
let full = quote! {
#from_sock
#sub
#pair
#from_parts
#as_socket
#stream
#sink
};
full.into()
}
fn has_attr(attrs: &[Attribute], name: &str) -> bool {
attrs.iter().any(|attr| {
attr.path
.segments
.last()
.map(|seg| seg.into_value())
.map(|seg| seg.ident == Ident::new(name, seg.ident.span()))
.unwrap_or(false)
})
}
fn only_has_inner_socket(input: &Data) -> bool {
let data_struct = match *input {
Data::Struct(ref data_struct) => data_struct,
_ => return false, };
let fields_named = match data_struct.fields {
Fields::Named(ref fields_named) => fields_named,
_ => return false, };
if fields_named.named.len() != 1 {
return false;
}
let field = fields_named.named.first().unwrap().into_value();
let found = field
.ident
.as_ref()
.map(|id| *id == Ident::new("inner", id.span()))
.unwrap_or(false);
if !found {
return false;
}
let type_path = match field.ty {
Type::Path(ref type_path) => type_path,
_ => return false,
};
type_path
.path
.segments
.last()
.map(|ps| ps.into_value())
.map(|ps| ps.ident == Ident::new("Socket", ps.ident.span()))
.unwrap_or(false)
}