From 3985d518484fa163ed4f88c0100822140e8ddd18 Mon Sep 17 00:00:00 2001 From: wangzishuai1987 Date: Mon, 27 Apr 2026 16:08:09 +0000 Subject: [PATCH] refactor: deduplicate package_hashes by reusing grey_state version `grey_state::accumulate::package_hashes` and `grey_services::accumulation::accumulated_package_hashes` had identical core logic (extract package_hash from WorkReport slice). The only difference was an optional `.take(count)` in the grey-services version. Make `package_hashes` pub in grey-state and have grey-services delegate to it with a truncated slice, eliminating the duplication. Contributes to #186. --- grey/crates/grey-services/src/accumulation.rs | 10 ++++------ grey/crates/grey-state/src/accumulate.rs | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/grey/crates/grey-services/src/accumulation.rs b/grey/crates/grey-services/src/accumulation.rs index 14b871452..7718eab8e 100644 --- a/grey/crates/grey-services/src/accumulation.rs +++ b/grey/crates/grey-services/src/accumulation.rs @@ -382,13 +382,11 @@ pub fn integrate_preimages( } } -/// Collect the set of work-package hashes from accumulated reports (P function). +/// Collect the set of work-package hashes from the first `count` reports (P function). +/// +/// Delegates to [`grey_state::accumulate::package_hashes`] after truncating to `count`. pub fn accumulated_package_hashes(reports: &[WorkReport], count: usize) -> BTreeSet { - reports - .iter() - .take(count) - .map(|r| r.package_spec.package_hash) - .collect() + grey_state::accumulate::package_hashes(&reports[..count.min(reports.len())]) } #[cfg(test)] diff --git a/grey/crates/grey-state/src/accumulate.rs b/grey/crates/grey-state/src/accumulate.rs index 1f4d18348..d0903b023 100644 --- a/grey/crates/grey-state/src/accumulate.rs +++ b/grey/crates/grey-state/src/accumulate.rs @@ -220,7 +220,7 @@ fn partition_reports(reports: &[WorkReport]) -> (Vec, Vec BTreeSet { +pub fn package_hashes(reports: &[WorkReport]) -> BTreeSet { reports .iter() .map(|r| r.package_spec.package_hash)