Skip to content

Commit a4be982

Browse files
committed
Deduplicate implementations of LineColumn
1 parent 9924b79 commit a4be982

File tree

4 files changed

+45
-57
lines changed

4 files changed

+45
-57
lines changed

src/fallback.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(span_locations)]
2+
use crate::location::LineColumn;
13
use crate::parse::{self, Cursor};
24
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
35
use crate::{Delimiter, Spacing, TokenTree};
@@ -332,12 +334,6 @@ impl Debug for SourceFile {
332334
}
333335
}
334336

335-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
336-
pub(crate) struct LineColumn {
337-
pub line: usize,
338-
pub column: usize,
339-
}
340-
341337
#[cfg(span_locations)]
342338
thread_local! {
343339
static SOURCE_MAP: RefCell<SourceMap> = RefCell::new(SourceMap {

src/lib.rs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ use crate::fallback as imp;
139139
#[cfg(wrap_proc_macro)]
140140
mod imp;
141141

142+
#[cfg(span_locations)]
143+
mod location;
144+
142145
use crate::marker::Marker;
143146
use core::cmp::Ordering;
144147
use core::fmt::{self, Debug, Display};
@@ -150,6 +153,9 @@ use std::error::Error;
150153
#[cfg(procmacro2_semver_exempt)]
151154
use std::path::PathBuf;
152155

156+
#[cfg(span_locations)]
157+
pub use crate::location::LineColumn;
158+
153159
/// An abstract stream of tokens, or more concretely a sequence of token trees.
154160
///
155161
/// This type provides interfaces for iterating over token trees and for
@@ -356,37 +362,6 @@ impl Debug for SourceFile {
356362
}
357363
}
358364

359-
/// A line-column pair representing the start or end of a `Span`.
360-
///
361-
/// This type is semver exempt and not exposed by default.
362-
#[cfg(span_locations)]
363-
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
364-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
365-
pub struct LineColumn {
366-
/// The 1-indexed line in the source file on which the span starts or ends
367-
/// (inclusive).
368-
pub line: usize,
369-
/// The 0-indexed column (in UTF-8 characters) in the source file on which
370-
/// the span starts or ends (inclusive).
371-
pub column: usize,
372-
}
373-
374-
#[cfg(span_locations)]
375-
impl Ord for LineColumn {
376-
fn cmp(&self, other: &Self) -> Ordering {
377-
self.line
378-
.cmp(&other.line)
379-
.then(self.column.cmp(&other.column))
380-
}
381-
}
382-
383-
#[cfg(span_locations)]
384-
impl PartialOrd for LineColumn {
385-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
386-
Some(self.cmp(other))
387-
}
388-
}
389-
390365
/// A region of source code, along with macro expansion information.
391366
#[derive(Copy, Clone)]
392367
pub struct Span {
@@ -492,8 +467,7 @@ impl Span {
492467
#[cfg(span_locations)]
493468
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
494469
pub fn start(&self) -> LineColumn {
495-
let imp::LineColumn { line, column } = self.inner.start();
496-
LineColumn { line, column }
470+
self.inner.start()
497471
}
498472

499473
/// Get the ending line/column in the source file for this span.
@@ -508,8 +482,7 @@ impl Span {
508482
#[cfg(span_locations)]
509483
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
510484
pub fn end(&self) -> LineColumn {
511-
let imp::LineColumn { line, column } = self.inner.end();
512-
LineColumn { line, column }
485+
self.inner.end()
513486
}
514487

515488
/// Creates an empty span pointing to directly before this span.

src/location.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use core::cmp::Ordering;
2+
3+
/// A line-column pair representing the start or end of a `Span`.
4+
///
5+
/// This type is semver exempt and not exposed by default.
6+
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
7+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8+
pub struct LineColumn {
9+
/// The 1-indexed line in the source file on which the span starts or ends
10+
/// (inclusive).
11+
pub line: usize,
12+
/// The 0-indexed column (in UTF-8 characters) in the source file on which
13+
/// the span starts or ends (inclusive).
14+
pub column: usize,
15+
}
16+
17+
impl Ord for LineColumn {
18+
fn cmp(&self, other: &Self) -> Ordering {
19+
self.line
20+
.cmp(&other.line)
21+
.then(self.column.cmp(&other.column))
22+
}
23+
}
24+
25+
impl PartialOrd for LineColumn {
26+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
27+
Some(self.cmp(other))
28+
}
29+
}

src/wrapper.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::detection::inside_proc_macro;
2+
#[cfg(span_locations)]
3+
use crate::location::LineColumn;
24
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
35
use core::fmt::{self, Debug, Display};
46
use core::iter::FromIterator;
@@ -389,12 +391,6 @@ impl Debug for SourceFile {
389391
}
390392
}
391393

392-
#[cfg(any(super_unstable, feature = "span-locations"))]
393-
pub(crate) struct LineColumn {
394-
pub line: usize,
395-
pub column: usize,
396-
}
397-
398394
#[derive(Copy, Clone)]
399395
pub(crate) enum Span {
400396
Compiler(proc_macro::Span),
@@ -471,7 +467,7 @@ impl Span {
471467
}
472468
}
473469

474-
#[cfg(any(super_unstable, feature = "span-locations"))]
470+
#[cfg(span_locations)]
475471
pub fn start(&self) -> LineColumn {
476472
match self {
477473
#[cfg(proc_macro_span)]
@@ -481,14 +477,11 @@ impl Span {
481477
}
482478
#[cfg(not(proc_macro_span))]
483479
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
484-
Span::Fallback(s) => {
485-
let fallback::LineColumn { line, column } = s.start();
486-
LineColumn { line, column }
487-
}
480+
Span::Fallback(s) => s.start(),
488481
}
489482
}
490483

491-
#[cfg(any(super_unstable, feature = "span-locations"))]
484+
#[cfg(span_locations)]
492485
pub fn end(&self) -> LineColumn {
493486
match self {
494487
#[cfg(proc_macro_span)]
@@ -498,10 +491,7 @@ impl Span {
498491
}
499492
#[cfg(not(proc_macro_span))]
500493
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
501-
Span::Fallback(s) => {
502-
let fallback::LineColumn { line, column } = s.end();
503-
LineColumn { line, column }
504-
}
494+
Span::Fallback(s) => s.end(),
505495
}
506496
}
507497

0 commit comments

Comments
 (0)