|
| 1 | +--- |
| 2 | +title: Runtime |
| 3 | +layout: guide |
| 4 | +permalink: /guides/1/init/runtime/ |
| 5 | +--- |
| 6 | + |
| 7 | +With hyper v1.0 removing `tokio` as runtime dependency, a new runtime trait `hyper::rt` is introduced. If you still want to use `tokio`, a `tokio` implementation for `hyper::rt` is provided by `hyper-util` crate. |
| 8 | + |
| 9 | +## Building your own `hyper::rt` implementations with `Tokio` |
| 10 | + |
| 11 | +Let's build a simple `hyper::rt` implementations with the help of tokio. First, make sure you have `tokio` as a dependency in your `Cargo.toml`: |
| 12 | + |
| 13 | +```toml |
| 14 | +[dependencies] |
| 15 | +tokio = { version = "1", features = ["full"] } |
| 16 | +``` |
| 17 | + |
| 18 | +Now, let's try to write a simple executor `hyper::rt::Executor`, we'll call it `TokioExecutor`: |
| 19 | + |
| 20 | +```rust |
| 21 | +# extern crate hyper; |
| 22 | +/// Future executor that utilises `tokio` threads. |
| 23 | +#[non_exhaustive] |
| 24 | +#[derive(Default, Debug, Clone)] |
| 25 | +pub struct TokioExecutor {} |
| 26 | +``` |
| 27 | + |
| 28 | +the trait `hyper::rt::Executor` expects an `execute` method that asks the runtime to execute futures. `tokio` allows this easily with `tokio::spawn` |
| 29 | + |
| 30 | +```rust |
| 31 | +# extern crate hyper; |
| 32 | +# extern crate tokio; |
| 33 | + |
| 34 | +use std::future::Future; |
| 35 | + |
| 36 | +use hyper::rt::Executor; |
| 37 | + |
| 38 | +# #[non_exhaustive] |
| 39 | +# #[derive(Default, Debug, Clone)] |
| 40 | +# pub struct TokioExecutor {} |
| 41 | + |
| 42 | +impl<Fut> Executor<Fut> for TokioExecutor |
| 43 | +where |
| 44 | + Fut: Future + Send + 'static, |
| 45 | + Fut::Output: Send + 'static, |
| 46 | +{ |
| 47 | + fn execute(&self, fut: Fut) { |
| 48 | + tokio::spawn(fut); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +We now have a working `hyper::rt::Executor` with Tokio, and is ready to be supplied to anything that requires `Executor`. For example, with the auto connection from hyper-util: |
| 54 | + |
| 55 | +```rust |
| 56 | +# extern crate hyper; |
| 57 | +# extern crate hyper_util; |
| 58 | +# extern crate tokio; |
| 59 | + |
| 60 | +use std::future::Future; |
| 61 | + |
| 62 | +use hyper_util::server::conn::auto; |
| 63 | +use hyper::rt::Executor; |
| 64 | + |
| 65 | +# #[non_exhaustive] |
| 66 | +# #[derive(Default, Debug, Clone)] |
| 67 | +# pub struct TokioExecutor {} |
| 68 | + |
| 69 | +# impl<Fut> Executor<Fut> for TokioExecutor |
| 70 | +# where |
| 71 | +# Fut: Future + Send + 'static, |
| 72 | +# Fut::Output: Send + 'static, |
| 73 | +# { |
| 74 | +# fn execute(&self, fut: Fut) { |
| 75 | +# tokio::spawn(fut); |
| 76 | +# } |
| 77 | +# } |
| 78 | + |
| 79 | +impl TokioExecutor { |
| 80 | + pub fn new() -> Self { |
| 81 | + Self {} |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +auto::Builder::new(TokioExecutor::new()); |
| 86 | +``` |
| 87 | + |
| 88 | +## Using `hyper::rt` implementations with tokio in hyper-util |
| 89 | + |
| 90 | +The crate `hyper-util` provides implementations for `hyper::rt` traits with Tokio. To use, we'll need to have `hyper-util` as a dependency. |
| 91 | + |
| 92 | +```toml |
| 93 | +[dependencies] |
| 94 | +hyper-util = { version = "0.1", features = ["full"] } |
| 95 | +``` |
| 96 | + |
| 97 | +Then you'll simply need to import and use it as in the example above |
| 98 | + |
| 99 | +```rust |
| 100 | +# extern crate hyper; |
| 101 | +# extern crate hyper_util; |
| 102 | + |
| 103 | +use hyper::rt::Executor; |
| 104 | +use hyper_util::rt::TokioExecutor; |
| 105 | +use hyper_util::server::conn::auto; |
| 106 | + |
| 107 | +auto::Builder::new(TokioExecutor::new()); |
| 108 | +``` |
| 109 | + |
| 110 | +There are more implementations in the hyper_util crate. Check out the docs on [`hyper_util::rt`][] for more details. |
| 111 | + |
| 112 | +[`hyper_util::rt`]: https://docs.rs/hyper-util/latest/hyper_util/rt/index.html |
0 commit comments