Skip to content

Commit 88741be

Browse files
committed
use arc mutex
1 parent 6bfeb47 commit 88741be

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/fetcher.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub struct Problems {
175175

176176
#[derive(Debug, Serialize, Deserialize)]
177177
pub struct StatWithStatus {
178-
pub stat: Stat,
178+
stat: Stat,
179179
difficulty: Difficulty,
180180
paid_only: bool,
181181
is_favor: bool,
@@ -184,19 +184,19 @@ pub struct StatWithStatus {
184184
}
185185

186186
#[derive(Debug, Serialize, Deserialize)]
187-
pub struct Stat {
187+
struct Stat {
188188
question_id: u32,
189189
#[serde(rename = "question__article__slug")]
190190
question_article_slug: Option<String>,
191191
#[serde(rename = "question__title")]
192192
question_title: Option<String>,
193193
#[serde(rename = "question__title_slug")]
194-
pub question_title_slug: Option<String>,
194+
question_title_slug: Option<String>,
195195
#[serde(rename = "question__hide")]
196196
question_hide: bool,
197197
total_acs: u32,
198198
total_submitted: u32,
199-
pub frontend_question_id: u32,
199+
frontend_question_id: u32,
200200
is_new_question: bool,
201201
}
202202

src/main.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use futures::executor::ThreadPool;
1919
use futures::future::join_all;
2020
use futures::stream::StreamExt;
2121
use futures::task::SpawnExt;
22+
use std::sync::{Arc, Mutex};
2223

2324
/// main() helps to generate the submission template .rs
2425
fn main() {
@@ -63,18 +64,9 @@ fn main() {
6364
let pool = ThreadPool::new().unwrap();
6465
let mut tasks = vec![];
6566
let problems = fetcher::get_problems().unwrap();
66-
let mut mod_file_addon = vec![];
67+
let mut mod_file_addon = Arc::new(Mutex::new(vec![]));
6768
for problem_stat in problems.stat_status_pairs {
68-
mod_file_addon.push(format!(
69-
"mod p{:04}_{};",
70-
problem_stat.stat.frontend_question_id,
71-
problem_stat
72-
.stat
73-
.question_title_slug
74-
.clone()
75-
.unwrap()
76-
.replace("-", "_")
77-
));
69+
let mod_file_addon = mod_file_addon.clone();
7870
tasks.push(
7971
pool.spawn_with_handle(async move {
8072
let problem = fetcher::get_problem_async(problem_stat).await;
@@ -90,6 +82,14 @@ fn main() {
9082
println!("Problem {} has no rust version.", problem.question_id);
9183
return;
9284
}
85+
async {
86+
mod_file_addon.lock().unwrap().push(format!(
87+
"mod p{:04}_{};",
88+
problem.question_id,
89+
problem.title_slug.replace("-", "_")
90+
));
91+
}
92+
.await;
9393
let code = code.unwrap();
9494
async { deal_problem(&problem, &code, false) }.await
9595
})
@@ -102,7 +102,7 @@ fn main() {
102102
.append(true)
103103
.open("./src/problem/mod.rs")
104104
.unwrap();
105-
writeln!(lib_file, "{}", mod_file_addon.join("\n"));
105+
writeln!(lib_file, "{}", mod_file_addon.lock().unwrap().join("\n"));
106106
break;
107107
} else {
108108
id = id_arg

0 commit comments

Comments
 (0)