From 3a421b7694cf4934bb00ee146ac34f2430f52c58 Mon Sep 17 00:00:00 2001 From: Vadim Belman Date: Tue, 26 Nov 2024 10:58:02 -0500 Subject: [PATCH] Implement FXOrig for optional types Also renamed method `span` into `orig_span` and added `fx_span` which returns non-optional span. --- fieldx_aux/src/with_origin.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/fieldx_aux/src/with_origin.rs b/fieldx_aux/src/with_origin.rs index 48cb73f..8d1b444 100644 --- a/fieldx_aux/src/with_origin.rs +++ b/fieldx_aux/src/with_origin.rs @@ -9,7 +9,29 @@ where fn orig(&self) -> Option<&O>; #[allow(dead_code)] - fn span(&self) -> Option { + fn orig_span(&self) -> Option { self.orig().and_then(|o| Some(o.span())) } + + fn fx_span(&self) -> Span { + self.orig_span().unwrap_or_else(|| Span::call_site()) + } +} + +impl FXOrig for Option +where + O: Spanned, + T: FXOrig, +{ + fn orig(&self) -> Option<&O> { + self.as_ref().and_then(|s| s.orig()) + } + + fn orig_span(&self) -> Option { + self.as_ref().and_then(|s| s.orig_span()) + } + + fn fx_span(&self) -> Span { + self.as_ref().map_or_else(|| Span::call_site(), |s| s.fx_span()) + } }