From: Ian Jackson Date: Wed, 12 May 2021 18:27:41 +0000 (+0100) Subject: progress: Count conversion, rework and Provide more From impl's X-Git-Tag: otter-0.6.0~324 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=feca34ab7d4a76d89cdee7937b4884d04803a176;p=otter.git progress: Count conversion, rework and Provide more From impl's Signed-off-by: Ian Jackson --- diff --git a/src/bundles.rs b/src/bundles.rs index c4399d72..a0f9ac0a 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -440,6 +440,7 @@ enum Phase { #[strum(message="scan")] Scan, #[strum(message="parse shape catalogues")] ParseLibs, } +impl progress::Enum for Phase { } #[throws(EH::Err)] fn parse_bundle(id: Id, instance: &InstanceName, file: File, eh: EH, @@ -457,6 +458,7 @@ fn parse_bundle(id: Id, instance: &InstanceName, file: File, eh: EH, #[strum(message="metadata")] Meta, #[strum(message="shape libs")] Libs, } + impl progress::Enum for ToScan { } for_progress.phase_item(Phase::Scan, ToScan::Meta); let meta = eh.besteffort(||{ diff --git a/src/progress.rs b/src/progress.rs index 376ee39f..8504f477 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -59,33 +59,46 @@ impl Reporter for () { fn item_(&mut self, item: usize, desc: Cow<'_, str>) { } } -impl<'t,T> From<&'t T> for Count<'t> -where T: EnumCount + ToPrimitive + EnumMessage -{ - fn from(t: &'t T) -> Count<'t> { +pub trait Enum: EnumCount + ToPrimitive + EnumMessage { } +impl From for Count<'static> where T: Enum { + fn from(t: T) -> Count<'static> { Count { i: t.to_usize().unwrap(), n: T::COUNT, - desc: Cow::Borrowed(t.get_message().unwrap_or("...")), + // Show be Borrowed https://github.com/Peternator7/strum/issues/159 + desc: Cow::Owned(t.get_message().unwrap_or("...").to_owned()), } } } +impl<'t> From<&'t str> for Count<'t> { + fn from(s: &'t str) -> Count<'t> { + Count { i:0, n:0, desc: Cow::Borrowed(s) } + } +} +impl From for Count<'static> { + fn from(s: String) -> Count<'static> { + Count { i:0, n:0, desc: Cow::Owned(s) } + } +} +impl<'t> From<()> for Count<'t> { fn from(_:()) -> Count<'t> { + Count { i:0, n:0, desc: Cow::Borrowed("") } +} } #[ext(pub, name=ReporterExt)] impl &mut dyn Reporter { - fn phase_item(&mut self, phase: P, item: E) - where for <'p> &'p P: Into>, - for <'e> &'e E: Into>, + fn phase_item<'p,'e,P,E>(&mut self, phase: P, item: E) + where P: Into>, + E: Into>, { - let phase = &phase; let phase = phase.into(); - let item = &item; let item = item .into(); + let phase = phase.into(); + let item = item .into(); self.report(ProgressInfo { phase, item }); } - fn phase

(&mut self, phase: P, len: usize) - where for <'p> &'p P: Into>, + fn phase<'p,P>(&mut self, phase: P, len: usize) + where P: Into>, { - self.phase_begin_((&phase).into(), len) + self.phase_begin_(phase.into(), len) } fn item<'s,S>(&mut self, item: usize, desc: S)