macro_rules! prepare { ($request:pat, $host:pat, $path:pat, $addr:pat, $(move |$($move:ident:$ty:ty),+|)? $code:block) => { ... }; }
Expand description
Construct a Prepare
extension like you write closures.
See super::PrepareCall
for a list of arguments.
The
path
will beNone
if and only ifcrate::host::Options::disable_fs
is true or percent decoding failed.request.uri().path()
will not have it’s percent encoding decoded.
See example below. Where times_called
is defined in the arguments of the macro, you can enter several Arc
s to capture from the environment.
They will be cloned before being moved to the future, mitigating the error cannot move out of 'times_called', a captured variable in an 'Fn' closure
.
Only Arc
s will work, since the variable has to be Send
and Sync
.
You have to have kvarn imported as kvarn
.
§Examples
These examples are applicable to all other extension-creation macros, but with different parameters. See their respective documentation.
use std::sync::{Arc, atomic};
let times_called = Arc::new(atomic::AtomicUsize::new(0));
prepare!(req, host, _path, _, move |times_called: Arc<atomic::AtomicUsize>| {
let times_called = times_called.fetch_add(1, atomic::Ordering::Relaxed);
println!("Called {} time(s). Request {:?}", times_called, req);
default_error_response(StatusCode::NOT_FOUND, host, None).await
});
To capture no variables, just leave out the move ||
.
prepare!(_request, host, _, _addr, {
default_error_response(StatusCode::METHOD_NOT_ALLOWED, host, None).await
});