tarpc::server::request_hook

Function before

Source
pub fn before() -> BeforeRequestNil
Expand description

Returns a request hook builder that runs a series of hooks before request execution.

Example

use futures::{executor::block_on, future};
use tarpc::{context, ServerError, server::{Serve, serve, request_hook::{self,
            BeforeRequest, BeforeRequestList}}};
use std::{cell::Cell, io};

let i = Cell::new(0);
let serve = request_hook::before()
    .then_fn(|_, _| async {
        assert!(i.get() == 0);
        i.set(1);
        Ok(())
    })
    .then_fn(|_, _| async {
        assert!(i.get() == 1);
        i.set(2);
        Ok(())
    })
    .serving(serve(|_ctx, i| async move { Ok(i + 1) }));
let response = serve.clone().serve(context::current(), 1);
assert!(block_on(response).is_ok());
assert!(i.get() == 2);