tamer: span (offset_add): New method

More will come in the future, including the ability to add two spans.
main
Mike Gerwitz 2021-10-11 10:28:35 -04:00
parent de3d7ef393
commit cf239531e0
1 changed files with 34 additions and 0 deletions

View File

@ -306,6 +306,18 @@ impl Span {
pub fn ctx(&self) -> Context {
self.ctx
}
/// Further offset a span.
///
/// This attempts to offset a span relative to its current offset by the
/// provided value.
/// If the resulting offset exceeds [`global::SourceFileSize`],
/// the result will be [`None`].
pub fn offset_add(self, value: global::SourceFileSize) -> Option<Self> {
self.offset
.checked_add(value)
.map(|offset| Self { offset, ..self })
}
}
impl Into<u64> for Span {
@ -503,6 +515,28 @@ mod test {
assert_eq!((offset, len, ctx), (span.offset(), span.len(), span.ctx()));
}
#[test]
pub fn span_offset_add() {
let ctx: Context = "addtest".intern().into();
let offset = 10;
let len = 5;
let span = Span::new(offset, len, ctx);
// Successful add.
assert_eq!(
span.offset_add(10),
Some(Span {
offset: offset + 10,
len,
ctx
})
);
// Fail, do not wrap.
assert_eq!(span.offset_add(global::SourceFileSize::MAX), None);
}
#[test]
pub fn span_into_twospan() {
let ctx: Context = "foo".intern().into();