Again, quite generic, and the traits are bascially auto-implemented.
Showing the changes to git-features/src/progress (1 of 1 file). Show all
Unified · Split
| @@ -74,55 +74,6 @@ impl Progress for Discard { · 73 unmodified lines | |||
| 74 | fn message(&mut self, _level: MessageLevel, _message: impl Into<String>) {} | 74 | fn message(&mut self, _level: MessageLevel, _message: impl Into<String>) {} |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | pub enum DoOrDiscard<T> { | ||
| 78 | Do(T), | ||
| 79 | Discard, | ||
| 80 | } | ||
| 81 | |||
| 82 | impl<T> From<Option<T>> for DoOrDiscard<T> | ||
| 83 | where | ||
| 84 | T: Progress, | ||
| 85 | { | ||
| 86 | fn from(p: Option<T>) -> Self { | ||
| 87 | match p { | ||
| 88 | Some(p) => DoOrDiscard::Do(p), | ||
| 89 | None => DoOrDiscard::Discard, | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | impl<T> Progress for DoOrDiscard<T> | ||
| 95 | where | ||
| 96 | T: Progress, | ||
| 97 | { | ||
| 98 | type SubProgress = DoOrDiscard<T::SubProgress>; | ||
| 99 | |||
| 100 | fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress { | ||
| 101 | match self { | ||
| 102 | DoOrDiscard::Discard => DoOrDiscard::Discard, | ||
| 103 | DoOrDiscard::Do(p) => DoOrDiscard::Do(p.add_child(name)), | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | fn init(&mut self, max: Option<u32>, unit: Option<&'static str>) { | ||
| 108 | if let DoOrDiscard::Do(p) = self { | ||
| 109 | p.init(max, unit) | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | fn set(&mut self, step: u32) { | ||
| 114 | if let DoOrDiscard::Do(p) = self { | ||
| 115 | p.set(step) | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | fn message(&mut self, level: MessageLevel, message: impl Into<String>) { | ||
| 120 | if let DoOrDiscard::Do(p) = self { | ||
| 121 | p.message(level, message) | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | pub enum Either<L, R> { | 77 | pub enum Either<L, R> { |
| 127 | Left(L), | 78 | Left(L), |
| 128 | Right(R), | 79 | Right(R), |
| @@ -164,6 +115,43 @@ where · 35 unmodified lines | |||
| 164 | } | 115 | } |
| 165 | } | 116 | } |
| 166 | 117 | ||
| 118 | pub struct DoOrDiscard<T>(Either<T, Discard>); | ||
| 119 | |||
| 120 | impl<T> From<Option<T>> for DoOrDiscard<T> | ||
| 121 | where | ||
| 122 | T: Progress, | ||
| 123 | { | ||
| 124 | fn from(p: Option<T>) -> Self { | ||
| 125 | match p { | ||
| 126 | Some(p) => DoOrDiscard(Either::Left(p)), | ||
| 127 | None => DoOrDiscard(Either::Right(Discard)), | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | impl<T> Progress for DoOrDiscard<T> | ||
| 133 | where | ||
| 134 | T: Progress, | ||
| 135 | { | ||
| 136 | type SubProgress = DoOrDiscard<T::SubProgress>; | ||
| 137 | |||
| 138 | fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress { | ||
| 139 | DoOrDiscard(self.0.add_child(name)) | ||
| 140 | } | ||
| 141 | |||
| 142 | fn init(&mut self, max: Option<u32>, unit: Option<&'static str>) { | ||
| 143 | self.0.init(max, unit) | ||
| 144 | } | ||
| 145 | |||
| 146 | fn set(&mut self, step: u32) { | ||
| 147 | self.0.set(step) | ||
| 148 | } | ||
| 149 | |||
| 150 | fn message(&mut self, level: MessageLevel, message: impl Into<String>) { | ||
| 151 | self.0.message(level, message) | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 167 | #[cfg(feature = "progress-log")] | 155 | #[cfg(feature = "progress-log")] |
| 168 | mod log; | 156 | mod log; |
| 169 | 157 | ||