History

express DoOrDiscard in terms of Either (progress)

Again, quite generic, and the traits are bascially auto-implemented.

Sebastian Thiel committed 6 years ago

Commit cb29a451 parent 237bb5e1 file · +37 −49Browse files

Unified · Split

git-features/src/progress/mod.rs+37−49
@@ -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
77pub enum DoOrDiscard<T> {
78 Do(T),
79 Discard,
80}
81
82impl<T> From<Option<T>> for DoOrDiscard<T>
83where
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
94impl<T> Progress for DoOrDiscard<T>
95where
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
126pub enum Either<L, R> {77pub 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
118pub struct DoOrDiscard<T>(Either<T, Discard>);
119
120impl<T> From<Option<T>> for DoOrDiscard<T>
121where
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
132impl<T> Progress for DoOrDiscard<T>
133where
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")]
168mod log;156mod log;
169 157