sortable-macro: remove need for 'identity' macro

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-11-24 09:14:38 +01:00
parent cdf8220676
commit be7b330d8f
2 changed files with 9 additions and 22 deletions

View File

@ -12,7 +12,6 @@ use quote::quote;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::visit_mut::VisitMut;
use syn::Ident;
macro_rules! format_err {
($span:expr => $($msg:tt)*) => { syn::Error::new_spanned($span, format!($($msg)*)) };
@ -47,20 +46,16 @@ pub fn sortable(_attr: TokenStream_1, item: TokenStream_1) -> TokenStream_1 {
struct SortedData;
impl VisitMut for SortedData {
fn visit_expr_macro_mut(&mut self, i: &mut syn::ExprMacro) {
if i.mac.path.is_ident("sorted") {
let span = i.mac.path.span();
i.mac.path.segments = Punctuated::new();
i.mac.path.segments.push(syn::PathSegment {
ident: Ident::new("identity", span),
arguments: Default::default(),
});
let tokens = mem::replace(&mut i.mac.tokens, TokenStream::new());
i.mac.tokens = handle_error(tokens.clone(), sort_data(tokens));
fn visit_expr_mut(&mut self, expr: &mut syn::Expr) {
match expr {
syn::Expr::Macro(i) => {
if i.mac.path.is_ident("sorted") {
let tokens = mem::replace(&mut i.mac.tokens, TokenStream::new());
*expr = syn::Expr::Verbatim(handle_error(tokens.clone(), sort_data(tokens)));
}
}
_ => syn::visit_mut::visit_expr_mut(self, expr),
}
// and recurse:
self.visit_macro_mut(&mut i.mac)
}
}

View File

@ -1,13 +1,5 @@
use proxmox_sortable_macro::sortable;
// The way #[sorted] works we require an 'identity' macro due to the inability of the syntax tree
// visitor to change the type of a syntax tree element.
//
// Iow.: it replaces `sorted!([3, 2, 1])` with `identity!([1, 2, 3])`.
macro_rules! identity {
($($x:tt)*) => { $($x)* }
}
// In a normal project we would use this Cargo.toml line:
//
// [dependencies]