diff options
| author | Xiretza <xiretza@xiretza.xyz> | 2022-09-11 18:30:18 +0200 |
|---|---|---|
| committer | Xiretza <xiretza@xiretza.xyz> | 2022-09-22 17:25:50 +0200 |
| commit | d4a1a6f6986fd722506f7019d4ad669e46f15129 (patch) | |
| tree | ef04d12778a311d3cb806279df29da1602de49c6 /compiler/rustc_macros/src/diagnostics/utils.rs | |
| parent | efb20bc85547b48c2de0950fbca1dd5b2ed2a564 (diff) | |
| download | rust-d4a1a6f6986fd722506f7019d4ad669e46f15129.tar.gz rust-d4a1a6f6986fd722506f7019d4ad669e46f15129.zip | |
Make SetOnce nicer to use
Diffstat (limited to 'compiler/rustc_macros/src/diagnostics/utils.rs')
| -rw-r--r-- | compiler/rustc_macros/src/diagnostics/utils.rs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/compiler/rustc_macros/src/diagnostics/utils.rs b/compiler/rustc_macros/src/diagnostics/utils.rs index ad9ecd39b9e..3efcd216d19 100644 --- a/compiler/rustc_macros/src/diagnostics/utils.rs +++ b/compiler/rustc_macros/src/diagnostics/utils.rs @@ -172,13 +172,17 @@ pub(crate) struct FieldInfo<'a> { /// Small helper trait for abstracting over `Option` fields that contain a value and a `Span` /// for error reporting if they are set more than once. pub(crate) trait SetOnce<T> { - fn set_once(&mut self, _: (T, Span)); + fn set_once(&mut self, value: T, span: Span); fn value(self) -> Option<T>; + fn value_ref(&self) -> Option<&T>; } -impl<T> SetOnce<T> for Option<(T, Span)> { - fn set_once(&mut self, (value, span): (T, Span)) { +/// An [`Option<T>`] that keeps track of the span that caused it to be set; used with [`SetOnce`]. +pub(super) type SpannedOption<T> = Option<(T, Span)>; + +impl<T> SetOnce<T> for SpannedOption<T> { + fn set_once(&mut self, value: T, span: Span) { match self { None => { *self = Some((value, span)); @@ -194,6 +198,10 @@ impl<T> SetOnce<T> for Option<(T, Span)> { fn value(self) -> Option<T> { self.map(|(v, _)| v) } + + fn value_ref(&self) -> Option<&T> { + self.as_ref().map(|(v, _)| v) + } } pub(crate) trait HasFieldMap { |
