Make SetOnce nicer to use

This commit is contained in:
Xiretza 2022-09-11 18:30:18 +02:00
parent efb20bc855
commit d4a1a6f698
3 changed files with 39 additions and 26 deletions

View file

@ -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 {