Skip to content

Commit 4037943

Browse files
authored
Merge pull request rust-lang#590 from sgrif/sg-request-constraint-not-object
Implement request extensions on `T: Request` not `Request`
2 parents 6127468 + 798332c commit 4037943

File tree

8 files changed

+54
-70
lines changed

8 files changed

+54
-70
lines changed

src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub trait RequestApp {
101101
fn app(&self) -> &Arc<App>;
102102
}
103103

104-
impl<'a> RequestApp for Request + 'a {
104+
impl<T: Request + ?Sized> RequestApp for T {
105105
fn app(&self) -> &Arc<App> {
106106
self.extensions().find::<Arc<App>>()
107107
.expect("Missing app")

src/bin/delete-crate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#![deny(warnings)]
99

10-
#[macro_use]
1110
extern crate cargo_registry;
1211
extern crate postgres;
1312
extern crate time;

src/db.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub trait RequestTransaction {
209209
fn commit(&self);
210210
}
211211

212-
impl<'a> RequestTransaction for Request + 'a {
212+
impl<T: Request + ?Sized> RequestTransaction for T {
213213
fn db_conn(&self) -> CargoResult<&r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>> {
214214
self.extensions().find::<Transaction>()
215215
.expect("Transaction not present in request")

src/tests/badge.rs

+29-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use conduit::{Request, Method};
1+
use conduit::Method;
22
use conduit_test::MockRequest;
3-
use postgres::GenericConnection;
43

54
use cargo_registry::badge::Badge;
65
use cargo_registry::db::RequestTransaction;
@@ -17,8 +16,6 @@ struct BadgeRef {
1716
gitlab_attributes: HashMap<String, String>,
1817
}
1918

20-
fn tx(req: &Request) -> &GenericConnection { req.tx().unwrap() }
21-
2219
fn set_up() -> (MockRequest, Crate, BadgeRef) {
2320
let (_b, app, _middle) = ::app();
2421
let mut req = ::req(app, Method::Get, "/api/v1/crates/badged_crate");
@@ -88,8 +85,8 @@ fn update_no_badges() {
8885
let badges = HashMap::new();
8986

9087
// Updating with no badges has no effect
91-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
92-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);
88+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
89+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![]);
9390
}
9491

9592
#[test]
@@ -102,8 +99,8 @@ fn update_add_appveyor() {
10299
String::from("appveyor"),
103100
test_badges.appveyor_attributes
104101
);
105-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
106-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![test_badges.appveyor]);
102+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
103+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![test_badges.appveyor]);
107104
}
108105

109106
#[test]
@@ -116,8 +113,8 @@ fn update_add_travis_ci() {
116113
String::from("travis-ci"),
117114
test_badges.travis_ci_attributes
118115
);
119-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
120-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![test_badges.travis_ci]);
116+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
117+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![test_badges.travis_ci]);
121118
}
122119

123120
#[test]
@@ -130,8 +127,8 @@ fn update_add_gitlab() {
130127
String::from("gitlab"),
131128
test_badges.gitlab_attributes
132129
);
133-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
134-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![test_badges.gitlab]);
130+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
131+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![test_badges.gitlab]);
135132
}
136133

137134
#[test]
@@ -145,17 +142,17 @@ fn replace_badge() {
145142
String::from("gitlab"),
146143
test_badges.gitlab_attributes
147144
);
148-
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
149-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![test_badges.gitlab]);
145+
Badge::update_crate(req.tx().unwrap(), &krate, badges.clone()).unwrap();
146+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![test_badges.gitlab]);
150147

151148
// Replace with another badge
152149
badges.clear();
153150
badges.insert(
154151
String::from("travis-ci"),
155152
test_badges.travis_ci_attributes.clone()
156153
);
157-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
158-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![test_badges.travis_ci]);
154+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
155+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![test_badges.travis_ci]);
159156
}
160157

161158
#[test]
@@ -169,8 +166,8 @@ fn update_attributes() {
169166
String::from("travis-ci"),
170167
test_badges.travis_ci_attributes
171168
);
172-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
173-
let current_badges = krate.badges(tx(&req)).unwrap();
169+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
170+
let current_badges = krate.badges(req.tx().unwrap()).unwrap();
174171
assert_eq!(current_badges.len(), 1);
175172
assert!(current_badges.contains(&test_badges.travis_ci));
176173

