Skip to content

Commit b441d26

Browse files
authored
Rollup merge of #77022 - Juici:master, r=lcnr
Reduce boilerplate for BytePos and CharPos Reduces boilerplate code for BytePos and CharPos by using a macro to implement shared traits.
2 parents 42b3303 + 9a1f177 commit b441d26

File tree

1 file changed

+55
-82
lines changed

1 file changed

+55
-82
lines changed

compiler/rustc_span/src/lib.rs

+55-82
Original file line numberDiff line numberDiff line change
@@ -1558,58 +1558,71 @@ pub trait Pos {
15581558
fn to_u32(&self) -> u32;
15591559
}
15601560

1561-
/// A byte offset. Keep this small (currently 32-bits), as AST contains
1562-
/// a lot of them.
1563-
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
1564-
pub struct BytePos(pub u32);
1565-
1566-
/// A character offset. Because of multibyte UTF-8 characters, a byte offset
1567-
/// is not equivalent to a character offset. The `SourceMap` will convert `BytePos`
1568-
/// values to `CharPos` values as necessary.
1569-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
1570-
pub struct CharPos(pub usize);
1561+
macro_rules! impl_pos {
1562+
(
1563+
$(
1564+
$(#[$attr:meta])*
1565+
$vis:vis struct $ident:ident($inner_vis:vis $inner_ty:ty);
1566+
)*
1567+
) => {
1568+
$(
1569+
$(#[$attr])*
1570+
$vis struct $ident($inner_vis $inner_ty);
1571+
1572+
impl Pos for $ident {
1573+
#[inline(always)]
1574+
fn from_usize(n: usize) -> $ident {
1575+
$ident(n as $inner_ty)
1576+
}
15711577

1572-
// FIXME: lots of boilerplate in these impls, but so far my attempts to fix
1573-
// have been unsuccessful.
1578+
#[inline(always)]
1579+
fn to_usize(&self) -> usize {
1580+
self.0 as usize
1581+
}
15741582

1575-
impl Pos for BytePos {
1576-
#[inline(always)]
1577-
fn from_usize(n: usize) -> BytePos {
1578-
BytePos(n as u32)
1579-
}
1583+
#[inline(always)]
1584+
fn from_u32(n: u32) -> $ident {
1585+
$ident(n as $inner_ty)
1586+
}
15801587

1581-
#[inline(always)]
1582-
fn to_usize(&self) -> usize {
1583-
self.0 as usize
1584-
}
1588+
#[inline(always)]
1589+
fn to_u32(&self) -> u32 {
1590+
self.0 as u32
1591+
}
1592+
}
15851593

1586-
#[inline(always)]
1587-
fn from_u32(n: u32) -> BytePos {
1588-
BytePos(n)
1589-
}
1594+
impl Add for $ident {
1595+
type Output = $ident;
15901596

1591-
#[inline(always)]
1592-
fn to_u32(&self) -> u32 {
1593-
self.0
1594-
}
1595-
}
1597+
#[inline(always)]
1598+
fn add(self, rhs: $ident) -> $ident {
1599+
$ident(self.0 + rhs.0)
1600+
}
1601+
}
15961602

1597-
impl Add for BytePos {
1598-
type Output = BytePos;
1603+
impl Sub for $ident {
1604+
type Output = $ident;
15991605

1600-
#[inline(always)]
1601-
fn add(self, rhs: BytePos) -> BytePos {
1602-
BytePos((self.to_usize() + rhs.to_usize()) as u32)
1603-
}
1606+
#[inline(always)]
1607+
fn sub(self, rhs: $ident) -> $ident {
1608+
$ident(self.0 - rhs.0)
1609+
}
1610+
}
1611+
)*
1612+
};
16041613
}
16051614

1606-
impl Sub for BytePos {
1607-
type Output = BytePos;
1615+
impl_pos! {
1616+
/// A byte offset. Keep this small (currently 32-bits), as AST contains
1617+
/// a lot of them.
1618+
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
1619+
pub struct BytePos(pub u32);
16081620

1609-
#[inline(always)]
1610-
fn sub(self, rhs: BytePos) -> BytePos {
1611-
BytePos((self.to_usize() - rhs.to_usize()) as u32)
1612-
}
1621+
/// A character offset. Because of multibyte UTF-8 characters, a byte offset
1622+
/// is not equivalent to a character offset. The `SourceMap` will convert `BytePos`
1623+
/// values to `CharPos` values as necessary.
1624+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
1625+
pub struct CharPos(pub usize);
16131626
}
16141627

16151628
impl<S: rustc_serialize::Encoder> Encodable<S> for BytePos {
@@ -1624,46 +1637,6 @@ impl<D: rustc_serialize::Decoder> Decodable<D> for BytePos {
16241637
}
16251638
}
16261639

1627-
impl Pos for CharPos {
1628-
#[inline(always)]
1629-
fn from_usize(n: usize) -> CharPos {
1630-
CharPos(n)
1631-
}
1632-
1633-
#[inline(always)]
1634-
fn to_usize(&self) -> usize {
1635-
self.0
1636-
}
1637-
1638-
#[inline(always)]
1639-
fn from_u32(n: u32) -> CharPos {
1640-
CharPos(n as usize)
1641-
}
1642-
1643-
#[inline(always)]
1644-
fn to_u32(&self) -> u32 {
1645-
self.0 as u32
1646-
}
1647-
}
1648-
1649-
impl Add for CharPos {
1650-
type Output = CharPos;
1651-
1652-
#[inline(always)]
1653-
fn add(self, rhs: CharPos) -> CharPos {
1654-
CharPos(self.to_usize() + rhs.to_usize())
1655-
}
1656-
}
1657-
1658-
impl Sub for CharPos {
1659-
type Output = CharPos;
1660-
1661-
#[inline(always)]
1662-
fn sub(self, rhs: CharPos) -> CharPos {
1663-
CharPos(self.to_usize() - rhs.to_usize())
1664-
}
1665-
}
1666-
16671640
// _____________________________________________________________________________
16681641
// Loc, SourceFileAndLine, SourceFileAndBytePos
16691642
//

0 commit comments

Comments
 (0)