Skip to content

Commit

Permalink
Update all formatting of Rust projects
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Jul 15, 2019
1 parent 338c453 commit a5bfbe1
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 256 deletions.
13 changes: 6 additions & 7 deletions frameworks/Rust/hyper/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ pub struct Fortune {
pub message: String,
}

pub fn connect(addr: SocketAddr, config: Config, handle: Handle) -> impl Future<Item = Db, Error = ()> {
pub fn connect(
addr: SocketAddr,
config: Config,
handle: Handle,
) -> impl Future<Item = Db, Error = ()> {
TcpStream::connect(&addr, &handle)
.map_err(|e| panic!("error connecting to postgresql: {}", e))
.and_then(move |tcp| {
Expand All @@ -29,12 +33,7 @@ pub fn connect(addr: SocketAddr, config: Config, handle: Handle) -> impl Future<
client
.prepare("SELECT id, message FROM fortune")
.map_err(|_| ())
.map(move |fortune| {
Db {
client,
fortune,
}
})
.map(move |fortune| Db { client, fortune })
})
}

Expand Down
17 changes: 8 additions & 9 deletions frameworks/Rust/hyper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ extern crate serde_derive;
extern crate serde_json;
extern crate tokio_core;


use futures::Future;

use hyper::{Body, Response, StatusCode};
use hyper::header::{CONTENT_LENGTH, CONTENT_TYPE, SERVER, HeaderValue};
use hyper::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE, SERVER};
use hyper::service::service_fn_ok;
use hyper::{Body, Response, StatusCode};

mod server;

Expand All @@ -23,7 +22,6 @@ struct JsonResponse<'a> {
message: &'a str,
}


fn main() {
// It seems most of the other benchmarks create static header values
// for performance, so just play by the same rules here...
Expand Down Expand Up @@ -65,7 +63,9 @@ fn main() {
Body::from(HELLO_WORLD)
}
"/json" => {
let rep = JsonResponse { message: "Hello, world!" };
let rep = JsonResponse {
message: "Hello, world!",
};
let rep_body = serde_json::to_vec(&rep).unwrap();
headers.insert(CONTENT_LENGTH, json_len.clone());
headers.insert(CONTENT_TYPE, json_ct.clone());
Expand All @@ -76,7 +76,7 @@ fn main() {
*res.status_mut() = StatusCode::NOT_FOUND;
*res.headers_mut() = headers;
return res;
},
}
};

headers.insert(SERVER, server_header.clone());
Expand All @@ -88,9 +88,8 @@ fn main() {

// Spawn the `serve_connection` future into the runtime.
handle.spawn(
http
.serve_connection(socket, svc)
.map_err(|e| eprintln!("connection error: {}", e))
http.serve_connection(socket, svc)
.map_err(|e| eprintln!("connection error: {}", e)),
);
})
}
35 changes: 14 additions & 21 deletions frameworks/Rust/hyper/src/main_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::fmt::Write;
use std::net::ToSocketAddrs;

use futures::{future, Future};
use hyper::header::{CONTENT_TYPE, SERVER, HeaderValue};
use hyper::header::{HeaderValue, CONTENT_TYPE, SERVER};
use hyper::service::service_fn;
use hyper::{Body, Response};

Expand Down Expand Up @@ -37,8 +37,8 @@ fn main() {
let server_header = HeaderValue::from_static("hyper");

// Before handling any requests, we should grab a DB connection.
let db_fut = db::connect(psql_addr, psql_config.clone(), handle.clone())
.map(move |mut db_conn| {
let db_fut =
db::connect(psql_addr, psql_config.clone(), handle.clone()).map(move |mut db_conn| {
let html_ct = html_ct.clone();
let server_header = server_header.clone();

Expand All @@ -57,35 +57,28 @@ fn main() {

match req.uri.path() {
"/fortune" => {
future::Either::A(db_conn
.tell_fortune()
.map(move |fortunes| {
let mut buf = String::with_capacity(2048);
let _ = write!(&mut buf, "{}", FortunesTemplate {
fortunes,
});
let mut res = Response::new(Body::from(buf));
*res.headers_mut() = headers;
res
}))
},
future::Either::A(db_conn.tell_fortune().map(move |fortunes| {
let mut buf = String::with_capacity(2048);
let _ = write!(&mut buf, "{}", FortunesTemplate { fortunes });
let mut res = Response::new(Body::from(buf));
*res.headers_mut() = headers;
res
}))
}
_ => {
let mut res = Response::new(Body::empty());
*res.status_mut() = hyper::StatusCode::NOT_FOUND;
*res.headers_mut() = headers;
future::Either::B(future::ok(res))
},
}
}
});


// Spawn the `serve_connection` future into the runtime.
handle2.spawn(
http
.serve_connection(socket, svc)
.map_err(|e| eprintln!("connection error: {}", e))
http.serve_connection(socket, svc)
.map_err(|e| eprintln!("connection error: {}", e)),
);

});
handle.spawn(db_fut);
});
Expand Down
14 changes: 7 additions & 7 deletions frameworks/Rust/hyper/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::thread;

use futures::{Future, Stream};
use hyper::server::conn::Http;
use tokio_core::reactor::{Core, Handle};
use tokio_core::net::{TcpListener, TcpStream};
use tokio_core::reactor::{Core, Handle};

pub(crate) fn run<F>(per_connection: F)
where
Expand Down Expand Up @@ -35,11 +35,11 @@ where

// Bind to 0.0.0.0:8080
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
let tcp = reuse_listener(&addr, &handle)
.expect("couldn't bind to addr");
let tcp = reuse_listener(&addr, &handle).expect("couldn't bind to addr");

// For every accepted connection, spawn an HTTP task
let server = tcp.incoming()
let server = tcp
.incoming()
.for_each(move |(sock, _addr)| {
let _ = sock.set_nodelay(true);
per_connection(sock, &mut http, &handle);
Expand All @@ -66,7 +66,7 @@ fn reuse_listener(addr: &SocketAddr, handle: &Handle) -> io::Result<TcpListener>

builder.reuse_address(true)?;
builder.bind(addr)?;
builder.listen(1024).and_then(|l| {
TcpListener::from_listener(l, addr, handle)
})
builder
.listen(1024)
.and_then(|l| TcpListener::from_listener(l, addr, handle))
}
Loading

0 comments on commit a5bfbe1

Please sign in to comment.