Skip to content

Commit 740ec43

Browse files
committed
chore: use const fn in Span
1 parent bb12771 commit 740ec43

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

crates/swc_common/src/syntax_pos.rs

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::sync::Mutex;
33
use std::{
44
borrow::Cow,
5-
cmp, fmt,
5+
fmt,
66
hash::{Hash, Hasher},
77
ops::{Add, Sub},
88
path::PathBuf,
@@ -385,71 +385,71 @@ extern "C" {
385385

386386
impl Span {
387387
#[inline]
388-
pub fn lo(self) -> BytePos {
388+
pub const fn lo(self) -> BytePos {
389389
self.lo
390390
}
391391

392392
#[inline]
393-
pub fn new(mut lo: BytePos, mut hi: BytePos) -> Self {
394-
if lo > hi {
395-
std::mem::swap(&mut lo, &mut hi);
393+
pub const fn new(lo: BytePos, hi: BytePos) -> Self {
394+
if lo.0 > hi.0 {
395+
Span { lo: hi, hi: lo }
396+
} else {
397+
Span { lo, hi }
396398
}
397-
398-
Span { lo, hi }
399399
}
400400

401401
#[inline]
402-
pub fn with_lo(&self, lo: BytePos) -> Span {
402+
pub const fn with_lo(&self, lo: BytePos) -> Span {
403403
Span::new(lo, self.hi)
404404
}
405405

406406
#[inline]
407-
pub fn hi(self) -> BytePos {
407+
pub const fn hi(self) -> BytePos {
408408
self.hi
409409
}
410410

411411
#[inline]
412-
pub fn with_hi(&self, hi: BytePos) -> Span {
412+
pub const fn with_hi(&self, hi: BytePos) -> Span {
413413
Span::new(self.lo, hi)
414414
}
415415

416416
/// Returns `true` if this is a dummy span with any hygienic context.
417417
#[inline]
418-
pub fn is_dummy(self) -> bool {
418+
pub const fn is_dummy(self) -> bool {
419419
self.lo.0 == 0 && self.hi.0 == 0 || self.lo.0 >= DUMMY_RESERVE
420420
}
421421

422422
#[inline]
423-
pub fn is_pure(self) -> bool {
423+
pub const fn is_pure(self) -> bool {
424424
self.lo.is_pure()
425425
}
426426

427427
#[inline]
428-
pub fn is_placeholder(self) -> bool {
428+
pub const fn is_placeholder(self) -> bool {
429429
self.lo.is_placeholder()
430430
}
431431

432432
/// Returns `true` if this is a dummy span with any hygienic context.
433433
#[inline]
434-
pub fn is_dummy_ignoring_cmt(self) -> bool {
435-
self.lo.0 == 0 && self.hi.0 == 0
434+
pub const fn is_dummy_ignoring_cmt(self) -> bool {
435+
self.lo.is_dummy() && self.hi.is_dummy()
436436
}
437437

438438
/// Returns a new span representing an empty span at the beginning of this
439439
/// span
440440
#[inline]
441-
pub fn shrink_to_lo(self) -> Span {
441+
pub const fn shrink_to_lo(self) -> Span {
442442
self.with_hi(self.lo)
443443
}
444444

445445
/// Returns a new span representing an empty span at the end of this span
446446
#[inline]
447-
pub fn shrink_to_hi(self) -> Span {
447+
pub const fn shrink_to_hi(self) -> Span {
448448
self.with_lo(self.hi)
449449
}
450450

451451
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
452-
pub fn substitute_dummy(self, other: Span) -> Span {
452+
pub const fn substitute_dummy(self, other: Span) -> Span {
453453
if self.is_dummy() {
454454
other
455455
} else {
@@ -458,22 +458,22 @@ impl Span {
458458
}
459459

460460
/// Return true if `self` fully encloses `other`.
461-
pub fn contains(self, other: Span) -> bool {
462-
self.lo <= other.lo && other.hi <= self.hi
461+
pub const fn contains(self, other: Span) -> bool {
462+
self.lo.0 <= other.lo.0 && other.hi.0 <= self.hi.0
463463
}
464464

465465
/// Return true if the spans are equal with regards to the source text.
466466
///
467467
/// Use this instead of `==` when either span could be generated code,
468468
/// and you only care that they point to the same bytes of source text.
469-
pub fn source_equal(self, other: Span) -> bool {
470-
self.lo == other.lo && self.hi == other.hi
469+
pub const fn source_equal(self, other: Span) -> bool {
470+
self.lo.0 == other.lo.0 && self.hi.0 == other.hi.0
471471
}
472472

473473
/// Returns `Some(span)`, where the start is trimmed by the end of `other`
474-
pub fn trim_start(self, other: Span) -> Option<Span> {
475-
if self.hi > other.hi {
476-
Some(self.with_lo(cmp::max(self.lo, other.hi)))
474+
pub const fn trim_start(self, other: Span) -> Option<Span> {
475+
if self.hi.0 > other.hi.0 {
476+
Some(self.with_lo(self.lo.max(other.hi)))
477477
} else {
478478
None
479479
}
@@ -488,31 +488,29 @@ impl Span {
488488
// output. It is preferable to have an incomplete span than a completely
489489
// nonsensical one.
490490

491-
Span::new(
492-
cmp::min(span_data.lo, end_data.lo),
493-
cmp::max(span_data.hi, end_data.hi),
494-
)
491+
let lo = span_data.lo.min(end_data.lo);
492+
let hi = span_data.hi.max(end_data.hi);
493+
Span::new(lo, hi)
495494
}
496495

497496
/// Return a `Span` between the end of `self` to the beginning of `end`.
498-
pub fn between(self, end: Span) -> Span {
497+
pub const fn between(self, end: Span) -> Span {
499498
let span = self;
500499
Span::new(span.hi, end.lo)
501500
}
502501

503502
/// Return a `Span` between the beginning of `self` to the beginning of
504503
/// `end`.
505-
pub fn until(self, end: Span) -> Span {
504+
pub const fn until(self, end: Span) -> Span {
506505
let span = self;
507506
Span::new(span.lo, end.lo)
508507
}
509508

510-
pub fn from_inner_byte_pos(self, start: usize, end: usize) -> Span {
509+
pub const fn from_inner_byte_pos(self, start: usize, end: usize) -> Span {
511510
let span = self;
512-
Span::new(
513-
span.lo + BytePos::from_usize(start),
514-
span.lo + BytePos::from_usize(end),
515-
)
511+
let lo = BytePos(span.lo.0 + start as u32);
512+
let hi = BytePos(span.lo.0 + end as u32);
513+
Span::new(lo, hi)
516514
}
517515

518516
/// Dummy span, both position are extremely large numbers so they would be
@@ -1109,6 +1107,22 @@ impl BytePos {
11091107
pub const fn can_have_comment(self) -> bool {
11101108
self.0 != 0
11111109
}
1110+
1111+
const fn max(self, other: BytePos) -> BytePos {
1112+
if self.0 > other.0 {
1113+
self
1114+
} else {
1115+
other
1116+
}
1117+
}
1118+
1119+
const fn min(self, other: BytePos) -> BytePos {
1120+
if self.0 < other.0 {
1121+
self
1122+
} else {
1123+
other
1124+
}
1125+
}
11121126
}
11131127

11141128
/// A character offset. Because of multibyte utf8 characters, a byte offset

0 commit comments

Comments
 (0)