From: Ian Jackson Date: Wed, 12 May 2021 00:37:26 +0000 (+0100) Subject: progress: Refactor/rework. Produces weird spurious warnings X-Git-Tag: otter-0.6.0~335 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=2e8b8fcb59b69e804287818d95c250ccd8a6cbd4;p=otter.git progress: Refactor/rework. Produces weird spurious warnings warning: unused variable: `phase` --> src/progress.rs:53:40 | 53 | fn phase_entry<'p,'e,P,E>(&mut self, phase: P, entry: E) | ^^^^^ help: if this is intentional, prefix it with an underscore: `_phase` | = note: `#[warn(unused_variables)]` on by default rustc 1.53.0-nightly (132b4e5d1 2021-04-13) Signed-off-by: Ian Jackson --- diff --git a/src/bundles.rs b/src/bundles.rs index fb64aeee..0ed58069 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -407,7 +407,7 @@ impl BundleParseErrorHandling for BundleParseUpload { #[throws(EH::Err)] fn parse_bundle(id: Id, file: &mut dyn ReadSeek, eh: EH, - _for_progress: &mut dyn ProgressReporter) -> Parsed + _for_progress: &mut dyn progress::Reporter) -> Parsed where EH: BundleParseErrorHandling, { match id.kind { Kind::Zip => () } @@ -440,7 +440,7 @@ fn parse_bundle(id: Id, file: &mut dyn ReadSeek, eh: EH, #[throws(LE)] fn process_bundle(id: Id, instance: &InstanceName, - _for_progress: &dyn ProgressReporter) + _for_progress: &dyn progress::Reporter) { let dir = id.path_dir(instance); fs::create_dir(&dir) @@ -590,7 +590,7 @@ impl InstanceBundles { impl Uploading { #[throws(MgmtError)] pub fn bulk(self, data: &mut R, expected: &Hash, - for_progress: &mut dyn ProgressReporter) -> Uploaded + for_progress: &mut dyn progress::Reporter) -> Uploaded where R: Read { let Uploading { id, mut file, instance } = self; diff --git a/src/prelude.rs b/src/prelude.rs index d4164d99..82db9dc7 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -94,7 +94,7 @@ pub use serde_with::DeserializeFromStr; pub use serde_with::SerializeDisplay; pub use sha2::{Sha512, Sha512Trunc256}; pub use slotmap::{dense::DenseSlotMap, SparseSecondaryMap, Key as _}; -pub use strum::{EnumString, EnumIter, EnumProperty}; +pub use strum::{AsStaticRef, EnumCount, EnumString, EnumIter, EnumProperty}; pub use strum::{IntoEnumIterator, IntoStaticStr}; pub use subtle::ConstantTimeEq; pub use tempfile::NamedTempFile; @@ -150,7 +150,7 @@ pub use crate::packetframe::{ReadExt}; pub use crate::pcaliases::*; pub use crate::pcrender::*; pub use crate::pieces::*; -pub use crate::progress::*; +pub use crate::progress::{self, ProgressInfo}; pub use crate::shapelib; pub use crate::shapelib::{CircleShape, RectShape}; pub use crate::slotmap_slot_idx::*; diff --git a/src/progress.rs b/src/progress.rs index 2d1486b3..66c2005c 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -7,27 +7,56 @@ use crate::packetframe::ResponseWriter; #[derive(Debug,Clone,Serialize,Deserialize,IntoOwned)] pub struct ProgressInfo<'pi> { - phase_num: usize, - phases: usize, - phase_desc: Cow<'pi, str>, - count: usize, - of: usize, - desc: Cow<'pi, str>, + phase: Count<'pi>, + entry: Count<'pi>, } -pub trait ProgressReporter { +#[derive(Debug,Clone,Serialize,Deserialize,IntoOwned)] +pub struct Count<'pi> { + pub i: usize, + pub n: usize, + pub desc: Cow<'pi, str>, +} + +pub trait Reporter { fn report(&mut self, info: ProgressInfo<'_>) -> Result<(), MgmtChannelWriteError>; } -impl ProgressReporter for ResponseWriter<'_, W> where W: Write { +impl Reporter for ResponseWriter<'_, W> where W: Write { #[throws(MgmtChannelWriteError)] fn report(&mut self, pi: ProgressInfo<'_>) { self.progress(pi)? } } -impl ProgressReporter for () { +impl Reporter for () { #[throws(MgmtChannelWriteError)] fn report(&mut self, _pi: ProgressInfo<'_>) { } } + +impl From for Count<'static> +where T: EnumCount + ToPrimitive + AsStaticRef +{ + fn from(t: T) -> Count<'static> { + Count { + i: t.to_usize().unwrap(), + n: T::COUNT, + desc: Cow::Borrowed(t.as_static()), + } + } +} + +#[ext(pub)] +impl &mut dyn Reporter { + #[throws(MgmtChannelWriteError)] + fn phase_entry<'p,'e,P,E>(&mut self, phase: P, entry: E) + where P: Into>, + E: Into>, + { + self.report(ProgressInfo { + phase: phase.into(), + entry: entry.into(), + })? + } +}