diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-05-23 19:10:00 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-23 19:10:00 +0200 |
| commit | 67759b74f44f4d119625115525b5375416e5f8d8 (patch) | |
| tree | a4258c27e56053adbed7c32ef19c50c7f8e8f0f0 | |
| parent | d5e3009bd8a2eb934415a1ed1b19b031b6f9c4e2 (diff) | |
| parent | 5a4bf448c764696be75eafb6631e182d95064e72 (diff) | |
| download | rust-67759b74f44f4d119625115525b5375416e5f8d8.tar.gz rust-67759b74f44f4d119625115525b5375416e5f8d8.zip | |
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.
| -rw-r--r-- | src/libproc_macro/lib.rs | 15 | ||||
| -rw-r--r-- | src/libproc_macro/tests/test.rs | 12 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index f11401b5a0c..2d5bd7e872b 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -39,6 +39,7 @@ mod diagnostic; #[unstable(feature = "proc_macro_diagnostic", issue = "54140")] pub use diagnostic::{Diagnostic, Level, MultiSpan}; +use std::cmp::Ordering; use std::ops::{Bound, RangeBounds}; use std::path::PathBuf; use std::str::FromStr; @@ -420,6 +421,20 @@ impl !Send for LineColumn {} #[unstable(feature = "proc_macro_span", issue = "54725")] impl !Sync for LineColumn {} +#[unstable(feature = "proc_macro_span", issue = "54725")] +impl Ord for LineColumn { + fn cmp(&self, other: &Self) -> Ordering { + self.line.cmp(&other.line).then(self.column.cmp(&other.column)) + } +} + +#[unstable(feature = "proc_macro_span", issue = "54725")] +impl PartialOrd for LineColumn { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + /// The source file of a given `Span`. #[unstable(feature = "proc_macro_span", issue = "54725")] #[derive(Clone)] diff --git a/src/libproc_macro/tests/test.rs b/src/libproc_macro/tests/test.rs new file mode 100644 index 00000000000..331b330cf29 --- /dev/null +++ b/src/libproc_macro/tests/test.rs @@ -0,0 +1,12 @@ +#![feature(proc_macro_span)] + +use proc_macro::LineColumn; + +#[test] +fn test_line_column_ord() { + let line0_column0 = LineColumn { line: 0, column: 0 }; + let line0_column1 = LineColumn { line: 0, column: 1 }; + let line1_column0 = LineColumn { line: 1, column: 0 }; + assert!(line0_column0 < line0_column1); + assert!(line0_column1 < line1_column0); +} |
