Skip to content

Commit d8ff8ed

Browse files
committed
Auto merge of #6451 - dwijnand:reify-DepFingerprint, r=alexcrichton
Reify the DepFingerprint type I'm very slightly concerned I might've incompatibly changed the serialise/deserialise code with respect to existing serialised data, so please double-check my work.
2 parents adf047d + 14f0f89 commit d8ff8ed

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

src/cargo/core/compiler/fingerprint.rs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ pub fn prepare_target<'a, 'cfg>(
132132
/// * its package id
133133
/// * its extern crate name
134134
/// * its calculated fingerprint for the dependency
135-
type DepFingerprint = (String, String, Arc<Fingerprint>);
135+
struct DepFingerprint {
136+
pkg_id: String,
137+
name: String,
138+
fingerprint: Arc<Fingerprint>,
139+
}
136140

137141
/// A fingerprint can be considered to be a "short string" representing the
138142
/// state of a world for a package.
@@ -162,10 +166,6 @@ pub struct Fingerprint {
162166
target: u64,
163167
profile: u64,
164168
path: u64,
165-
#[serde(
166-
serialize_with = "serialize_deps",
167-
deserialize_with = "deserialize_deps"
168-
)]
169169
deps: Vec<DepFingerprint>,
170170
local: Vec<LocalFingerprint>,
171171
#[serde(skip_serializing, skip_deserializing)]
@@ -174,32 +174,31 @@ pub struct Fingerprint {
174174
edition: Edition,
175175
}
176176

177-
fn serialize_deps<S>(deps: &[DepFingerprint], ser: S) -> Result<S::Ok, S::Error>
178-
where
179-
S: ser::Serializer,
180-
{
181-
ser.collect_seq(deps.iter().map(|&(ref a, ref b, ref c)| (a, b, c.hash())))
177+
impl Serialize for DepFingerprint {
178+
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
179+
where
180+
S: ser::Serializer,
181+
{
182+
(&self.pkg_id, &self.name, &self.fingerprint.hash()).serialize(ser)
183+
}
182184
}
183185

184-
fn deserialize_deps<'de, D>(d: D) -> Result<Vec<DepFingerprint>, D::Error>
185-
where
186-
D: de::Deserializer<'de>,
187-
{
188-
let decoded = <Vec<(String, String, u64)>>::deserialize(d)?;
189-
Ok(decoded
190-
.into_iter()
191-
.map(|(pkg_id, name, hash)| {
192-
(
193-
pkg_id,
194-
name,
195-
Arc::new(Fingerprint {
196-
local: vec![LocalFingerprint::Precalculated(String::new())],
197-
memoized_hash: Mutex::new(Some(hash)),
198-
..Fingerprint::new()
199-
}),
200-
)
186+
impl<'de> Deserialize<'de> for DepFingerprint {
187+
fn deserialize<D>(d: D) -> Result<DepFingerprint, D::Error>
188+
where
189+
D: de::Deserializer<'de>,
190+
{
191+
let (pkg_id, name, hash) = <(String, String, u64)>::deserialize(d)?;
192+
Ok(DepFingerprint {
193+
pkg_id,
194+
name,
195+
fingerprint: Arc::new(Fingerprint {
196+
local: vec![LocalFingerprint::Precalculated(String::new())],
197+
memoized_hash: Mutex::new(Some(hash)),
198+
..Fingerprint::new()
199+
}),
201200
})
202-
.collect())
201+
}
203202
}
204203

205204
#[derive(Serialize, Deserialize, Hash)]
@@ -352,8 +351,8 @@ impl Fingerprint {
352351
failure::bail!("number of dependencies has changed")
353352
}
354353
for (a, b) in self.deps.iter().zip(old.deps.iter()) {
355-
if a.1 != b.1 || a.2.hash() != b.2.hash() {
356-
failure::bail!("new ({}) != old ({})", a.0, b.0)
354+
if a.name != b.name || a.fingerprint.hash() != b.fingerprint.hash() {
355+
failure::bail!("new ({}) != old ({})", a.pkg_id, b.pkg_id)
357356
}
358357
}
359358
Ok(())
@@ -380,7 +379,12 @@ impl hash::Hash for Fingerprint {
380379
.hash(h);
381380

382381
h.write_usize(deps.len());
383-
for &(ref pkg_id, ref name, ref fingerprint) in deps {
382+
for DepFingerprint {
383+
pkg_id,
384+
name,
385+
fingerprint,
386+
} in deps
387+
{
384388
pkg_id.hash(h);
385389
name.hash(h);
386390
// use memoized dep hashes to avoid exponential blowup
@@ -455,7 +459,11 @@ fn calculate<'a, 'cfg>(
455459
.map(|dep| {
456460
calculate(cx, dep).and_then(|fingerprint| {
457461
let name = cx.bcx.extern_crate_name(unit, dep)?;
458-
Ok((dep.pkg.package_id().to_string(), name, fingerprint))
462+
Ok(DepFingerprint {
463+
pkg_id: dep.pkg.package_id().to_string(),
464+
name,
465+
fingerprint,
466+
})
459467
})
460468
})
461469
.collect::<CargoResult<Vec<_>>>()?;
@@ -470,7 +478,7 @@ fn calculate<'a, 'cfg>(
470478
LocalFingerprint::Precalculated(fingerprint)
471479
};
472480
let mut deps = deps;
473-
deps.sort_by(|&(ref a, _, _), &(ref b, _, _)| a.cmp(b));
481+
deps.sort_by(|a, b| a.pkg_id.cmp(&b.pkg_id));
474482
let extra_flags = if unit.mode.is_doc() {
475483
bcx.rustdocflags_args(unit)?
476484
} else {

0 commit comments

Comments
 (0)