Trait manyhow::SpanRanged

source ·
pub trait SpanRanged {
    // Required method
    fn span_range(&self) -> Range<Span> ;

    // Provided method
    fn span_joined(&self) -> Option<Span> { ... }
}
Expand description

Returns the Range<Span> from the start to the end of multi-token structures.

start and end can be the same when called on single Tokens or Span.

Due to compiler limitations, it is currently not possible to implement SpanRanged for T: ToTokens, therefor there is to_tokens_span_range().

For types that either implement SpanRanged or ToTokens (but NOT both) the span_range! macro can be used as well.

§Motivation

This is superior to a normal Span (at least until Span::join works on stable), because it leads to better error messages:

Given the following expression

let a = |something: usize| something;

ErrorMessage::new(first_pipe_span, "error message") would result in something like

error: error message

let a = |something: usize| something;
        ^

While ErrorMessage::new(first_pipe_span..something_span, "error message") would improve the error message to:

error: error message

let a = |something: usize| something;
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Required Methods§

source

fn span_range(&self) -> Range<Span>

Returns the Range<Span> fully encompasing self

Provided Methods§

source

fn span_joined(&self) -> Option<Span>

Returns Self::span_range as a single span if possible, currently only possible on nightly. more

Implementations on Foreign Types§

source§

impl SpanRanged for Span

source§

impl SpanRanged for TokenStream

source§

impl SpanRanged for Span

source§

impl<A: SpanRanged, B: SpanRanged> SpanRanged for (A, B)

source§

impl<T: SpanRanged> SpanRanged for Option<T>

source§

impl<T: SpanRanged> SpanRanged for &T

source§

impl<T: SpanRanged> SpanRanged for Range<T>

Implementors§