Skip to content

Commit 076d70f

Browse files
committed
Add tokio::sync::RwLock instead of parking_lot
1 parent 4dcf32a commit 076d70f

File tree

8 files changed

+49
-144
lines changed

8 files changed

+49
-144
lines changed

ch_04/final/Cargo.lock

+16-69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch_04/final/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
warp = "0.3"
8-
parking_lot = "0.10.0"
98
serde = { version = "1.0", features = ["derive"] }
109
serde_json = "1.0"
1110
tokio = { version = "1.1.1", features = ["full"] }

ch_04/final/src/main.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use parking_lot::RwLock;
21
use serde::{Deserialize, Serialize};
32
use std::{collections::HashMap, sync::Arc};
43
use warp::{
@@ -9,6 +8,8 @@ use warp::{
98
Filter, Rejection, Reply,
109
};
1110

11+
use tokio::sync::RwLock;
12+
1213
#[derive(Deserialize, Serialize, Debug, Clone)]
1314
struct Question {
1415
id: QuestionId,
@@ -123,11 +124,11 @@ async fn get_questions(
123124
) -> Result<impl warp::Reply, warp::Rejection> {
124125
if !params.is_empty() {
125126
let pagination = extract_pagination(params)?;
126-
let res: Vec<Question> = store.questions.read().values().cloned().collect();
127+
let res: Vec<Question> = store.questions.read().await.values().cloned().collect();
127128
let res = &res[pagination.start..pagination.end];
128129
Ok(warp::reply::json(&res))
129130
} else {
130-
let res: Vec<Question> = store.questions.read().values().cloned().collect();
131+
let res: Vec<Question> = store.questions.read().await.values().cloned().collect();
131132
Ok(warp::reply::json(&res))
132133
}
133134
}
@@ -139,6 +140,7 @@ async fn add_question(
139140
store
140141
.questions
141142
.write()
143+
.await
142144
.insert(question.id.clone(), question);
143145

144146
Ok(warp::reply::with_status("Question added", StatusCode::OK))
@@ -149,7 +151,7 @@ async fn update_question(
149151
store: Store,
150152
question: Question,
151153
) -> Result<impl warp::Reply, warp::Rejection> {
152-
match store.questions.write().get_mut(&QuestionId(id)) {
154+
match store.questions.write().await.get_mut(&QuestionId(id)) {
153155
Some(q) => *q = question,
154156
None => return Err(warp::reject::custom(Error::QuestionNotFound)),
155157
}
@@ -158,7 +160,7 @@ async fn update_question(
158160
}
159161

160162
async fn delete_question(id: String, store: Store) -> Result<impl warp::Reply, warp::Rejection> {
161-
match store.questions.write().remove(&QuestionId(id)) {
163+
match store.questions.write().await.remove(&QuestionId(id)) {
162164
Some(_) => return Ok(warp::reply::with_status("Question deleted", StatusCode::OK)),
163165
None => return Err(warp::reject::custom(Error::QuestionNotFound)),
164166
}
@@ -174,7 +176,11 @@ async fn add_answer(
174176
question_id: QuestionId(params.get("questionId").unwrap().to_string()),
175177
};
176178

177-
store.answers.write().insert(answer.id.clone(), answer);
179+
store
180+
.answers
181+
.write()
182+
.await
183+
.insert(answer.id.clone(), answer);
178184

179185
Ok(warp::reply::with_status("Answer added", StatusCode::OK))
180186
}

0 commit comments

Comments
 (0)