@@ -189,8 +186,8 @@ fn update_attributes() {
189186
String::from("travis-ci"),
190187
badge_attributes_travis_ci2.clone()
191188
);
192-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
193-
let current_badges = krate.badges(tx(&req)).unwrap();
189+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
190+
let current_badges = krate.badges(req.tx().unwrap()).unwrap();
194191
assert_eq!(current_badges.len(), 1);
195192
assert!(current_badges.contains(&travis_ci2));
196193
}
@@ -215,18 +212,18 @@ fn clear_badges() {
215212
String::from("gitlab"),
216213
test_badges.gitlab_attributes
217214
);
218-
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
215+
Badge::update_crate(req.tx().unwrap(), &krate, badges.clone()).unwrap();
219216

220-
let current_badges = krate.badges(tx(&req)).unwrap();
217+
let current_badges = krate.badges(req.tx().unwrap()).unwrap();
221218
assert_eq!(current_badges.len(), 3);
222219
assert!(current_badges.contains(&test_badges.appveyor));
223220
assert!(current_badges.contains(&test_badges.travis_ci));
224221
assert!(current_badges.contains(&test_badges.gitlab));
225222

226223
// Removing all badges
227224
badges.clear();
228-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
229-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);
225+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
226+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![]);
230227
}
231228

232229
#[test]
@@ -247,8 +244,8 @@ fn appveyor_extra_keys() {
247244
test_badges.appveyor_attributes
248245
);
249246

250-
Badge::update_crate(tx(&req), &krate, badges).unwrap();
251-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![test_badges.appveyor]);
247+
Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
248+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![test_badges.appveyor]);
252249
}
253250

254251
#[test]
@@ -265,10 +262,10 @@ fn travis_ci_required_keys() {
265262
test_badges.travis_ci_attributes
266263
);
267264

268-
let invalid_badges = Badge::update_crate(tx(&req), &krate, badges).unwrap();
265+
let invalid_badges = Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
269266
assert_eq!(invalid_badges.len(), 1);
270267
assert!(invalid_badges.contains(&String::from("travis-ci")));
271-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);
268+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![]);
272269
}
273270

274271
#[test]
@@ -285,10 +282,10 @@ fn gitlab_required_keys() {
285282
test_badges.gitlab_attributes
286283
);
287284

288-
let invalid_badges = Badge::update_crate(tx(&req), &krate, badges).unwrap();
285+
let invalid_badges = Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
289286
assert_eq!(invalid_badges.len(), 1);
290287
assert!(invalid_badges.contains(&String::from("gitlab")));
291-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);
288+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![]);
292289
}
293290

294291
#[test]
@@ -309,8 +306,8 @@ fn unknown_badge() {
309306
invalid_attributes
310307
);
311308

312-
let invalid_badges = Badge::update_crate(tx(&req), &krate, badges).unwrap();
309+
let invalid_badges = Badge::update_crate(req.tx().unwrap(), &krate, badges).unwrap();
313310
assert_eq!(invalid_badges.len(), 1);
314311
assert!(invalid_badges.contains(&String::from("not-a-badge")));
315-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);
312+
assert_eq!(krate.badges(req.tx().unwrap()).unwrap(), vec![]);
316313
}

src/tests/category.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use postgres::GenericConnection;
2-
use conduit::{Handler, Request, Method};
1+
use conduit::{Handler, Method};
32
use conduit_test::MockRequest;
43

54
use cargo_registry::db::RequestTransaction;
@@ -62,8 +61,6 @@ fn show() {
6261
assert_eq!(json.category.subcategories[0].category, "Foo Bar::Baz");
6362
}
6463

65-
fn tx(req: &Request) -> &GenericConnection { req.tx().unwrap() }
66-
6764
#[test]
6865
fn update_crate() {
6966
let (_b, app, middle) = ::app();
@@ -79,42 +76,42 @@ fn update_crate() {
7976
::mock_category(&mut req, "Category 2", "category-2");
8077

8178
// Updating with no categories has no effect
82-
Category::update_crate(tx(&req), &krate, &[]).unwrap();
79+
Category::update_crate(req.tx().unwrap(), &krate, &[]).unwrap();
8380
assert_eq!(cnt(&mut req, "cat1"), 0);
8481
assert_eq!(cnt(&mut req, "category-2"), 0);
8582

8683
// Happy path adding one category
87-
Category::update_crate(tx(&req), &krate, &["cat1".to_string()]).unwrap();
84+
Category::update_crate(req.tx().unwrap(), &krate, &["cat1".to_string()]).unwrap();
8885
assert_eq!(cnt(&mut req, "cat1"), 1);
8986
assert_eq!(cnt(&mut req, "category-2"), 0);
9087

9188
// Replacing one category with another
9289
Category::update_crate(
93-
tx(&req), &krate, &["category-2".to_string()]
90+
req.tx().unwrap(), &krate, &["category-2".to_string()]
9491
).unwrap();
9592
assert_eq!(cnt(&mut req, "cat1"), 0);
9693
assert_eq!(cnt(&mut req, "category-2"), 1);
9794

9895
// Removing one category
99-
Category::update_crate(tx(&req), &krate, &[]).unwrap();
96+
Category::update_crate(req.tx().unwrap(), &krate, &[]).unwrap();
10097
assert_eq!(cnt(&mut req, "cat1"), 0);
10198
assert_eq!(cnt(&mut req, "category-2"), 0);
10299

103100
// Adding 2 categories
104101
Category::update_crate(
105-
tx(&req), &krate, &["cat1".to_string(),
102+
req.tx().unwrap(), &krate, &["cat1".to_string(),
106103
"category-2".to_string()]).unwrap();
107104
assert_eq!(cnt(&mut req, "cat1"), 1);
108105
assert_eq!(cnt(&mut req, "category-2"), 1);
109106

110107
// Removing all categories
111-
Category::update_crate(tx(&req), &krate, &[]).unwrap();
108+
Category::update_crate(req.tx().unwrap(), &krate, &[]).unwrap();
112109
assert_eq!(cnt(&mut req, "cat1"), 0);
113110
assert_eq!(cnt(&mut req, "category-2"), 0);
114111

115112
// Attempting to add one valid category and one invalid category
116113
let invalid_categories = Category::update_crate(
117-
tx(&req), &krate, &["cat1".to_string(),
114+
req.tx().unwrap(), &krate, &["cat1".to_string(),
118115
"catnope".to_string()]
119116
).unwrap();
120117
assert_eq!(invalid_categories, vec!["catnope".to_string()]);
@@ -131,15 +128,15 @@ fn update_crate() {
131128

132129
// Attempting to add a category by display text; must use slug
133130
Category::update_crate(
134-
tx(&req), &krate, &["Category 2".to_string()]
131+
req.tx().unwrap(), &krate, &["Category 2".to_string()]
135132
).unwrap();
136133
assert_eq!(cnt(&mut req, "cat1"), 0);
137134
assert_eq!(cnt(&mut req, "category-2"), 0);
138135

139136
// Add a category and its subcategory
140137
::mock_category(&mut req, "cat1::bar", "cat1::bar");
141138
Category::update_crate(
142-
tx(&req), &krate, &["cat1".to_string(),
139+
req.tx().unwrap(), &krate, &["cat1".to_string(),
143140
"cat1::bar".to_string()]).unwrap();
144141
assert_eq!(cnt(&mut req, "cat1"), 1);
145142
assert_eq!(cnt(&mut req, "cat1::bar"), 1);

src/tests/keyword.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use postgres::GenericConnection;
2-
use conduit::{Handler, Request, Method};
1+
use conduit::{Handler, Method};
32
use conduit_test::MockRequest;
43

54
use cargo_registry::db::RequestTransaction;
@@ -53,8 +52,6 @@ fn uppercase() {
5352
assert_eq!(json.keyword.keyword, "upper".to_string());
5453
}
5554

56-
fn tx(req: &Request) -> &GenericConnection { req.tx().unwrap() }
57-
5855
#[test]
5956
fn update_crate() {
6057
let (_b, app, middle) = ::app();
@@ -69,28 +66,28 @@ fn update_crate() {
6966
::mock_keyword(&mut req, "kw1");
7067
::mock_keyword(&mut req, "kw2");
7168

72-
Keyword::update_crate(tx(&req), &krate, &[]).unwrap();
69+
Keyword::update_crate(req.tx().unwrap(), &krate, &[]).unwrap();
7370
assert_eq!(cnt(&mut req, "kw1"), 0);
7471
assert_eq!(cnt(&mut req, "kw2"), 0);
7572

76-
Keyword::update_crate(tx(&req), &krate, &["kw1".to_string()]).unwrap();
73+
Keyword::update_crate(req.tx().unwrap(), &krate, &["kw1".to_string()]).unwrap();
7774
assert_eq!(cnt(&mut req, "kw1"), 1);
7875
assert_eq!(cnt(&mut req, "kw2"), 0);
7976

80-
Keyword::update_crate(tx(&req), &krate, &["kw2".to_string()]).unwrap();
77+
Keyword::update_crate(req.tx().unwrap(), &krate, &["kw2".to_string()]).unwrap();
8178
assert_eq!(cnt(&mut req, "kw1"), 0);
8279
assert_eq!(cnt(&mut req, "kw2"), 1);
8380

84-
Keyword::update_crate(tx(&req), &krate, &[]).unwrap();
81+
Keyword::update_crate(req.tx().unwrap(), &krate, &[]).unwrap();
8582
assert_eq!(cnt(&mut req, "kw1"), 0);
8683
assert_eq!(cnt(&mut req, "kw2"), 0);
8784

88-
Keyword::update_crate(tx(&req), &krate, &["kw1".to_string(),
85+
Keyword::update_crate(req.tx().unwrap(), &krate, &["kw1".to_string(),
8986
"kw2".to_string()]).unwrap();
9087
assert_eq!(cnt(&mut req, "kw1"), 1);
9188
assert_eq!(cnt(&mut req, "kw2"), 1);
9289

93-
Keyword::update_crate(tx(&req), &krate, &[]).unwrap();
90+
Keyword::update_crate(req.tx().unwrap(), &krate, &[]).unwrap();
9491
assert_eq!(cnt(&mut req, "kw1"), 0);
9592
assert_eq!(cnt(&mut req, "kw2"), 0);
9693

src/tests/krate.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::fs::{self, File};
55
use conduit::{Handler, Request, Method};
66

77
use git2;
8-
use postgres::GenericConnection;
98
use rustc_serialize::json;
109
use semver;
1110

@@ -80,8 +79,6 @@ fn index() {
8079
assert_eq!(json.crates[0].id, krate.name);
8180
}
8281

83-
fn tx(req: &Request) -> &GenericConnection { req.tx().unwrap() }
84-
8582
#[test]
8683
fn index_queries() {
8784
let (_b, app, middle) = ::app();
@@ -94,8 +91,8 @@ fn index_queries() {
9491
let (krate, _) = ::mock_crate(&mut req, krate.clone());
9592
let krate2 = ::krate("BAR_INDEX_QUERIES");
9693
let (krate2, _) = ::mock_crate(&mut req, krate2.clone());
97-
Keyword::update_crate(tx(&req), &krate, &["kw1".into()]).unwrap();
98-
Keyword::update_crate(tx(&req), &krate2, &["KW1".into()]).unwrap();
94+
Keyword::update_crate(req.tx().unwrap(), &krate, &["kw1".into()]).unwrap();
95+
Keyword::update_crate(req.tx().unwrap(), &krate2, &["KW1".into()]).unwrap();
9996

10097
let mut response = ok_resp!(middle.call(req.with_query("q=baz")));
10198
assert_eq!(::json::<CrateList>(&mut response).meta.total, 0);
@@ -134,7 +131,7 @@ fn index_queries() {
134131

135132
::mock_category(&mut req, "cat1", "cat1");
136133
::mock_category(&mut req, "cat1::bar", "cat1::bar");
137-
Category::update_crate(tx(&req), &krate, &["cat1".to_string(),
134+
Category::update_crate(req.tx().unwrap(), &krate, &["cat1".to_string(),
138135
"cat1::bar".to_string()]).unwrap();
139136
let mut response = ok_resp!(middle.call(req.with_query("category=cat1")));
140137
let cl = ::json::<CrateList>(&mut response);
@@ -215,8 +212,7 @@ fn exact_match_on_queries_with_sort() {
215212
let (k4, _) = ::mock_crate(&mut req, krate4.clone());
216213

217214
{
218-
let req2: &mut Request = &mut req;
219-
let tx = req2.tx().unwrap();
215+
let tx = req.tx().unwrap();
220216
tx.execute("UPDATE crates set downloads = $1
221217
WHERE id = $2", &[&krate.downloads, &k.id]).unwrap();
222218
tx.execute("UPDATE crates set downloads = $1
@@ -267,7 +263,7 @@ fn show() {
267263
krate.documentation = Some(format!("https://example.com"));
268264
krate.homepage = Some(format!("http://example.com"));
269265
let (krate, _) = ::mock_crate(&mut req, krate.clone());
270-
Keyword::update_crate(tx(&req), &krate, &["kw1".into()]).unwrap();
266+
Keyword::update_crate(req.tx().unwrap(), &krate, &["kw1".into()]).unwrap();
271267

272268
let mut response = ok_resp!(middle.call(&mut req));
273269
let json: CrateResponse = ::json(&mut response);

0 commit comments

Comments
 (0)