Skip to content

Commit

Permalink
Merge pull request #374 from neocturne/inline
Browse files Browse the repository at this point in the history
Allow inlining standard Parse* trait impls
  • Loading branch information
kevinmehall committed Apr 25, 2024
2 parents 1b2da45 + 45b32b2 commit 2fc1cad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions peg-runtime/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use super::{Parse, ParseElem, ParseLiteral, ParseSlice, RuleResult};

impl<T> Parse for [T] {
type PositionRepr = usize;
#[inline]
fn start(&self) -> usize {
0
}

#[inline]
fn is_eof(&self, pos: usize) -> bool {
pos >= self.len()
}

#[inline]
fn position_repr(&self, pos: usize) -> usize {
pos
}
Expand All @@ -18,6 +21,7 @@ impl<T> Parse for [T] {
impl<'input, T: 'input + Copy> ParseElem<'input> for [T] {
type Element = T;

#[inline]
fn parse_elem(&'input self, pos: usize) -> RuleResult<T> {
match self[pos..].first() {
Some(c) => RuleResult::Matched(pos + 1, *c),
Expand All @@ -27,6 +31,7 @@ impl<'input, T: 'input + Copy> ParseElem<'input> for [T] {
}

impl ParseLiteral for [u8] {
#[inline]
fn parse_string_literal(&self, pos: usize, literal: &str) -> RuleResult<()> {
let l = literal.len();
if self.len() >= pos + l && &self[pos..pos + l] == literal.as_bytes() {
Expand All @@ -39,6 +44,7 @@ impl ParseLiteral for [u8] {

impl<'input, T: 'input> ParseSlice<'input> for [T] {
type Slice = &'input [T];
#[inline]
fn parse_slice(&'input self, p1: usize, p2: usize) -> &'input [T] {
&self[p1..p2]
}
Expand Down
5 changes: 5 additions & 0 deletions peg-runtime/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ impl Display for LineCol {

impl Parse for str {
type PositionRepr = LineCol;
#[inline]
fn start(&self) -> usize {
0
}

#[inline]
fn is_eof(&self, pos: usize) -> bool {
pos >= self.len()
}
Expand All @@ -47,6 +49,7 @@ impl Parse for str {
impl<'input> ParseElem<'input> for str {
type Element = char;

#[inline]
fn parse_elem(&'input self, pos: usize) -> RuleResult<char> {
match self[pos..].chars().next() {
Some(c) => RuleResult::Matched(pos + c.len_utf8(), c),
Expand All @@ -56,6 +59,7 @@ impl<'input> ParseElem<'input> for str {
}

impl ParseLiteral for str {
#[inline]
fn parse_string_literal(&self, pos: usize, literal: &str) -> RuleResult<()> {
let l = literal.len();
if self.len() >= pos + l && &self.as_bytes()[pos..pos + l] == literal.as_bytes() {
Expand All @@ -68,6 +72,7 @@ impl ParseLiteral for str {

impl<'input> ParseSlice<'input> for str {
type Slice = &'input str;
#[inline]
fn parse_slice(&'input self, p1: usize, p2: usize) -> &'input str {
&self[p1..p2]
}
Expand Down

0 comments on commit 2fc1cad

Please sign in to comment.