rl-utils 0.2.2

Shared utility functions used across the rl-lang toolchain
Documentation
use std::ops::Range;

/// A byte-offset range into the source string.
///
/// Used for pointing error reports at exact source locations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Span {
    pub start: usize,
    pub end: usize,
}

impl Span {
    pub fn new(start: usize, end: usize) -> Self {
        Self { start, end }
    }

    /// A sentinel span used when no real location is known.
    pub fn dummy() -> Self {
        Self { start: 0, end: 0 }
    }

    /// Span covering both `self` and `other` (and everything between).
    pub fn join(self, other: Self) -> Self {
        Self {
            start: self.start.min(other.start),
            end: self.end.max(other.end),
        }
    }
}

impl From<Span> for Range<usize> {
    fn from(s: Span) -> Self {
        s.start..s.end
    }
}

impl From<Range<usize>> for Span {
    fn from(r: Range<usize>) -> Self {
        Self {
            start: r.start,
            end: r.end,
        }
    }
}