January 2019
Intermediate to advanced
520 pages
14h 32m
English
To get access to a shared state, you need to provide a reference to the shared data. This is simple, because we've already wrapped our state with Arc, which provides us with a clone() function to duplicate the reference to the shared object.
Since our service function needs extra parameters, we have to rewrite the definition and call our microservice_handler function. Now it has an extra argument, which is the reference to the shared state:
fn microservice_handler(req: Request<Body>, user_db: &UserDb) -> impl Future<Item=Response<Body>, Error=Error>
We also have to send this expected reference to the main function:
fn main() { let addr = ([127, 0, 0, 1], 8080).into(); let builder = Server::bind(&addr); ...Read now
Unlock full access