Reduce boilerplate for BytePos and CharPos
This commit is contained in:
parent
e0bf356f9e
commit
b4b4a2f092
1 changed files with 56 additions and 83 deletions
|
@ -1558,24 +1558,21 @@ pub trait Pos {
|
||||||
fn to_u32(&self) -> u32;
|
fn to_u32(&self) -> u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A byte offset. Keep this small (currently 32-bits), as AST contains
|
macro_rules! impl_pos {
|
||||||
/// a lot of them.
|
(
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
$(
|
||||||
pub struct BytePos(pub u32);
|
$(#[$attr:meta])*
|
||||||
|
$vis:vis struct $ident:ident($inner_vis:vis $inner_ty:ty);
|
||||||
|
)*
|
||||||
|
) => {
|
||||||
|
$(
|
||||||
|
$(#[$attr])*
|
||||||
|
$vis struct $ident($inner_vis $inner_ty);
|
||||||
|
|
||||||
/// A character offset. Because of multibyte UTF-8 characters, a byte offset
|
impl Pos for $ident {
|
||||||
/// is not equivalent to a character offset. The `SourceMap` will convert `BytePos`
|
|
||||||
/// values to `CharPos` values as necessary.
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
|
||||||
pub struct CharPos(pub usize);
|
|
||||||
|
|
||||||
// FIXME: lots of boilerplate in these impls, but so far my attempts to fix
|
|
||||||
// have been unsuccessful.
|
|
||||||
|
|
||||||
impl Pos for BytePos {
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_usize(n: usize) -> BytePos {
|
fn from_usize(n: usize) -> $ident {
|
||||||
BytePos(n as u32)
|
$ident(n as $inner_ty)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -1584,32 +1581,48 @@ impl Pos for BytePos {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from_u32(n: u32) -> BytePos {
|
fn from_u32(n: u32) -> $ident {
|
||||||
BytePos(n)
|
$ident(n as $inner_ty)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_u32(&self) -> u32 {
|
fn to_u32(&self) -> u32 {
|
||||||
self.0
|
self.0 as u32
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for BytePos {
|
impl Add for $ident {
|
||||||
type Output = BytePos;
|
type Output = $ident;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn add(self, rhs: BytePos) -> BytePos {
|
fn add(self, rhs: $ident) -> $ident {
|
||||||
BytePos((self.to_usize() + rhs.to_usize()) as u32)
|
$ident((self.to_usize() + rhs.to_usize()) as $inner_ty)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for BytePos {
|
impl Sub for $ident {
|
||||||
type Output = BytePos;
|
type Output = $ident;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn sub(self, rhs: BytePos) -> BytePos {
|
fn sub(self, rhs: $ident) -> $ident {
|
||||||
BytePos((self.to_usize() - rhs.to_usize()) as u32)
|
$ident((self.to_usize() - rhs.to_usize()) as $inner_ty)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_pos! {
|
||||||
|
/// A byte offset. Keep this small (currently 32-bits), as AST contains
|
||||||
|
/// a lot of them.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
||||||
|
pub struct BytePos(pub u32);
|
||||||
|
|
||||||
|
/// A character offset. Because of multibyte UTF-8 characters, a byte offset
|
||||||
|
/// is not equivalent to a character offset. The `SourceMap` will convert `BytePos`
|
||||||
|
/// values to `CharPos` values as necessary.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
|
||||||
|
pub struct CharPos(pub usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: rustc_serialize::Encoder> Encodable<S> for BytePos {
|
impl<S: rustc_serialize::Encoder> Encodable<S> for BytePos {
|
||||||
|
@ -1624,46 +1637,6 @@ impl<D: rustc_serialize::Decoder> Decodable<D> for BytePos {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pos for CharPos {
|
|
||||||
#[inline(always)]
|
|
||||||
fn from_usize(n: usize) -> CharPos {
|
|
||||||
CharPos(n)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn to_usize(&self) -> usize {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn from_u32(n: u32) -> CharPos {
|
|
||||||
CharPos(n as usize)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn to_u32(&self) -> u32 {
|
|
||||||
self.0 as u32
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for CharPos {
|
|
||||||
type Output = CharPos;
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn add(self, rhs: CharPos) -> CharPos {
|
|
||||||
CharPos(self.to_usize() + rhs.to_usize())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for CharPos {
|
|
||||||
type Output = CharPos;
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn sub(self, rhs: CharPos) -> CharPos {
|
|
||||||
CharPos(self.to_usize() - rhs.to_usize())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// _____________________________________________________________________________
|
// _____________________________________________________________________________
|
||||||
// Loc, SourceFileAndLine, SourceFileAndBytePos
|
// Loc, SourceFileAndLine, SourceFileAndBytePos
|
||||||
//
|
//
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue