You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some thing like @decorator in python or #[derive()] for fn
structUser{/* ... */}implFromRequestforUser{// ...}#[decorate(HttpHandler)]#[http(get("/"))]fnindex(user:User) -> String{format!("hello, {}", user.name)}#[decorate(HttpHandler)]#[http(get("/users/<id>"))]fnget(user:User,id:String) -> Result<String,Status>{if user.id != id {Err(Status::Forbidden)}else{Ok(format!("name: {}, last login at: {}", user.name, user.last_login_time))}}fnmain(){HttpServer::new().mount("/",vec![index, get]).launch().expect("Failed to start server");}
For now, this can be done by syntax plugin, but it will far from landing in stable (perhaps never).
See: https://github.com/SergioBenitez/Rocket (the example above is almost same as Rocket's example)
So maybe it can be done via same mechanism as derive in proc macro:
The codegen crate will receive the signature of fn and output a new decorated fnand a inner function name , the inner function name will replace the old one. for example:
and parse (via syn as current proc macro codegen crates) and produce (via quote) :
fnget(request:Request) -> Result<Response,Status>{let user = User::from_request(&request)?;let params = request.parse_parms("/user/<id>")?;let id = params.get(1)?;let body = __http_inner_get(user, id)?;Ok(Response::build().header("Content-Type","text/plain").sized_body(Cursor::new(body)).ok())}
and a inner function name for original function: '__http_inner_get'.
And then rustc will replace the original function's name with the given one. This is the only difference with deriving a struct/enum of current proc macro mechanism.
It is like python's decorator:
defrequire_user(request):
returndb.load_user_by_session(request.cookie.get("session"))
@http.get("/users/<id:str>", from_request= [require_user])defget(user, id):
# do somethingreturn"hello %s"%user.name
The semantic of decorator is: function = decorator(old_function)
If multi decorator is used, then pipe them one by one: function = decorator1(decorator2(old_function))
The text was updated successfully, but these errors were encountered:
There are actually three types of procedural macros planned. One of them is proc_macro_attribute, which will be similar to a proc_macro_derive, except unlike derive, you get to modify the thing you call it on. This is a replacement for the old syntax extensions, and is available in nightly in a raw form (the TokenStream api is being redesigned so it's not just strings).
You can also stack them:
#[first_macro]
#[second_macro("you get this as input too!")]
fn foo() { ... }
Some thing like
@decorator
in python or#[derive()]
forfn
For now, this can be done by syntax plugin, but it will far from landing in stable (perhaps never).
See: https://github.com/SergioBenitez/Rocket (the example above is almost same as Rocket's example)
So maybe it can be done via same mechanism as
derive
in proc macro:The codegen crate will receive the signature of
fn
and output a new decoratedfn
and a inner function name , the inner function name will replace the old one. for example:codegen crate will receive
and parse (via
syn
as current proc macro codegen crates) and produce (viaquote
) :and a inner function name for original function: '__http_inner_get'.
And then rustc will replace the original function's name with the given one. This is the only difference with deriving a struct/enum of current proc macro mechanism.
It is like python's decorator:
The semantic of decorator is:
function = decorator(old_function)
If multi decorator is used, then pipe them one by one:
function = decorator1(decorator2(old_function))
The text was updated successfully, but these errors were encountered: