Skip to content

Commit e5d6aa8

Browse files
committed
Use derive_deref
The generated impls from that crate are very slightly different than the impls we had before. Things will deref to `&String` and `&Vec<T>` rather than `&str` and `&[T]`. However, this rarely matters, and in the few places it did, it was just a matter of one extra `*` or `&`
1 parent e4f8898 commit e5d6aa8

File tree

5 files changed

+26
-68
lines changed

5 files changed

+26
-68
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ docopt = "0.8.1"
5959
itertools = "0.6.0"
6060
lettre = "0.6"
6161
scheduled-thread-pool = "0.2.0"
62+
derive_deref = "1.0.0"
6263

6364
conduit = "0.8"
6465
conduit-conditional-get = "0.8"

src/krate/publish.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ pub fn publish(req: &mut Request) -> CargoResult<Response> {
5151
let keywords = new_crate
5252
.keywords
5353
.as_ref()
54-
.map(|kws| kws.iter().map(|kw| &**kw).collect())
54+
.map(|kws| kws.iter().map(|kw| &***kw).collect())
5555
.unwrap_or_else(Vec::new);
5656

5757
let categories = new_crate.categories.as_ref().map(|s| &s[..]).unwrap_or(&[]);
58-
let categories: Vec<_> = categories.iter().map(|k| &**k).collect();
58+
let categories: Vec<_> = categories.iter().map(|k| &***k).collect();
5959

6060
let conn = req.db_conn()?;
6161
// Create a transaction on the database, if there are no errors,
@@ -87,7 +87,7 @@ pub fn publish(req: &mut Request) -> CargoResult<Response> {
8787
));
8888
}
8989

90-
if krate.name != name {
90+
if &krate.name != name {
9191
return Err(human(&format_args!(
9292
"crate was previously named `{}`",
9393
krate.name

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern crate chrono;
1212
extern crate comrak;
1313
extern crate curl;
1414
#[macro_use]
15+
extern crate derive_deref;
16+
#[macro_use]
1517
extern crate diesel;
1618
extern crate diesel_full_text_search;
1719
extern crate dotenv;

src/upload.rs

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! to and from structs. The serlializing is only utilised in
44
//! integration tests.
55
use std::collections::HashMap;
6-
use std::ops::Deref;
76

87
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
98
use semver;
@@ -32,21 +31,21 @@ pub struct NewCrate {
3231
pub badges: Option<HashMap<String, HashMap<String, String>>>,
3332
}
3433

35-
#[derive(PartialEq, Eq, Hash, Serialize, Debug)]
34+
#[derive(PartialEq, Eq, Hash, Serialize, Debug, Deref)]
3635
pub struct CrateName(pub String);
37-
#[derive(Debug)]
36+
#[derive(Debug, Deref)]
3837
pub struct CrateVersion(pub semver::Version);
39-
#[derive(Debug)]
38+
#[derive(Debug, Deref)]
4039
pub struct CrateVersionReq(pub semver::VersionReq);
41-
#[derive(Serialize, Debug)]
40+
#[derive(Serialize, Debug, Deref)]
4241
pub struct KeywordList(pub Vec<Keyword>);
43-
#[derive(Serialize, Debug)]
42+
#[derive(Serialize, Debug, Deref)]
4443
pub struct Keyword(pub String);
45-
#[derive(Serialize, Debug)]
44+
#[derive(Serialize, Debug, Deref)]
4645
pub struct CategoryList(pub Vec<Category>);
47-
#[derive(Serialize, Deserialize, Debug)]
46+
#[derive(Serialize, Deserialize, Debug, Deref)]
4847
pub struct Category(pub String);
49-
#[derive(Serialize, Debug)]
48+
#[derive(Serialize, Debug, Deref)]
5049
pub struct Feature(pub String);
5150

5251
#[derive(Serialize, Deserialize, Debug)]
@@ -177,6 +176,7 @@ impl<'de> Deserialize<'de> for CategoryList {
177176
}
178177
}
179178
}
179+
180180
impl Serialize for CrateVersion {
181181
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
182182
where
@@ -195,62 +195,6 @@ impl Serialize for CrateVersionReq {
195195
}
196196
}
197197

198-
impl Deref for CrateName {
199-
type Target = str;
200-
fn deref(&self) -> &str {
201-
&self.0
202-
}
203-
}
204-
205-
impl Deref for Keyword {
206-
type Target = str;
207-
fn deref(&self) -> &str {
208-
&self.0
209-
}
210-
}
211-
212-
impl Deref for Category {
213-
type Target = str;
214-
fn deref(&self) -> &str {
215-
&self.0
216-
}
217-
}
218-
219-
impl Deref for Feature {
220-
type Target = str;
221-
fn deref(&self) -> &str {
222-
&self.0
223-
}
224-
}
225-
226-
impl Deref for CrateVersion {
227-
type Target = semver::Version;
228-
fn deref(&self) -> &semver::Version {
229-
&self.0
230-
}
231-
}
232-
233-
impl Deref for CrateVersionReq {
234-
type Target = semver::VersionReq;
235-
fn deref(&self) -> &semver::VersionReq {
236-
&self.0
237-
}
238-
}
239-
240-
impl Deref for KeywordList {
241-
type Target = [Keyword];
242-
fn deref(&self) -> &[Keyword] {
243-
&self.0
244-
}
245-
}
246-
247-
impl Deref for CategoryList {
248-
type Target = [Category];
249-
fn deref(&self) -> &[Category] {
250-
&self.0
251-
}
252-
}
253-
254198
use diesel::pg::Pg;
255199
use diesel::types::{IsNull, Text, ToSql, ToSqlOutput};
256200
use std::error::Error;

0 commit comments

Comments
 (0)