Struct WrappedFn

Source
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: ItemFn

syn::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: Ident

Identifier token for the closure that wraps all of the original code from the wrapped function. wrapper by default.

§result_ident: Ident

Identifier token for the variable that holds the return value of the wrapped function. result by default.

Implementations§

Source§

impl WrappedFn

Source

pub fn set_pre_code(&mut self, pre_code: TokenStream)

Sets the code that gets run before the rest of the function executes.

Source

pub fn set_post_code(&mut self, post_code: TokenStream)

Sets the code that gets run after the rest of the function executes.

Source

pub fn remove_pre_code(&mut self)

Removes any code that was going to be added before the rest of the function.

Source

pub fn remove_post_code(&mut self)

Removes any code that was going to be added after the rest of the function.

Source

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).

Source

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 Clone for WrappedFn

Source§

fn clone(&self) -> WrappedFn

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WrappedFn

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&WrappedFn> for ItemFn

Allows WrappedFns to be converted to syn::ItemFns for easier use in procedural macros.

Source§

fn from(function: &WrappedFn) -> Self

Converts a WrappedFn into a syn::ItemFn.

Source§

impl From<&WrappedFn> for TokenStream

Allows WrappedFns to be converted into proc_macro2::TokenStreams for easier use in procedural attribute macros.

Source§

fn from(function: &WrappedFn) -> Self

Converts a WrappedFn into a proc_macro2::TokenStream.

Source§

impl From<WrappedFn> for ItemFn

Allows WrappedFns to be converted to syn::ItemFns for easier use in procedural macros.

Source§

fn from(function: WrappedFn) -> Self

Converts a WrappedFn into a syn::ItemFn.

Source§

impl From<WrappedFn> for TokenStream

Allows WrappedFns to be converted into proc_macro2::TokenStreams for easier use in procedural attribute macros.

Source§

fn from(function: WrappedFn) -> Self

Converts a WrappedFn into a proc_macro2::TokenStream.

Source§

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>

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.

Source§

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

Convert self directly into a TokenStream object. Read more
Source§

fn into_token_stream(self) -> TokenStream
where Self: Sized,

Convert self directly into a TokenStream object. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Spanned for T
where T: Spanned + ?Sized,

Source§

fn span(&self) -> Span

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.