@@ -1558,58 +1558,71 @@ pub trait Pos {
1558
1558
fn to_u32 ( & self ) -> u32 ;
1559
1559
}
1560
1560
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
+ }
1571
1577
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
+ }
1574
1582
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
+ }
1580
1587
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
+ }
1585
1593
1586
- #[ inline( always) ]
1587
- fn from_u32 ( n : u32 ) -> BytePos {
1588
- BytePos ( n)
1589
- }
1594
+ impl Add for $ident {
1595
+ type Output = $ident;
1590
1596
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
+ }
1596
1602
1597
- impl Add for BytePos {
1598
- type Output = BytePos ;
1603
+ impl Sub for $ident {
1604
+ type Output = $ident ;
1599
1605
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
+ } ;
1604
1613
}
1605
1614
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 ) ;
1608
1620
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 ) ;
1613
1626
}
1614
1627
1615
1628
impl < S : rustc_serialize:: Encoder > Encodable < S > for BytePos {
@@ -1624,46 +1637,6 @@ impl<D: rustc_serialize::Decoder> Decodable<D> for BytePos {
1624
1637
}
1625
1638
}
1626
1639
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
-
1667
1640
// _____________________________________________________________________________
1668
1641
// Loc, SourceFileAndLine, SourceFileAndBytePos
1669
1642
//
0 commit comments