Crates.io | quasiquote |
lib.rs | quasiquote |
version | 0.1.1 |
source | src |
created_at | 2023-05-02 19:05:35.050588 |
updated_at | 2024-06-05 21:09:58.337837 |
description | quote compatible quasiquote macro with support for interpolating expressions |
homepage | https://github.com/fearless-cow/quasiquote |
repository | https://github.com/fearless-cow/quasiquote |
max_upload_size | |
id | 854829 |
size | 52,178 |
Provides a wrapper around quote! that allows interpolating arbitrary expressions.
Expected syntax is identical to what's used for quote!, except that a new interpolation pattern is allowed.
With quote:
use quote::quote;
use proc_macro2::TokenStream;
use syn::{Field, Member};
pub fn expand_getter(field: &Field) -> TokenStream {
let ident = &field.ident;
let member = Member::Named(ident.as_ref().cloned().unwrap());
let ty = &field.ty;
quote! {
pub fn #ident(&self) -> #ty {
&self.#member
}
}
}
With quasiquote:
use quasiquote::quasiquote;
use proc_macro2::TokenStream;
use syn::{Field, Member};
pub fn expand_getter(field: &Field) -> TokenStream {
let member = Member::Named(field.ident.as_ref().cloned().unwrap());
quasiquote! {
pub fn #{&field.ident}(&self) -> #{&field.ty} {
&self.#member
}
}
}