Skip to content

Commit

Permalink
docs(service): change the make_service_fn example to a full snippet
Browse files Browse the repository at this point in the history
Include the creation of server too. Previously, the parameter type of
the closure had a different type than the default server provided, which
wasn't obvious to the user.
  • Loading branch information
vorner authored and seanmonstar committed Mar 1, 2019
1 parent cbae429 commit 2114950
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/service/make_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,33 @@ where
///
/// # Example
///
/// ```rust
/// ```rust,no_run
/// # #[cfg(feature = "runtime")] fn main() {
/// use std::net::TcpStream;
/// use hyper::{Body, Request, Response};
/// use hyper::{Body, Request, Response, Server};
/// use hyper::rt::{self, Future};
/// use hyper::server::conn::AddrStream;
/// use hyper::service::{make_service_fn, service_fn_ok};
///
/// let make_svc = make_service_fn(|socket: &TcpStream| {
/// let remote_addr = socket.peer_addr().unwrap();
/// let addr = ([127, 0, 0, 1], 3000).into();
///
/// let make_svc = make_service_fn(|socket: &AddrStream| {
/// let remote_addr = socket.remote_addr();
/// service_fn_ok(move |_: Request<Body>| {
/// Response::new(Body::from(format!("Hello, {}", remote_addr)))
/// })
/// });
///
/// // Then bind and serve...
/// let server = Server::bind(&addr)
/// .serve(make_svc);
///
/// // Finally, spawn `server` onto an Executor...
/// rt::run(server.map_err(|e| {
/// eprintln!("server error: {}", e);
/// }));
/// # }
/// # #[cfg(not(feature = "runtime"))] fn main() {}
/// ```
pub fn make_service_fn<F, Ctx, Ret>(f: F) -> MakeServiceFn<F>
where
Expand Down

0 comments on commit 2114950

Please sign in to comment.