pub struct WrappedFn {
pub function: ItemFn,
pub pre_code: Option<TokenStream>,
pub post_code: Option<TokenStream>,
pub wrapper_ident: Ident,
pub result_ident: Ident,
}Expand description
Function that can have code inserted before and after the rest of the function executes.
Can be constructed with syn::parse() and other variations of parsing from the syn crate.
Example:
let mut function = parse_macro_input!(token_stream as WrappedFn);Code that runs before the function can be set using the set_pre_code() method.
Example:
function.set_pre_code(quote!{ println!("Hi at the start :)"); });Code that runs after the function can be set using the set_post_code() method.
Example:
function.set_post_code(quote!{ println!("Hi at the end :)"); });Fields§
§function: ItemFnsyn::ItemFn that contains all of the data of the original function, including the code inside, the function signature, any attributes, etc.
pre_code: Option<TokenStream>Contains code that gets run before the rest of the function.
post_code: Option<TokenStream>Contains code that gets run after the rest of the function.
wrapper_ident: IdentIdentifier token for the closure that wraps all of the original code from the wrapped function. wrapper by default.
result_ident: IdentIdentifier token for the variable that holds the return value of the wrapped function. result by default.
Implementations§
Source§impl WrappedFn
impl WrappedFn
Sourcepub fn set_pre_code(&mut self, pre_code: TokenStream)
pub fn set_pre_code(&mut self, pre_code: TokenStream)
Sets the code that gets run before the rest of the function executes.
Sourcepub fn set_post_code(&mut self, post_code: TokenStream)
pub fn set_post_code(&mut self, post_code: TokenStream)
Sets the code that gets run after the rest of the function executes.
Sourcepub fn remove_pre_code(&mut self)
pub fn remove_pre_code(&mut self)
Removes any code that was going to be added before the rest of the function.
Sourcepub fn remove_post_code(&mut self)
pub fn remove_post_code(&mut self)
Removes any code that was going to be added after the rest of the function.
Sourcepub fn set_wrapper_ident(&mut self, ident: &str)
pub fn set_wrapper_ident(&mut self, ident: &str)
Changes the identifier for the closure that wraps the code of the original function (wrapper by default).
Sourcepub fn set_result_ident(&mut self, ident: &str)
pub fn set_result_ident(&mut self, ident: &str)
Changes the identifier for the variable that holds the value that the function returns (result by default).
Trait Implementations§
Source§impl From<&WrappedFn> for ItemFn
Allows WrappedFns to be converted to syn::ItemFns for easier use in procedural macros.
impl From<&WrappedFn> for ItemFn
Allows WrappedFns to be converted to syn::ItemFns for easier use in procedural macros.
Source§impl From<&WrappedFn> for TokenStream
Allows WrappedFns to be converted into proc_macro2::TokenStreams for easier use in procedural attribute macros.
impl From<&WrappedFn> for TokenStream
Allows WrappedFns to be converted into proc_macro2::TokenStreams for easier use in procedural attribute macros.
Source§impl From<WrappedFn> for ItemFn
Allows WrappedFns to be converted to syn::ItemFns for easier use in procedural macros.
impl From<WrappedFn> for ItemFn
Allows WrappedFns to be converted to syn::ItemFns for easier use in procedural macros.
Source§impl From<WrappedFn> for TokenStream
Allows WrappedFns to be converted into proc_macro2::TokenStreams for easier use in procedural attribute macros.
impl From<WrappedFn> for TokenStream
Allows WrappedFns to be converted into proc_macro2::TokenStreams for easier use in procedural attribute macros.
Source§impl Parse for WrappedFn
Main way to construct a WrappedFn.
Can be constructed using syn::parse_macro_input like this:
impl Parse for WrappedFn
Main way to construct a WrappedFn.
Can be constructed using syn::parse_macro_input like this:
let mut function = parse_macro_input!(token_stream as WrappedFn);Source§fn parse(input: ParseStream<'_>) -> Result<Self>
fn parse(input: ParseStream<'_>) -> Result<Self>
Constructs a WrappedFn from a syn::ParseStream.
Source§impl ToTokens for WrappedFn
Allows WrappedFns to be easily appended to and converted to proc_macro2::TokenStreams.
impl ToTokens for WrappedFn
Allows WrappedFns to be easily appended to and converted to proc_macro2::TokenStreams.
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
Adds a WrappedFn to the end of a proc_macro2::TokenStream.
Source§fn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
Source§fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
Auto Trait Implementations§
impl !Send for WrappedFn
impl !Sync for WrappedFn
impl Freeze for WrappedFn
impl RefUnwindSafe for WrappedFn
impl Unpin for WrappedFn
impl UnsafeUnpin for WrappedFn
impl UnwindSafe for WrappedFn
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Spanned for Twhere
T: Spanned + ?Sized,
impl<T> Spanned for Twhere
T: Spanned + ?Sized,
Source§fn span(&self) -> Span
fn span(&self) -> Span
Span covering the complete contents of this syntax tree
node, or Span::call_site() if this node is empty.