Skip to content

Commit

Permalink
Rustup and Clippy fixes for Rust 1.78
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfier committed Jun 10, 2024
1 parent fa6ef63 commit 4746f4b
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 45 deletions.
6 changes: 3 additions & 3 deletions examples/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn main() {
let mut db = db.transaction().unwrap();

// For better readability, we handle the request in a separate function.
let response = note_routes(&request, &mut db);
let response = note_routes(request, &mut db);

// If the response is a success, we commit the transaction before returning. It's only at
// this point that data are actually written in the database.
Expand Down Expand Up @@ -148,7 +148,7 @@ fn note_routes(request: &Request, db: &mut Transaction) -> Response {
// This route modifies the content of an existing note.

// We start by reading the body of the HTTP request into a `String`.
let body = try_or_400!(rouille::input::plain_text_body(&request));
let body = try_or_400!(rouille::input::plain_text_body(request));

// And write the content with a query. This line can only panic if the
// SQL is malformed.
Expand All @@ -168,7 +168,7 @@ fn note_routes(request: &Request, db: &mut Transaction) -> Response {
// This route creates a new note whose initial content is the body.

// We start by reading the body of the HTTP request into a `String`.
let body = try_or_400!(rouille::input::plain_text_body(&request));
let body = try_or_400!(rouille::input::plain_text_body(request));

// To do so, we first create a variable that will receive the content.
let mut id: Option<i32> = None;
Expand Down
4 changes: 2 additions & 2 deletions examples/git-http-backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
println!("Now listening on localhost:8000");

rouille::start_server("localhost:8000", move |request| {
rouille::log(&request, io::stdout(), || {
rouille::log(request, io::stdout(), || {
// When a request is received, we invoke the `git http-backend` command through CGI.
let mut cmd = Command::new("git");
cmd.arg("http-backend");
Expand All @@ -42,7 +42,7 @@ fn main() {
// Note that an error is returned only if `git http-backend` fails to execute, and not
// if the client sends bad data for example. In other words, an error can only occur
// if the server was misconfigured. Therefore it's okay-ish to call `unwrap()` here.
cmd.start_cgi(&request).unwrap()
cmd.start_cgi(request).unwrap()
})
});
}
12 changes: 4 additions & 8 deletions examples/login-session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
let sessions_storage: Mutex<HashMap<String, SessionData>> = Mutex::new(HashMap::new());

rouille::start_server("localhost:8000", move |request| {
rouille::log(&request, io::stdout(), || {
rouille::log(request, io::stdout(), || {
// We call `session::session` in order to assign a unique identifier to each client.
// This identifier is tracked through a cookie that is automatically appended to the
// response.
Expand All @@ -48,19 +48,15 @@ fn main() {
//
// We thus obtain a `Option<SessionData>`.
let mut session_data = if session.client_has_sid() {
if let Some(data) = sessions_storage.lock().unwrap().get(session.id()) {
Some(data.clone())
} else {
None
}
sessions_storage.lock().unwrap().get(session.id()).map(|data| data.clone())
} else {
None
};

// Use a separate function to actually handle the request, for readability.
// We pass a mutable reference to the `Option<SessionData>` so that the function
// is free to modify it.
let response = handle_route(&request, &mut session_data);
let response = handle_route(request, &mut session_data);

// Since the function call to `handle_route` can modify the session data, we have
// to store it back in the `sessions_storage` when necessary.
Expand Down Expand Up @@ -117,7 +113,7 @@ fn handle_route(request: &Request, session_data: &mut Option<SessionData>) -> Re
// In this example all login attempts are successful in the password starts with the
// letter 'b'. Of course in a real website you should check the credentials in a proper
// way.
if data.password.starts_with("b") {
if data.password.starts_with('b') {
// Logging the user in is done by writing the content of `session_data`.
//
// A minor warning here: in this demo we store in memory directly the data that
Expand Down
2 changes: 1 addition & 1 deletion examples/php.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ fn main() {
cmd.arg("-n"); // Don't use a php.ini.
cmd.env("SCRIPT_FILENAME", "examples/php-test.php"); // The PHP script to use.
cmd.env("REDIRECT_STATUS", "1"); // Necessary for security.
cmd.start_cgi(&request).unwrap()
cmd.start_cgi(request).unwrap()
});
}
2 changes: 1 addition & 1 deletion examples/reverse-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {

rouille::start_server("localhost:8000", move |request| {
rouille::proxy::full_proxy(
&request,
request,
rouille::proxy::ProxyConfig {
addr: "example.com:80",
replace_host: Some("example.com".into()),
Expand Down
4 changes: 2 additions & 2 deletions examples/simple-form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() {
println!("Now listening on localhost:8000");

rouille::start_server("localhost:8000", move |request| {
rouille::log(&request, io::stdout(), || {
rouille::log(request, io::stdout(), || {
router!(request,
(GET) (/) => {
// When viewing the home page, we return an HTML document described below.
Expand Down Expand Up @@ -46,7 +46,7 @@ fn main() {
}

// The HTML document of the home page.
static FORM: &'static str = r#"
static FORM: &str = r#"
<html>
<head>
<title>Form</title>
Expand Down
2 changes: 1 addition & 1 deletion examples/static-files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {
// located.
// In order to avoid potential security threats, `match_assets` will never return any
// file outside of this directory even if the URL is for example `/../../foo.txt`.
let response = rouille::match_assets(&request, ".");
let response = rouille::match_assets(request, ".");

// If a file is found, the `match_assets` function will return a response with a 200
// status code and the content of the file. If no file is found, it will instead return
Expand Down
2 changes: 1 addition & 1 deletion examples/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() {
// function, and a `websocket` variable of type `Receiver<Websocket>`.
// Once the response has been sent back to the client, the `Receiver` will be
// filled by rouille with a `Websocket` object representing the websocket.
let (response, websocket) = try_or_400!(websocket::start(&request, Some("echo")));
let (response, websocket) = try_or_400!(websocket::start(request, Some("echo")));

// Because of the nature of I/O in Rust, we need to spawn a separate thread for
// each websocket.
Expand Down
4 changes: 2 additions & 2 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ use Response;
/// In this example, a request made to `/static/test.txt` will return the file
/// `public/test.txt` if it exists.
///
pub fn match_assets<P: ?Sized>(request: &Request, path: &P) -> Response
pub fn match_assets<P>(request: &Request, path: &P) -> Response
where
P: AsRef<Path>,
P: AsRef<Path> + ?Sized,
{
let path = path.as_ref();
let path = match path.canonicalize() {
Expand Down
4 changes: 2 additions & 2 deletions src/cgi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ impl CgiRun for Command {
.env("REMOTE_USER", "") // FIXME:
.env(
"CONTENT_TYPE",
&request.header("Content-Type").unwrap_or(""),
request.header("Content-Type").unwrap_or(""),
)
.env(
"CONTENT_LENGTH",
&request.header("Content-Length").unwrap_or(""),
request.header("Content-Length").unwrap_or(""),
)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
Expand Down
4 changes: 2 additions & 2 deletions src/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn apply(request: &Request, mut response: Response) -> Response {
if response
.headers
.iter()
.any(|&(ref key, _)| key.eq_ignore_ascii_case("Content-Encoding"))
.any(|(key, _)| key.eq_ignore_ascii_case("Content-Encoding"))
{
return response;
}
Expand All @@ -91,7 +91,7 @@ pub fn apply(request: &Request, mut response: Response) -> Response {
// Since encoding is purely an optimization, it's not a problem if the function sometimes has
// false positives or false negatives.
fn response_is_text(response: &Response) -> bool {
response.headers.iter().any(|&(ref key, ref value)| {
response.headers.iter().any(|(key, value)| {
if !key.eq_ignore_ascii_case("Content-Type") {
return false;
}
Expand Down
7 changes: 5 additions & 2 deletions src/input/basic_http_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! - In order to read a plain text body, see
//! [the `plain_text_body` function](fn.plain_text_body.html).
use base64::{Engine as _, prelude::BASE64_STANDARD};
use base64::{prelude::BASE64_STANDARD, Engine as _};
use Request;

/// Credentials returned by `basic_http_auth`.
Expand Down Expand Up @@ -73,7 +73,10 @@ pub fn basic_http_auth(request: &Request) -> Option<HttpAuthCredentials> {
return None;
}

let authvalue = match split.next().and_then(|val| BASE64_STANDARD.decode(val).ok()) {
let authvalue = match split
.next()
.and_then(|val| BASE64_STANDARD.decode(val).ok())
{
Some(v) => v,
None => return None,
};
Expand Down
2 changes: 1 addition & 1 deletion src/input/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mod test {

assert_eq!(
cookies(&request).collect::<Vec<_>>(),
vec![("a".into(), "b".into()), ("hello".into(), "world".into())]
vec![("a", "b"), ("hello", "world")]
);
}
}
6 changes: 3 additions & 3 deletions src/input/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ mod tests {

let input = post_input!(&request, { field: bool }).unwrap();

assert_eq!(input.field, true);
assert!(input.field);
}

#[test]
Expand Down Expand Up @@ -908,7 +908,7 @@ mod tests {

let input = post_input!(&request, { field: bool }).unwrap();

assert_eq!(input.field, true);
assert!(input.field);
}

#[test]
Expand Down Expand Up @@ -1051,7 +1051,7 @@ mod tests {

let input = post_input!(&request, { field: bool }).unwrap();

assert_eq!(input.field, false);
assert!(!input.field);
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,8 @@ impl Request {
pub fn header(&self, key: &str) -> Option<&str> {
self.headers
.iter()
.find(|&&(ref k, _)| k.eq_ignore_ascii_case(key))
.map(|&(_, ref v)| &v[..])
.find(|&(k, _)| k.eq_ignore_ascii_case(key))
.map(|(_, v)| &v[..])
}

/// Returns a list of all the headers of the request.
Expand Down Expand Up @@ -973,8 +973,8 @@ impl Request {
/// ```
pub fn do_not_track(&self) -> Option<bool> {
match self.header("DNT") {
Some(h) if h == "1" => Some(true),
Some(h) if h == "0" => Some(false),
Some("1") => Some(true),
Some("0") => Some(false),
_ => None,
}
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ impl<'a> Iterator for HeadersIter<'a> {

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|&(ref k, ref v)| (&k[..], &v[..]))
self.iter.next().map(|(k, v)| (&k[..], &v[..]))
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ impl Response {
/// Removes all headers from the response that match `header`.
pub fn without_header(mut self, header: &str) -> Response {
self.headers
.retain(|&(ref h, _)| !h.eq_ignore_ascii_case(header));
.retain(|(h, _)| !h.eq_ignore_ascii_case(header));
self
}

Expand Down Expand Up @@ -534,7 +534,7 @@ impl Response {
let header = header.into();

let mut found_one = false;
self.headers.retain(|&(ref h, _)| {
self.headers.retain(|(h, _)| {
if h.eq_ignore_ascii_case(&header) {
if !found_one {
found_one = true;
Expand Down Expand Up @@ -597,7 +597,7 @@ impl Response {
}

let mut not_modified = false;
for &(ref key, ref etag) in &self.headers {
for (key, etag) in &self.headers {
if !key.eq_ignore_ascii_case("ETag") {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
F: FnOnce(&Session<'r>) -> Response,
{
let mut cookie = input::cookies(request);
let cookie = cookie.find(|&(ref k, _)| k == &cookie_name);
let cookie = cookie.find(|(k, _)| k == &cookie_name);
let cookie = cookie.map(|(_, v)| v);

let session = if let Some(cookie) = cookie {
Expand Down Expand Up @@ -125,7 +125,7 @@ pub fn generate_session_id() -> String {
.sample_iter(&Alphanumeric)
.map(char::from)
.filter(|&c| {
('a'..='z').contains(&c) || ('A'..='Z').contains(&c) || ('0'..='9').contains(&c)
c.is_ascii_lowercase() || c.is_ascii_uppercase() || c.is_ascii_digit()
})
.take(64)
.collect::<String>()
Expand Down
6 changes: 3 additions & 3 deletions src/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub use self::websocket::Message;
pub use self::websocket::SendError;
pub use self::websocket::Websocket;

use base64::{Engine as _, prelude::BASE64_STANDARD};
use base64::{prelude::BASE64_STANDARD, Engine as _};
use sha1_smol::Sha1;
use std::borrow::Cow;
use std::error;
Expand Down Expand Up @@ -152,7 +152,7 @@ where
// TODO: there are some version shenanigans to handle
// see https://tools.ietf.org/html/rfc6455#section-4.4
match request.header("Sec-WebSocket-Version") {
Some(h) if h == "13" => (),
Some("13") => (),
_ => return Err(WebsocketError::InvalidWebsocketRequest),
}

Expand Down Expand Up @@ -246,5 +246,5 @@ fn convert_key(input: &str) -> String {
sha1.update(input.as_bytes());
sha1.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");

BASE64_STANDARD.encode(&sha1.digest().bytes())
BASE64_STANDARD.encode(sha1.digest().bytes())
}
2 changes: 1 addition & 1 deletion src/websocket/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Iterator for Websocket {
// Read `n` bytes in `buf`.
let mut buf = [0; 256];
let n = match self.socket.as_mut().unwrap().read(&mut buf) {
Ok(n) if n == 0 => {
Ok(0) => {
// Read returning zero means EOF
self.socket = None;
return None;
Expand Down

0 comments on commit 4746f4b

Please sign in to comment.