microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
76e91f14a9d330a29358b45692e7530a3ccae10d

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

compiler/qsc_data_structures/src/span.rs

76lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use miette::SourceSpan;
5use std::{
6 fmt::{self, Display, Formatter},
7 ops::{Add, Index, Sub},
8};
9
10/// A region between two offsets in an array. Spans are the half-open interval `[lo, hi)`.
11#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
12pub struct Span {
13 /// The smallest offset contained in the span.
14 pub lo: u32,
15 /// The next offset after the largest offset contained in the span.
16 pub hi: u32,
17}
18
19impl Add<u32> for Span {
20 type Output = Self;
21
22 fn add(self, rhs: u32) -> Self::Output {
23 Self {
24 lo: self.lo + rhs,
25 hi: self.hi + rhs,
26 }
27 }
28}
29
30impl Sub<u32> for Span {
31 type Output = Self;
32
33 fn sub(self, rhs: u32) -> Self::Output {
34 Self {
35 lo: self.lo - rhs,
36 hi: self.hi - rhs,
37 }
38 }
39}
40
41impl Display for Span {
42 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
43 write!(f, "[{}-{}]", self.lo, self.hi)?;
44 Ok(())
45 }
46}
47
48impl Index<Span> for str {
49 type Output = str;
50
51 fn index(&self, index: Span) -> &Self::Output {
52 &self[(index.lo as usize)..(index.hi as usize)]
53 }
54}
55
56impl Index<&Span> for str {
57 type Output = str;
58
59 fn index(&self, index: &Span) -> &Self::Output {
60 &self[(index.lo as usize)..(index.hi as usize)]
61 }
62}
63
64impl Index<Span> for String {
65 type Output = str;
66
67 fn index(&self, index: Span) -> &Self::Output {
68 &self[(index.lo as usize)..(index.hi as usize)]
69 }
70}
71
72impl From<Span> for SourceSpan {
73 fn from(value: Span) -> Self {
74 Self::from((value.lo as usize)..(value.hi as usize))
75 }
76}
77