diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-11-26 15:05:09 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-11-26 15:05:09 +0000 |
commit | 1542797284f5d3ea51d4e44c5c3c0c673d22d79a (patch) | |
tree | 58b0150a27a6b35000d745807461100786952e93 /crates/mbe/src/lib.rs | |
parent | 0bcd8147ec08db2a94dd2a3068a527ba6c47a4cd (diff) | |
parent | c784c8ec766edf4c7ab558f71d3f87e6c7c870b9 (diff) |
Merge #6638
6638: Use named fields in `ExpandResult` r=jonas-schievink a=jonas-schievink
bors r+ :robot:
Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/mbe/src/lib.rs')
-rw-r--r-- | crates/mbe/src/lib.rs | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index f854ca09a..183e3b988 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs | |||
@@ -246,33 +246,35 @@ fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> { | |||
246 | Ok(()) | 246 | Ok(()) |
247 | } | 247 | } |
248 | 248 | ||
249 | #[derive(Debug)] | 249 | #[derive(Debug, Clone, Eq, PartialEq)] |
250 | pub struct ExpandResult<T>(pub T, pub Option<ExpandError>); | 250 | pub struct ExpandResult<T> { |
251 | pub value: T, | ||
252 | pub err: Option<ExpandError>, | ||
253 | } | ||
251 | 254 | ||
252 | impl<T> ExpandResult<T> { | 255 | impl<T> ExpandResult<T> { |
253 | pub fn ok(t: T) -> ExpandResult<T> { | 256 | pub fn ok(value: T) -> Self { |
254 | ExpandResult(t, None) | 257 | Self { value, err: None } |
255 | } | 258 | } |
256 | 259 | ||
257 | pub fn only_err(err: ExpandError) -> ExpandResult<T> | 260 | pub fn only_err(err: ExpandError) -> Self |
258 | where | 261 | where |
259 | T: Default, | 262 | T: Default, |
260 | { | 263 | { |
261 | ExpandResult(Default::default(), Some(err)) | 264 | Self { value: Default::default(), err: Some(err) } |
262 | } | 265 | } |
263 | 266 | ||
264 | pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> { | 267 | pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> { |
265 | ExpandResult(f(self.0), self.1) | 268 | ExpandResult { value: f(self.value), err: self.err } |
266 | } | 269 | } |
267 | 270 | ||
268 | pub fn result(self) -> Result<T, ExpandError> { | 271 | pub fn result(self) -> Result<T, ExpandError> { |
269 | self.1.map(Err).unwrap_or(Ok(self.0)) | 272 | self.err.map(Err).unwrap_or(Ok(self.value)) |
270 | } | 273 | } |
271 | } | 274 | } |
272 | 275 | ||
273 | impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> { | 276 | impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> { |
274 | fn from(result: Result<T, ExpandError>) -> ExpandResult<T> { | 277 | fn from(result: Result<T, ExpandError>) -> Self { |
275 | result | 278 | result.map_or_else(|e| Self::only_err(e), |it| Self::ok(it)) |
276 | .map_or_else(|e| ExpandResult(Default::default(), Some(e)), |it| ExpandResult(it, None)) | ||
277 | } | 279 | } |
278 | } | 280 | } |