Skip to content

Commit

Permalink
Implement FXOrig for optional types
Browse files Browse the repository at this point in the history
Also renamed method `span` into `orig_span` and added `fx_span`
which returns non-optional span.
  • Loading branch information
vrurg committed Nov 26, 2024
1 parent b1c9b69 commit 3a421b7
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion fieldx_aux/src/with_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,29 @@ where
fn orig(&self) -> Option<&O>;

#[allow(dead_code)]
fn span(&self) -> Option<Span> {
fn orig_span(&self) -> Option<Span> {
self.orig().and_then(|o| Some(o.span()))
}

fn fx_span(&self) -> Span {
self.orig_span().unwrap_or_else(|| Span::call_site())
}
}

impl<O, T> FXOrig<O> for Option<T>
where
O: Spanned,
T: FXOrig<O>,
{
fn orig(&self) -> Option<&O> {
self.as_ref().and_then(|s| s.orig())
}

fn orig_span(&self) -> Option<Span> {
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())
}
}

0 comments on commit 3a421b7

Please sign in to comment.