Struct telety_impl::Command
source · pub struct Command { /* private fields */ }Expand description
Used to invoke the telety-generated macro in a manageable way.
Implementations§
source§impl Command
impl Command
sourcepub fn apply(
&self,
macro_path: &Path,
needle: impl Into<SingleToken>,
haystack: impl ToTokens,
args: Option<TokenStream>
) -> ItemMacro
pub fn apply( &self, macro_path: &Path, needle: impl Into<SingleToken>, haystack: impl ToTokens, args: Option<TokenStream> ) -> ItemMacro
Creates a macro invocation to use this command with the telety-generated macro at macro_path.
The output of the command will be inserted into haystack at each instance of needle.
macro_path must point to a valid telety-generated macro, otherwise a compile error will occur.
To support future Commands, args are passed to the command invocation, but they are not currently used.
§Example
#[proc_macro]
pub fn my_public_macro(tokens: TokenStream) -> TokenStream {
// ...
let my_needle: TokenTree = format_ident!("__my_needle__").into();
v1::UNIQUE_IDENT.apply(
&parse_quote!(crate::MyTeletyObj),
&my_needle,
quote! {
my_crate::my_macro_implementation!(#my_needle);
},
None,
)
}
#[doc(hidden)]
#[proc_macro]
pub fn my_macro_implementation(tokens: TokenStream) -> TokenStream {
let ident: Ident = parse2(tokens);
// ...
}sourcepub fn apply_or(
&self,
macro_path: &Path,
needle: impl Into<SingleToken>,
haystack: impl ToTokens,
args: Option<TokenStream>,
macro_fallback: &Path
) -> ItemMacro
pub fn apply_or( &self, macro_path: &Path, needle: impl Into<SingleToken>, haystack: impl ToTokens, args: Option<TokenStream>, macro_fallback: &Path ) -> ItemMacro
Similar to Command::apply, except if macro_path does not include a macro, macro_fallback will be used instead.
Note that macro_path must still be valid path of some sort (i.e. a type or value), otherwise compilation will fail.
Additionally, for name resolution to succeed, macro_path must start with a qualifier (e.g. ::, self::, crate::, …).
If you see the error “import resolution is stuck, try simplifying macro imports”, you are probably missing the qualifier.
Finally, the output will be placed inside a block, which means any items defined inside cannot be easily referenced elsewhere.
The primary use-case is to create impls.
sourcepub fn apply_exported_or_noop(
&self,
macro_path: &Path,
needle: impl Into<SingleToken>,
haystack: impl ToTokens,
args: Option<TokenStream>,
unique_macro_ident: &Ident
) -> Vec<Item>
pub fn apply_exported_or_noop( &self, macro_path: &Path, needle: impl Into<SingleToken>, haystack: impl ToTokens, args: Option<TokenStream>, unique_macro_ident: &Ident ) -> Vec<Item>
Similar to Command::apply_or, except haystack is expanded in the current module or block if macro_path is a macro,
otherwise, a noop macro is expanded instead.
haystack is forwarded through a macro_rules! macro, but $ tokens within haystack will be
automatically converted to work within the macro.
unique_macro_ident must be an identifier unique to the crate, as the forwarding macro must be #[macro_export].