Skip to content

Commit 8aab8b5

Browse files
authored
Merge pull request #605 from Eh2406/remove_mut_vec
Remove mut vec
2 parents 9e5ccf0 + 6286ec6 commit 8aab8b5

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/bin/fill-in-user-id.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ struct GithubUser {
5252
}
5353

5454
fn update(app: &App, tx: &postgres::transaction::Transaction) {
55-
let mut rows = Vec::new();
56-
let query = "SELECT id, gh_login, gh_access_token, gh_avatar FROM users \
57-
WHERE gh_id IS NULL";
58-
for row in &tx.query(query, &[]).unwrap() {
55+
let query = "SELECT id, gh_login, gh_access_token, gh_avatar FROM users
56+
WHERE gh_id IS NULL";
57+
let rows = tx.query(query, &[]).unwrap().into_iter().map(|row| {
5958
let id: i32 = row.get("id");
6059
let login: String = row.get("gh_login");
6160
let token: String = row.get("gh_access_token");
6261
let avatar: Option<String> = row.get("gh_avatar");
63-
rows.push((id, login, http::token(token), avatar));
64-
}
62+
(id, login, http::token(token), avatar)
63+
}).collect::<Vec<_>>();
6564

6665
for (id, login, token, avatar) in rows {
6766
println!("attempt: {}/{}", id, login);

src/krate.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1015,11 +1015,9 @@ pub fn downloads(req: &mut Request) -> CargoResult<Response> {
10151015
WHERE date > $1
10161016
AND version_id = ANY($2)
10171017
ORDER BY date ASC")?;
1018-
let mut downloads = Vec::new();
1019-
for row in stmt.query(&[&cutoff_date, &ids])?.iter() {
1020-
let download: VersionDownload = Model::from_row(&row);
1021-
downloads.push(download.encodable());
1022-
}
1018+
let downloads = stmt.query(&[&cutoff_date, &ids])?.iter().map(|row| {
1019+
VersionDownload::from_row(&row).encodable()
1020+
}).collect::<Vec<_>>();
10231021

10241022
let stmt = tx.prepare("\
10251023
SELECT COALESCE(to_char(DATE(version_downloads.date), 'YYYY-MM-DD'), '') AS date,
@@ -1032,13 +1030,12 @@ pub fn downloads(req: &mut Request) -> CargoResult<Response> {
10321030
AND versions.id != ALL($3)
10331031
GROUP BY DATE(version_downloads.date)
10341032
ORDER BY DATE(version_downloads.date) ASC")?;
1035-
let mut extra = Vec::new();
1036-
for row in stmt.query(&[&cutoff_date, &krate.id, &ids])?.iter() {
1037-
extra.push(ExtraDownload {
1033+
let extra = stmt.query(&[&cutoff_date, &krate.id, &ids])?.iter().map(|row| {
1034+
ExtraDownload {
10381035
downloads: row.get("downloads"),
10391036
date: row.get("date")
1040-
});
1041-
}
1037+
}
1038+
}).collect::<Vec<_>>();
10421039

10431040
#[derive(RustcEncodable)]
10441041
struct ExtraDownload { date: String, downloads: i64 }

src/version.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,9 @@ pub fn downloads(req: &mut Request) -> CargoResult<Response> {
307307
let stmt = tx.prepare("SELECT * FROM version_downloads
308308
WHERE date > $1 AND version_id = $2
309309
ORDER BY date ASC")?;
310-
let mut downloads = Vec::new();
311-
for row in stmt.query(&[&cutoff_date, &version.id])?.iter() {
312-
let download: VersionDownload = Model::from_row(&row);
313-
downloads.push(download.encodable());
314-
}
310+
let downloads = stmt.query(&[&cutoff_date, &version.id])?.iter().map(|row| {
311+
VersionDownload::from_row(&row).encodable()
312+
}).collect();
315313

316314
#[derive(RustcEncodable)]
317315
struct R { version_downloads: Vec<EncodableVersionDownload> }

0 commit comments

Comments
 (0)