pub struct Span { /* private fields */ }
Expand description
Represents a specific range of text within a Source
.
Internally Span
is extremely lightweight and is essentially just a reference to a
Source
and a range of bytes within that source, so it can be cheaply cloned and passed
around without issue. The underlying Source
mechanism is stored within an Rc
so
that it can be shared between multiple Span
s without needing to be cloned. This cheap
sharing, combined with the lack of any sort of tokenization in Quoth allows us to provide
direct access to the original, unmodified source text for any given Span
.
Spans can be created directly using Span::new
, or by using the Spanned
trait to
access the underlying Span
of a type.
use quoth::*;
use std::rc::Rc;
let span = Span::new(Rc::new(Source::from_str("Hello, world!")), 0..5);
Spans can be joined together using the Span::join
method, which will return a new
Span
that encompasses both of the original spans. This can be useful for combining
spans that were generated from different parts of the same source.
use quoth::*;
use std::rc::Rc;
let source = Rc::new(Source::from_str("Hello, world!"));
let span1 = Span::new(source.clone(), 0..5);
let span2 = Span::new(source.clone(), 7..12);
let encompassing_span = span1.join(&span2).unwrap();
assert_eq!(encompassing_span.source_text(), "Hello, world");