Struct function_wrapper::WrappedFn

source ·
pub struct WrappedFn {
    pub pre_code: Option<TokenStream>,
    pub function: ItemFn,
    pub wrapper_ident: Ident,
    pub result_ident: Ident,
    pub post_code: Option<TokenStream>,
}
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§

§pre_code: Option<TokenStream>

Contains code that gets run before the rest of the function.

§function: ItemFn

syn::ItemFn that contains all of the data of the original function, including the code inside, the function signature, any attributes, etc.

§wrapper_ident: Ident

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

§result_ident: Ident

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

§post_code: Option<TokenStream>

Contains code that gets run after the rest of the function.

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.

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 From<WrappedFn> for TokenStream

Allows WrappedFns to be converted into tokenstreams for easy 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.

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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.