Skip to content

Commit 67759b7

Browse files
authored
Rollup merge of #72446 - dtolnay:ord, r=petrochenkov
Impl Ord for proc_macro::LineColumn ```rust impl Ord for LineColumn {...} impl PartialOrd for LineColumn {...} ``` for https://doc.rust-lang.org/nightly/proc_macro/struct.LineColumn.html. The ordering is the natural one you would get by writing one line after another, where we compare line first, then compare columns within the same line.
2 parents d5e3009 + 5a4bf44 commit 67759b7

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/libproc_macro/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mod diagnostic;
3939
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
4040
pub use diagnostic::{Diagnostic, Level, MultiSpan};
4141

42+
use std::cmp::Ordering;
4243
use std::ops::{Bound, RangeBounds};
4344
use std::path::PathBuf;
4445
use std::str::FromStr;
@@ -420,6 +421,20 @@ impl !Send for LineColumn {}
420421
#[unstable(feature = "proc_macro_span", issue = "54725")]
421422
impl !Sync for LineColumn {}
422423

424+
#[unstable(feature = "proc_macro_span", issue = "54725")]
425+
impl Ord for LineColumn {
426+
fn cmp(&self, other: &Self) -> Ordering {
427+
self.line.cmp(&other.line).then(self.column.cmp(&other.column))
428+
}
429+
}
430+
431+
#[unstable(feature = "proc_macro_span", issue = "54725")]
432+
impl PartialOrd for LineColumn {
433+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
434+
Some(self.cmp(other))
435+
}
436+
}
437+
423438
/// The source file of a given `Span`.
424439
#[unstable(feature = "proc_macro_span", issue = "54725")]
425440
#[derive(Clone)]

src/libproc_macro/tests/test.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(proc_macro_span)]
2+
3+
use proc_macro::LineColumn;
4+
5+
#[test]
6+
fn test_line_column_ord() {
7+
let line0_column0 = LineColumn { line: 0, column: 0 };
8+
let line0_column1 = LineColumn { line: 0, column: 1 };
9+
let line1_column0 = LineColumn { line: 1, column: 0 };
10+
assert!(line0_column0 < line0_column1);
11+
assert!(line0_column1 < line1_column0);
12+
}

0 commit comments

Comments
 (0)