mirror of
https://github.com/latex-lsp/texlab.git
synced 2025-08-04 18:58:31 +00:00
Refactor TeX distribution wrapper
This commit is contained in:
parent
402478c4a7
commit
77d5b2069f
12 changed files with 754 additions and 2 deletions
14
crates/futures_boxed/Cargo.toml
Normal file
14
crates/futures_boxed/Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "futures-boxed"
|
||||
version = "0.1.0"
|
||||
authors = [
|
||||
"Eric Förster <efoerster@users.noreply.github.com>",
|
||||
"Patrick Förster <pfoerster@users.noreply.github.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = "1.0"
|
||||
quote = "1.0"
|
67
crates/futures_boxed/src/lib.rs
Normal file
67
crates/futures_boxed/src/lib.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
#![recursion_limit = "128"]
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro::{TokenStream, TokenTree};
|
||||
use quote::{quote, ToTokens};
|
||||
use std::iter::FromIterator;
|
||||
use syn::{export::TokenStream2, *};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn boxed(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
match parse::<ItemFn>(item.clone()) {
|
||||
Ok(fn_) => boxed_fn(fn_),
|
||||
Err(_) => {
|
||||
let item = TokenStream::from_iter(item.into_iter().filter(|x| match x {
|
||||
TokenTree::Ident(x) if x.to_string() == "async" => false,
|
||||
_ => true,
|
||||
}));
|
||||
|
||||
let method: TraitItemMethod = parse(item).unwrap();
|
||||
boxed_trait_method(method)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn boxed_fn(fn_: ItemFn) -> TokenStream {
|
||||
let attrs = &fn_.attrs;
|
||||
let vis = &fn_.vis;
|
||||
let sig = boxed_fn_sig(&fn_.sig);
|
||||
let block = &fn_.block;
|
||||
let tokens = quote! {
|
||||
#(#attrs)*
|
||||
#vis #sig {
|
||||
use futures::future::FutureExt;
|
||||
let task = async move #block;
|
||||
task.boxed()
|
||||
}
|
||||
};
|
||||
|
||||
tokens.into()
|
||||
}
|
||||
|
||||
fn boxed_trait_method(method: TraitItemMethod) -> TokenStream {
|
||||
let attrs = &method.attrs;
|
||||
let sig = boxed_fn_sig(&method.sig);
|
||||
let tokens = quote! {
|
||||
#(#attrs)*
|
||||
#sig;
|
||||
};
|
||||
|
||||
tokens.into()
|
||||
}
|
||||
|
||||
fn boxed_fn_sig(sig: &Signature) -> TokenStream2 {
|
||||
let constness = &sig.constness;
|
||||
let ident = &sig.ident;
|
||||
let generics = &sig.generics;
|
||||
let inputs = &sig.inputs;
|
||||
let return_ty = match &sig.output {
|
||||
ReturnType::Default => quote!(()),
|
||||
ReturnType::Type(_, ty) => ty.into_token_stream(),
|
||||
};
|
||||
|
||||
quote! {
|
||||
#constness fn #ident #generics(#inputs) -> futures::future::BoxFuture<'_, #return_ty>
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue