tamer: span: Introduce twospan

The intent is to support the composition and decomposition of spans such
that (A, B) is as documented here.  This only performs the trivial case for
the sake of providing a convenient API when the developer would otherwise
just type (S, S).
main
Mike Gerwitz 2021-10-11 09:56:48 -04:00
parent 1a2f6bd209
commit de3d7ef393
1 changed files with 22 additions and 0 deletions

View File

@ -346,6 +346,20 @@ impl Ord for Span {
// because otherwise your week has just been ruined.
assert_eq_size!(Span, u64);
impl From<Span> for (Span, Span) {
/// Expand a [`Span`] into a two-span.
///
/// A two-span `(A, B)` is equivalent to a span beginning at the start
/// of `A` and ending at the end of `B`.
///
/// We gain no resolution from performing this operation,
/// but it does allow for using a single span in contexts where a
/// higher resolution is supported.
fn from(span: Span) -> Self {
(span, span)
}
}
/// Context for byte offsets (e.g. a source file).
///
/// A context is lifetime-free and [`Copy`]-able,
@ -488,4 +502,12 @@ mod test {
assert_eq!((offset, len, ctx), (span.offset(), span.len(), span.ctx()));
}
#[test]
pub fn span_into_twospan() {
let ctx: Context = "foo".intern().into();
let span = Span::new(10, 50, ctx);
assert_eq!((span, span), span.into());
}
}