Macro anterofit::with_builder[][src]

macro_rules! with_builder {
    (|$builder:ident| $expr:expr) => { ... };
    (move |$builder:ident| $expr:expr) => { ... };
    (|mut $builder:ident| $expr:expr) => { ... };
    (move |mut $builder:ident| $expr:expr) => { ... };
}

Use in a service body to access the builder without consuming it.

The expression can resolve to anything, as the result is silently discarded.

service! {
    trait MyService {
        fn get_whatever(&self) {
            GET("/whatever");
            with_builder!(|builder| println!("Request: {:?}", builder.head()))
        }
    }
}

You can even use try!() as long as the error type is convertible to anterofit::Error:

use std::fs::OpenOptions;
// Required for `write!()`
use std::io::Write;

service! {
    trait MyService {
        fn get_whatever(&self) {
            GET("/whatever");
            with_builder!(|builder| {
                let mut logfile = try!(OpenOptions::new()
                    .append(true).create(true).open("/etc/log"));
                try!(write!(logfile, "Request: {:?}", builder.head()));
            })
        }
    }
}

(In practice, logging requests should probably be done in an Interceptor instead; this is merely an example demonstrating a plausible use-case.)