-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.rs
575 lines (510 loc) · 18.3 KB
/
lib.rs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#![recursion_limit = "256"]
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
extern crate syn;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::parse_macro_input;
use syn::parse_quote;
use syn::spanned::Spanned;
// ---
#[proc_macro_derive(ClonePy)]
pub fn clonepy_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as syn::DeriveInput);
match &ast.data {
syn::Data::Enum(e) => TokenStream::from(clonepy_impl_enum(&ast, &e)),
syn::Data::Struct(s) => TokenStream::from(clonepy_impl_struct(&ast, &s)),
_ => panic!("#[derive(ClonePy)] only supports enum or structs"),
}
}
fn clonepy_impl_enum(ast: &syn::DeriveInput, en: &syn::DataEnum) -> TokenStream2 {
let mut variants = Vec::new();
// Build clone_py for each variant
for variant in &en.variants {
let name = &variant.ident;
variants.push(quote!(#name(x) => #name(x.clone_py(py))));
}
// Build clone implementation
let name = &ast.ident;
let expanded = quote! {
#[automatically_derived]
#[allow(unused)]
impl ClonePy for #name {
fn clone_py(&self, py: Python) -> Self {
Python::with_gil(|py| {
use self::#name::*;
match self {
#(#variants,)*
}
})
}
}
};
expanded
}
fn clonepy_impl_struct(ast: &syn::DeriveInput, _en: &syn::DataStruct) -> TokenStream2 {
let name = &ast.ident;
let expanded = quote! {
#[automatically_derived]
impl ClonePy for #name {
fn clone_py(&self, _py: Python) -> Self {
self.clone()
}
}
};
expanded
}
// ---
// ---
#[proc_macro_derive(EqPy)]
pub fn eqpy_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as syn::DeriveInput);
match &ast.data {
syn::Data::Enum(e) => TokenStream::from(eqpy_impl_enum(&ast, &e)),
syn::Data::Struct(s) => TokenStream::from(eqpy_impl_struct(&ast, &s)),
_ => panic!("#[derive(EqPy)] only supports enums or structs"),
}
}
fn eqpy_impl_enum(ast: &syn::DeriveInput, en: &syn::DataEnum) -> TokenStream2 {
let mut variants = Vec::new();
// Build eq_py for each variant
for variant in &en.variants {
let name = &variant.ident;
variants.push(quote! {
( #name(l), #name(r) ) => (*l.bind(py).borrow()).eq_py(&*r.bind(py).borrow(), py)
});
}
// Build eq implementation
let name = &ast.ident;
let expanded = quote! {
#[automatically_derived]
#[allow(unused)]
impl EqPy for #name {
fn eq_py(&self, other: &Self, py: Python) -> bool {
use self::#name::*;
match (self, other) {
#(#variants,)*
_ => false
}
}
}
};
expanded
}
fn eqpy_impl_struct(ast: &syn::DeriveInput, en: &syn::DataStruct) -> TokenStream2 {
let mut expression: syn::Expr = parse_quote!(true);
// let fields: Vec<_> = match &en.fields {
// syn::Fields::Named(n) => n.named.iter().map(|field| field.ident.as_ref().unwrap()).collect(),
// _ => unreachable!(),
// };
if let syn::Fields::Named(n) = &en.fields {
for field in n.named.iter() {
let name = field.ident.as_ref().unwrap();
let condition = parse_quote!(self.#name.eq_py(&other.#name, py));
expression = syn::Expr::from(syn::ExprBinary {
attrs: Vec::new(),
left: Box::new(expression),
op: syn::BinOp::And(<syn::Token![&&]>::default()),
right: Box::new(condition),
})
}
} else {
unreachable!()
}
let name = &ast.ident;
let expanded = quote! {
#[automatically_derived]
impl EqPy for #name {
fn eq_py(&self, other: &Self, py: Python) -> bool {
#expression
}
}
};
expanded
}
// ---
#[proc_macro_derive(PyWrapper, attributes(wraps))]
pub fn pywrapper_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as syn::DeriveInput);
let mut output = TokenStream2::new();
if let syn::Data::Enum(e) = &ast.data {
output.extend(intopyobject_impl_enum(&ast, &e));
output.extend(frompyobject_impl_enum(&ast, &e));
output.extend(aspyptr_impl_enum(&ast, &e));
output.extend(intopy_impl_enum(&ast, &e));
} else {
panic!("only supports enums");
}
TokenStream::from(output)
}
fn aspyptr_impl_enum(ast: &syn::DeriveInput, en: &syn::DataEnum) -> TokenStream2 {
let mut variants = Vec::new();
// Build clone for each variant
for variant in &en.variants {
let name = &variant.ident;
variants.push(quote!(#name(x) => x.as_ptr()));
}
// Build clone implementation
let name = &ast.ident;
let expanded = quote! {
#[automatically_derived]
unsafe impl pyo3::AsPyPointer for #name {
fn as_ptr(&self) -> *mut pyo3::ffi::PyObject {
use self::#name::*;
match self {
#(#variants,)*
}
}
}
};
expanded
}
fn intopyobject_impl_enum(ast: &syn::DeriveInput, en: &syn::DataEnum) -> TokenStream2 {
let mut variants = Vec::new();
// extract name of base struct
let meta = &ast
.attrs
.iter()
.find(|attr| attr.path().is_ident(&syn::Ident::new("wraps", attr.span())))
.expect("could not find #[wraps] attribute")
.meta;
let base: syn::Ident = match meta {
syn::Meta::List(l) => syn::parse2(l.tokens.clone()).unwrap(),
_ => panic!("#[wraps] argument must be a class ident"),
};
// Build clone for each variant
for variant in &en.variants {
let name = &variant.ident;
variants.push(quote!(#name(x) => x.extract(py)));
}
// Build clone implementation
let name = &ast.ident;
let expanded = quote! {
#[automatically_derived]
impl<'py> pyo3::IntoPyObject<'py> for &#name {
type Error = PyErr;
type Target = #base;
type Output = Bound<'py, Self::Target>;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
use self::#name::*;
match self {
#(#variants,)*
}
}
}
#[automatically_derived]
impl<'py> pyo3::IntoPyObject<'py> for #name {
type Error = PyErr;
type Target = #base;
type Output = Bound<'py, Self::Target>;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
(&self).into_pyobject(py)
}
}
};
expanded
}
fn frompyobject_impl_enum(ast: &syn::DeriveInput, en: &syn::DataEnum) -> TokenStream2 {
let wrapped = &ast.ident;
let mut variants = Vec::new();
// Build clone for each variant
for variant in &en.variants {
// Name of the variant
let name = &variant.ident;
// Name of the class wrapped by the variant in a `Py<...>` reference.
let ty = variant.fields.iter().next().unwrap().ty.clone();
let args = match ty {
syn::Type::Path(path) => path.path.segments.iter().next().unwrap().arguments.clone(),
_ => unreachable!(),
};
let arg = match &args {
syn::PathArguments::AngleBracketed(ref br) => br.args.iter().next().unwrap(),
_ => unreachable!(),
};
let path = match &arg {
syn::GenericArgument::Type(syn::Type::Path(ref path)) => path.path.clone(),
_ => unreachable!(),
};
let lit = &syn::LitStr::new(
&path.segments.iter().next().unwrap().ident.to_string(),
path.segments.iter().next().unwrap().ident.span(),
);
variants.push(quote!(#lit => ob.extract::<pyo3::Py<#path>>().map(#wrapped::#name)));
}
// extract name of base struct
let meta = &ast
.attrs
.iter()
.find(|attr| attr.path().is_ident(&syn::Ident::new("wraps", attr.span())))
.expect("could not find #[wraps] attribute")
.meta;
let base: syn::Ident = match meta {
syn::Meta::List(l) => syn::parse2(l.tokens.clone()).unwrap(),
_ => panic!("#[wraps] argument must be a class ident"),
};
// Build FromPyObject implementation
let err_sub = syn::LitStr::new(
&format!("subclassing {} is not supported", quote!(#base)),
base.span(),
);
let err_ty = syn::LitStr::new(
&format!("expected {} instance, {{}} found", quote!(#base)),
base.span(),
);
let expanded = quote! {
#[automatically_derived]
impl<'source> pyo3::FromPyObject<'source> for #wrapped {
fn extract_bound(ob: &Bound<'source, pyo3::types::PyAny>) -> pyo3::PyResult<Self> {
use pyo3::AsPyPointer;
let qualname = ob.get_type().name()?;
let q = qualname.to_str()?;
let ty = match q.rfind('.') {
Some(idx) => &q[idx+1..],
None => &q,
};
if ob.is_instance_of::<#base>() {
match ty.as_ref() {
#(#variants,)*
_ => Err(pyo3::exceptions::PyTypeError::new_err(#err_sub))
}
} else {
Err(pyo3::exceptions::PyTypeError::new_err(format!(
#err_ty,
ob.get_type().name()?,
)))
}
}
}
};
expanded
}
fn intopy_impl_enum(ast: &syn::DeriveInput, en: &syn::DataEnum) -> TokenStream2 {
let mut variants = Vec::new();
// Build IntoPy for each variant
for variant in &en.variants {
let name = &variant.ident;
variants.push(quote!(
#name(x) => (&x.bind(py).borrow()).clone_py(py).into_py(py)
));
}
// Build clone implementation
let name = &ast.ident;
let expanded = quote! {
#[automatically_derived]
impl IntoPy<fastobo::ast::#name> for &#name {
fn into_py(self, py: Python) -> fastobo::ast::#name {
use std::ops::Deref;
use self::#name::*;
match self {
#(#variants,)*
}
}
}
};
expanded
}
// ---
#[proc_macro_attribute]
pub fn listlike(args: TokenStream, input: TokenStream) -> TokenStream {
let mut field: Option<syn::Ident> = None;
let mut ty: Option<syn::Type> = None;
let listlike_parser = syn::meta::parser(|meta| {
if meta.path.is_ident("field") {
let name: syn::LitStr = meta.value()?.parse()?;
field = Some(syn::parse_str(&name.value())?);
Ok(())
} else if meta.path.is_ident("type") {
let name: syn::LitStr = meta.value()?.parse()?;
ty = Some(syn::parse_str(&name.value())?);
Ok(())
} else {
Err(meta.error("unsupported listlike property"))
}
});
parse_macro_input!(args with listlike_parser);
// add additional methods to the impl block
let ast = parse_macro_input!(input as syn::ItemImpl);
TokenStream::from(listlike_impl_methods(&field.unwrap(), &ty.unwrap(), ast))
}
fn listlike_impl_methods(
field: &syn::Ident,
ty: &syn::Type,
mut imp: syn::ItemImpl,
) -> TokenStream2 {
imp.items.push(parse_quote! {
/// Append object to the end of the list.
///
/// Raises:
/// TypeError: when the object is not of the right type for
/// this container (see type-level documentation for the
/// required type).
#[pyo3(text_signature = "(self, object)")]
fn append<'py>(&mut self, object: &Bound<'py, PyAny>) -> PyResult<()> {
let item = <#ty as pyo3::prelude::FromPyObject>::extract_bound(object)?;
self.#field.push(item);
Ok(())
}
});
imp.items.push(parse_quote! {
/// Remove all items from list.
#[pyo3(text_signature = "(self)")]
fn clear(&mut self) {
self.#field.clear();
}
});
imp.items.push(parse_quote! {
/// Return a shallow copy of the list.
#[pyo3(text_signature = "(self)")]
fn copy(&self) -> PyResult<Py<Self>> {
Python::with_gil(|py| {
let copy = self.clone_py(py);
Py::new(py, copy)
})
}
});
imp.items.push(parse_quote! {
/// Return number of occurrences of value.
///
/// Raises:
/// TypeError: when the object is not of the right type for
/// this container (see type-level documentation for the
/// required type).
#[pyo3(text_signature = "(self, value)")]
fn count<'py>(&mut self, value: &Bound<'py, PyAny>) -> PyResult<usize> {
let py = value.py();
let item = <#ty as pyo3::prelude::FromPyObject>::extract_bound(value)?;
Ok(self.#field.iter().filter(|&x| x.eq_py(&item, py)).count())
}
});
// | extend($self, iterable, /)
// | Extend list by appending elements from the iterable.
// |
// | index(self, value, start=0, stop=9223372036854775807, /)
// | Return first index of value.
// |
// | Raises ValueError if the value is not present.
// |
imp.items.push(parse_quote! {
/// Insert `object` before `index`.
///
/// If `index` is greater than the number of elements in the list,
/// `object` will be added at the end of the list.
#[pyo3(text_signature = "(self, index, object)")]
fn insert<'py>(&mut self, mut index: isize, object: &Bound<'py, PyAny>) -> PyResult<()> {
let item = <#ty as pyo3::prelude::FromPyObject>::extract_bound(object)?;
if index >= self.#field.len() as isize {
self.#field.push(item);
} else {
if index < 0 {
index %= self.#field.len() as isize;
}
self.#field.insert(index as usize, item);
}
Ok(())
}
});
imp.items.push(parse_quote! {
/// Remove and return item at index (default last).
///
/// Raises:
/// IndexError: when list is empty or index is out of range.
#[pyo3(text_signature = "(self, index=-1)", signature=(index=-1))]
fn pop(&mut self, mut index: isize) -> PyResult<#ty> {
// Wrap once to allow negative indexing
if index < 0 {
index += self.#field.len() as isize;
}
// Pop if the index is in vector bounds
if index >= 0 && index < self.#field.len() as isize {
Ok(self.#field.remove(index as usize))
} else {
Err(pyo3::exceptions::PyIndexError::new_err("pop index out of range"))
}
}
});
// | remove(self, value, /)
// | Remove first occurrence of value.
// |
// | Raises ValueError if the value is not present.
imp.items.push(parse_quote! {
/// Reverse *IN PLACE*.
#[pyo3(text_signature = "(self)")]
fn reverse(&mut self) {
self.#field.reverse()
}
});
// | sort(self, /, *, key=None, reverse=False)
// | Stable sort *IN PLACE*.
quote!(#imp)
}
// ---
#[proc_macro_derive(FinalClass, attributes(base))]
pub fn finalclass_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as syn::DeriveInput);
match &ast.data {
syn::Data::Struct(s) => TokenStream::from(finalclass_impl_struct(&ast, &s)),
_ => panic!("#[derive(FinalClass)] only supports structs"),
}
}
fn finalclass_impl_struct(ast: &syn::DeriveInput, _st: &syn::DataStruct) -> TokenStream2 {
// Get the `base` attribute.
let meta = &ast
.attrs
.iter()
.find(|attr| attr.path().is_ident(&syn::Ident::new("base", attr.span())))
.expect("could not find #[base] attribute")
.meta;
let base: syn::Type = match meta {
syn::Meta::List(l) => syn::parse2(l.tokens.clone()).unwrap(),
_ => panic!("#[base] argument must be a class ident"),
};
// Get the name of the wrapped struct.
let name = &ast.ident;
// derive an implementation of PyClassInitializer using simply the
// default value of the base class as the initializer value
quote! {
impl FinalClass for #name {}
impl Into<pyo3::pyclass_init::PyClassInitializer<#name>> for #name {
fn into(self) -> pyo3::pyclass_init::PyClassInitializer<Self> {
<#base as AbstractClass>::initializer()
.add_subclass(self)
}
}
}
}
// ---
#[proc_macro_derive(AbstractClass, attributes(base))]
pub fn abstractclass_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as syn::DeriveInput);
match &ast.data {
syn::Data::Struct(s) => TokenStream::from(abstractclass_impl_struct(&ast, &s)),
_ => panic!("#[derive(AbstractClass)] only supports structs"),
}
}
fn abstractclass_impl_struct(ast: &syn::DeriveInput, _st: &syn::DataStruct) -> TokenStream2 {
// Get the `base` attribute.
let meta = &ast
.attrs
.iter()
.find(|attr| attr.path().is_ident(&syn::Ident::new("base", attr.span())))
.expect("could not find #[base] attribute")
.meta;
let base: syn::Type = match meta {
syn::Meta::List(l) => syn::parse2(l.tokens.clone()).unwrap(),
_ => panic!("#[base] argument must be a class ident"),
};
// Get the name of the wrapped struct.
let name = &ast.ident;
// derive an implementation of PyClassInitializer using simply the
// default value of the base class as the initializer value
quote! {
impl AbstractClass for #name {
fn initializer() -> pyo3::pyclass_init::PyClassInitializer<Self> {
<#base as AbstractClass>::initializer()
.add_subclass(Self {})
}
}
}
}