chiark / gitweb /
progress: Refactor/rework. Produces weird spurious warnings
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 May 2021 00:37:26 +0000 (01:37 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 May 2021 01:01:06 +0000 (02:01 +0100)
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 <ijackson@chiark.greenend.org.uk>
src/bundles.rs
src/prelude.rs
src/progress.rs

index fb64aeeec68d1f281c4bdb0e2b51d864865e8cf4..0ed5806999576072098fd93e8354df47a5a31489 100644 (file)
@@ -407,7 +407,7 @@ impl BundleParseErrorHandling for BundleParseUpload {
 
 #[throws(EH::Err)]
 fn parse_bundle<EH>(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<EH>(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<R>(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;
index d4164d992afdf2ad159bd4168565dfeb810a7221..82db9dc793cc5463930042efefaa943ceb8a8cb9 100644 (file)
@@ -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::*;
index 2d1486b3ce84dd523f52aff32248383f0051d18d..66c2005cc9aba3f39cde7f9e31fce6e93b6ca25f 100644 (file)
@@ -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<W> ProgressReporter for ResponseWriter<'_, W> where W: Write {
+impl<W> 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<T> From<T> for Count<'static>
+where T: EnumCount + ToPrimitive + AsStaticRef<str>
+{
+  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<Count<'p>>,
+        E: Into<Count<'e>>,
+  {
+    self.report(ProgressInfo {
+      phase: phase.into(),
+      entry: entry.into(),
+    })?
+  }
+}