Attribute Macro axum_starter::prepare

source ·
#[prepare]
Expand description

make a function can apply as a Prepare

§Example

prepare support either sync or async function. It can generate type which implement the Prepare trait

the function arguments require can be provided by the Configure.

the return type , usually can be one of :

  • ()
  • Result<impl @, CustomError>
  • impl @

the @ can be PrepareRouteEffect, PrepareStateEffect or PrepareMiddlewareEffect

Note if the return type is Result<impl @, Error>, need add ? following the generate Name

use axum_starter_macro::prepare;
#[prepare(Foo?)]
fn prepare_foo() -> Result<(), std::io::Error>{
    // do something that might return Err()
    todo!()
}

the generate type name is present by the macro argument, for example, if you want a Prepare task named SeaConn, just using like #[prepare(SeaConn)]

if your function argument contain reference or other types witch need a lifetime, just add the lifetime to the macro arguments list, like this.

use axum_starter_macro::prepare;
#[prepare(Foo 'f)]
fn prepare_foo(foo_name: &'f String){
    // do somethings
}

Or,you can not provide any lifetime symbol, the macro will automatic find all needing lifetime places and giving a default symbol

use axum_starter_macro::prepare;
#[prepare(Foo)]
fn prepare_foo(foo_name: &String){
    // do somethings
}

Or only give lifetime symbol in macro input. The macro will auto replace '_ into 'arg if necessary

use axum_starter_macro::prepare;
#[prepare(Foo 'arg)]
fn prepare_foo(foo_name: &String){
    // do somethings
}

sometimes store Future on stack may cause Stack Overflow, you can using box before generate name make the return type became Pin<Box<dyn Future>>

use axum_starter_macro::prepare;
#[prepare(box Foo)]
async fn prepare_foo(){
    // do something may take place large space
}

if you want a Prepare return Ready, or in other word, a sync Prepare,you can use sync before the Ident. note that box and sync cannot use in the same time

use axum_starter_macro::prepare;
#[prepare(sync Foo)]
fn prepare_foo(){
    // do something not using `await`
}

By default, the macro will not keep the origin function exist, if you want use that original function, using origin, the origin is after the box or sync, but before the Ident

 use axum_starter_macro::prepare;
 #[prepare(sync origin Foo)]
 fn prepare_foo(){
     // do something not using `await`
 }